LF08/helicopter.py

76 lines
2.7 KiB
Python
Raw Permalink Normal View History

import pygame
import player
import rotor
2023-04-24 13:50:48 +02:00
from fahrzeug import Fahrzeug, get_angle
import time
2023-04-28 10:30:51 +02:00
class Helicopter(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/helicopter.jpg").convert_alpha()
self.image = self.image.subsurface(44, 0, 44, 99)
2023-04-28 10:30:51 +02:00
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
2023-04-24 13:50:48 +02:00
self.duration_speed = 0.02
self.start_time = time.time()
2023-05-08 22:11:59 +02:00
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
2023-04-24 13:50:48 +02:00
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
2023-04-28 10:30:51 +02:00
self.angle = get_angle((self.rect.centerx, self.rect.centery), (self.dest_x, self.dest_y)) - 90
2023-04-24 13:50:48 +02:00
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)
2023-04-28 10:30:51 +02:00
# debug nicht anrühren
# self.angle += 1
2023-05-08 22:11:59 +02:00
if self.debug:
pygame.draw.rect(win, (0, 255, 0), self.rect)
win.blit(rotated_image, rotated_rect)
2023-04-28 10:30:51 +02:00
self.rotor.draw(win)