2023-04-20 09:08:35 +02:00
|
|
|
import pygame
|
|
|
|
from fahrzeug import Fahrzeug
|
|
|
|
|
2023-05-08 22:11:59 +02:00
|
|
|
|
2023-04-20 09:08:35 +02:00
|
|
|
class Player(Fahrzeug, pygame.sprite.Sprite):
|
|
|
|
def __init__(self, x, y):
|
|
|
|
super().__init__(x, y)
|
2023-04-22 12:45:53 +02:00
|
|
|
self.image = pygame.image.load("resources/player.png").convert_alpha()
|
2023-05-08 22:11:59 +02:00
|
|
|
self.image = pygame.transform.scale(self.image, (self.image.get_width() / 1.2, self.image.get_height() / 1.2))
|
2023-04-20 09:08:35 +02:00
|
|
|
self.rect = self.image.get_rect(center=(x, y))
|
|
|
|
self.rect.centerx = x
|
|
|
|
self.rect.centery = y
|
|
|
|
self.speed = 1
|
|
|
|
self.dest_x = x
|
|
|
|
self.dest_y = y
|
|
|
|
self.direction = pygame.math.Vector2(0, -1)
|
|
|
|
self.angle = 0
|
|
|
|
self.Tank = 250
|
2023-04-27 13:21:11 +02:00
|
|
|
self.Erz = 0
|
2023-05-08 22:11:59 +02:00
|
|
|
self.debug = False
|
|
|
|
self.b_move = False
|
2023-04-20 09:08:35 +02:00
|
|
|
|
|
|
|
def draw(self, win):
|
|
|
|
rotated_image = pygame.transform.rotate(self.image, self.angle)
|
|
|
|
rotated_rect = rotated_image.get_rect(center=self.rect.center)
|
|
|
|
|
2023-05-08 22:11:59 +02:00
|
|
|
# debug, nicht anrühren
|
|
|
|
# self.angle += 1
|
|
|
|
if self.debug:
|
|
|
|
pygame.draw.rect(win, (255, 0, 0), rotated_rect)
|
2023-04-20 09:08:35 +02:00
|
|
|
|
|
|
|
win.blit(rotated_image, rotated_rect)
|
|
|
|
|
|
|
|
def move(self):
|
|
|
|
if self.Tank > 0:
|
|
|
|
if self.rect.centerx < self.dest_x:
|
|
|
|
self.rect.centerx += self.speed
|
|
|
|
if self.rect.centerx > self.dest_x:
|
|
|
|
self.rect.centerx -= self.speed
|
|
|
|
if self.rect.centery < self.dest_y:
|
|
|
|
self.rect.centery += self.speed
|
|
|
|
if self.rect.centery > self.dest_y:
|
|
|
|
self.rect.centery -= self.speed
|
|
|
|
|
2023-05-08 22:11:59 +02:00
|
|
|
#bug wenn beide methoden zeitgleich benutzt werden
|
|
|
|
|
|
|
|
self.b_move = False
|
2023-04-20 09:08:35 +02:00
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
self.rect.centerx -= self.speed
|
|
|
|
self.dest_x -= self.speed
|
|
|
|
self.angle = 90
|
2023-05-08 22:11:59 +02:00
|
|
|
self.b_move = True
|
2023-04-20 09:08:35 +02:00
|
|
|
if keys[pygame.K_RIGHT]:
|
|
|
|
self.rect.centerx += self.speed
|
|
|
|
self.dest_x += self.speed
|
|
|
|
self.angle = 270
|
2023-05-08 22:11:59 +02:00
|
|
|
self.b_move = True
|
2023-04-20 09:08:35 +02:00
|
|
|
if keys[pygame.K_UP]:
|
|
|
|
self.rect.centery -= self.speed
|
|
|
|
self.dest_y -= self.speed
|
|
|
|
self.angle = 0
|
2023-05-08 22:11:59 +02:00
|
|
|
self.b_move = True
|
2023-04-20 09:08:35 +02:00
|
|
|
if keys[pygame.K_DOWN]:
|
|
|
|
self.rect.centery += self.speed
|
|
|
|
self.dest_y += self.speed
|
|
|
|
self.angle = 180
|
2023-05-08 22:11:59 +02:00
|
|
|
self.b_move = True
|
|
|
|
if self.b_move:
|
|
|
|
self.Tank -= 0.1
|
2023-04-20 09:08:35 +02:00
|
|
|
|
|
|
|
def dest(self, pos):
|
|
|
|
self.dest_x, self.dest_y = pos
|