LF08/helicopter.py
2023-05-08 22:11:59 +02:00

76 lines
2.7 KiB
Python

import pygame
import player
import rotor
from fahrzeug import Fahrzeug, get_angle
import time
class Helicopter(Fahrzeug, pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__(x, y)
self.image = pygame.image.load("resources/helicopter.jpg").convert_alpha()
self.image = self.image.subsurface(44, 0, 44, 99)
self.image = pygame.transform.scale(self.image, (self.image.get_width() * 1.2, self.image.get_height() * 1.2))
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
self.duration_speed = 0.02
self.start_time = time.time()
self.debug = False
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
elapsed_time = time.time() - self.start_time
if elapsed_time >= self.duration_speed:
if self.rect.centerx < player.rect.centerx:
self.rect.centerx += self.speed
if self.rect.centery < player.rect.centery:
self.rect.centery += self.speed
if self.rect.centerx > player.rect.centerx:
self.rect.centerx -= self.speed
if self.rect.centery > player.rect.centery:
self.rect.centery -= self.speed
self.start_time = time.time()
if self.rect.centerx != self.dest_x or self.rect.centery != self.dest_y:
if self.Tank > 0:
self.Tank -= 0.1
self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y)) - 90
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
if self.debug:
pygame.draw.rect(win, (0, 255, 0), self.rect)
win.blit(rotated_image, rotated_rect)
self.rotor.draw(win)