diff --git a/fahrzeug.py b/fahrzeug.py index 9fef426..c428627 100644 --- a/fahrzeug.py +++ b/fahrzeug.py @@ -14,10 +14,6 @@ def get_angle(p1, p2): 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 = 1 self.dest_x = x self.dest_y = y @@ -25,16 +21,6 @@ class Fahrzeug(pygame.sprite.Sprite): self.angle = 0 self.Tank = 250 - def draw(self, win): - rotated_image = pygame.transform.rotate(self.image, self.angle) - rotated_rect = rotated_image.get_rect(center=self.rect.center) - - #debug, nicht anrühren - #self.angle += 1 - #pygame.draw.rect(win, (255, 0, 0), rotated_rect) - - win.blit(rotated_image, rotated_rect) - def update(self): self.move() @@ -43,34 +29,9 @@ class Fahrzeug(pygame.sprite.Sprite): self.Tank -= 0.1 self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y))-90 - 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 + def move(self): + pass - keys = pygame.key.get_pressed() - if keys[pygame.K_LEFT]: - self.rect.centerx -= self.speed - self.dest_x -= self.speed - self.angle = 90 - if keys[pygame.K_RIGHT]: - self.rect.centerx += self.speed - self.dest_x += self.speed - self.angle = 270 - if keys[pygame.K_UP]: - self.rect.centery -= self.speed - self.dest_y -= self.speed - self.angle = 0 - if keys[pygame.K_DOWN]: - self.rect.centery += self.speed - self.dest_y += self.speed - self.angle = 180 + def dest(self, pos): + self.dest_x, self.dest_y = pos - def dest(self, pos): - self.dest_x, self.dest_y = pos diff --git a/game.py b/game.py index ce02bcb..0a2bcc8 100644 --- a/game.py +++ b/game.py @@ -1,5 +1,6 @@ import pygame -import fahrzeug +import player +import helicopter import background import tilemap import sys @@ -7,7 +8,8 @@ import sys def start(): pygame.init() win = pygame.display.set_mode((800, 800)) - player1 = fahrzeug.Fahrzeug(0, 0) + player1 = player.Player(0, 0) + enemy = helicopter.Helicopter(5, 5) Erzmine = background.Erz() Tanken = background.Tank() Ziele = background.Ziel() @@ -70,4 +72,7 @@ def start(): player1.update() player1.draw(win) + enemy.update(player1) + enemy.draw(win) + pygame.display.update() diff --git a/helicopter.jpg b/helicopter.jpg new file mode 100644 index 0000000..c42fa18 Binary files /dev/null and b/helicopter.jpg differ diff --git a/helicopter.py b/helicopter.py new file mode 100644 index 0000000..f6ebc3a --- /dev/null +++ b/helicopter.py @@ -0,0 +1,52 @@ +import pygame +import player +import rotor +from fahrzeug import Fahrzeug + +class Helicopter(Fahrzeug, pygame.sprite.Sprite): + def __init__(self, x, y): + super().__init__(x, y) + self.image = pygame.image.load("helicopter.jpg").convert_alpha() + self.image = self.image.subsurface(44, 0, 44, 99) + self.rect = self.image.get_rect(center=(x, y)) + self.rect.centerx = x + self.rect.centery = y + self.rotor = rotor.Rotor(self.rect.centerx, self.rect.centery) + self.dest_x = x + self.dest_y = y + self.rect = self.image.get_rect() + self.angle = 0 + self.speed = 1 + + def update(self, player): + keys = pygame.key.get_pressed() + if keys[pygame.K_a]: + self.rect.centerx -= self.speed + self.dest_x -= self.speed + self.angle = 90 + if keys[pygame.K_d]: + self.rect.centerx += self.speed + self.dest_x += self.speed + self.angle = 270 + if keys[pygame.K_w]: + self.rect.centery -= self.speed + self.dest_y -= self.speed + self.angle = 0 + if keys[pygame.K_s]: + self.rect.centery += self.speed + self.dest_y += self.speed + self.angle = 180 + + self.rotor.update(self) + + def draw(self, win): + rotated_image = pygame.transform.rotate(self.image, self.angle) + rotated_rect = rotated_image.get_rect(center=self.rect.center) + + #debug, nicht anrühren + #self.angle += 1 + #pygame.draw.rect(win, (255, 0, 0), rotated_rect) + + win.blit(rotated_image, rotated_rect) + self.rotor.draw(win) + diff --git a/player.py b/player.py new file mode 100644 index 0000000..56760ec --- /dev/null +++ b/player.py @@ -0,0 +1,59 @@ +import pygame +from fahrzeug import Fahrzeug + +class Player(Fahrzeug, pygame.sprite.Sprite): + def __init__(self, x, y): + super().__init__(x, y) + 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 = 1 + self.dest_x = x + self.dest_y = y + self.direction = pygame.math.Vector2(0, -1) + self.angle = 0 + self.Tank = 250 + + + def draw(self, win): + rotated_image = pygame.transform.rotate(self.image, self.angle) + rotated_rect = rotated_image.get_rect(center=self.rect.center) + + #debug, nicht anrühren + #self.angle += 1 + #pygame.draw.rect(win, (255, 0, 0), rotated_rect) + + 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 + + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + self.rect.centerx -= self.speed + self.dest_x -= self.speed + self.angle = 90 + if keys[pygame.K_RIGHT]: + self.rect.centerx += self.speed + self.dest_x += self.speed + self.angle = 270 + if keys[pygame.K_UP]: + self.rect.centery -= self.speed + self.dest_y -= self.speed + self.angle = 0 + if keys[pygame.K_DOWN]: + self.rect.centery += self.speed + self.dest_y += self.speed + self.angle = 180 + + def dest(self, pos): + self.dest_x, self.dest_y = pos diff --git a/rotor.py b/rotor.py new file mode 100644 index 0000000..c3b80d7 --- /dev/null +++ b/rotor.py @@ -0,0 +1,32 @@ +import pygame +import helicopter +from fahrzeug import Fahrzeug + +class Rotor(Fahrzeug, pygame.sprite.Sprite): + def __init__(self, x, y): + super().__init__(x, y) + self.image = pygame.image.load("helicopter.jpg").convert_alpha() + self.image = self.image.subsurface(132, 98, 94, 94) + self.rect = self.image.get_rect(center=(x, y)) + + + def update(self, helicopter): + self.rect.center = helicopter.rect.center + if helicopter.angle == 0: + self.rect.centery -= 10 + if helicopter.angle == 90: + self.rect.centerx -= 10 + if helicopter.angle == 180: + self.rect.centery += 10 + if helicopter.angle == 270: + self.rect.centerx += 10 + + def draw(self, win): + rotated_image = pygame.transform.rotate(self.image, self.angle) + rotated_rect = rotated_image.get_rect(center=self.rect.center) + + #debug, nicht anrühren + self.angle += 1 + #pygame.draw.rect(win, (255, 0, 0), rotated_rect) + + win.blit(rotated_image, rotated_rect) \ No newline at end of file