From f4fa36e94127195261842501fb2ccb460312f8fd Mon Sep 17 00:00:00 2001 From: Yadciel Date: Fri, 31 Mar 2023 12:33:56 +0200 Subject: [PATCH] Fahrzeug --- fahrzeug.py | 35 +++++++++++++++++++++++++++++++++++ game.py | 4 ++-- player.py | 36 ------------------------------------ 3 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 fahrzeug.py delete mode 100644 player.py diff --git a/fahrzeug.py b/fahrzeug.py new file mode 100644 index 0000000..9727d40 --- /dev/null +++ b/fahrzeug.py @@ -0,0 +1,35 @@ +import pygame +import math + +class Fahrzeug(pygame.sprite.Sprite): + def __init__(self, x, y): + super().__init__() + self.image = pygame.image.load("player.png").convert_alpha() + self.rect = self.image.get_rect(center=(x, y)) + self.rect.centerx = x + self.rect.centery = y + self.speed = 2 + self.dest_x = x + self.dest_y = y + self.direction = pygame.math.Vector2(0, -1) + self.angle = 0 + + def draw(self, win): + rotated_image = pygame.transform.rotate(self.image, self.angle) + rotated_rect = rotated_image.get_rect(center=self.rect.center) + #self.angle += 1 + #pygame.draw.rect(win, (255, 0, 0), rotated_rect) + win.blit(rotated_image, rotated_rect) + + def update(self): + 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 + + def dest(self, pos): + self.dest_x, self.dest_y = pos diff --git a/game.py b/game.py index 294cfd8..5c653f4 100644 --- a/game.py +++ b/game.py @@ -1,11 +1,11 @@ import pygame -import player +import fahrzeug import sys def start(): pygame.init() win = pygame.display.set_mode((800, 800)) - player1 = player.Player(0, 0) + player1 = fahrzeug.Fahrzeug(0, 0) # Farben white = (255, 255, 255) diff --git a/player.py b/player.py deleted file mode 100644 index 78aac9c..0000000 --- a/player.py +++ /dev/null @@ -1,36 +0,0 @@ -import pygame -import math - -class Player(pygame.sprite.Sprite): - def __init__(self, x, y): - super().__init__() - self.image = pygame.image.load("player.png").convert_alpha() - self.rect = self.image.get_rect() - self.rect.x = x - self.rect.y = y - self.speed = 2 - self.dest_x = x - self.dest_y = y - self.direction = pygame.math.Vector2(1, 0) - - def draw(self, win): - win.blit(self.image, self.rect) - - def update(self): - if self.rect.x < self.dest_x: - self.rect.x += self.speed - if self.rect.x > self.dest_x: - self.rect.x -= self.speed - if self.rect.y < self.dest_y: - self.rect.y += self.speed - if self.rect.y > self.dest_y: - self.rect.y -= self.speed - - direction = pygame.math.Vector2(self.dest_x - self.rect.centerx, self.dest_y - self.rect.centery) - angle = -math.degrees(math.atan2(direction.y, direction.x)) - self.image = pygame.transform.rotate(self.image, angle) - self.rect = self.image.get_rect(center=self.rect.center) - self.direction = direction.normalize() - - def dest(self, pos): - self.dest_x, self.dest_y = pos \ No newline at end of file