import os import pygame from display import Scaling from images import Images DEBUG = os.getenv("DEBUG") class PlayerSprite(pygame.sprite.Sprite): def __init__(self, image, pos): super().__init__() self.image = image x, y = self.image.get_size() x = pos[0] - x // 2 y = pos[1] - y // 2 self.rect = self.image.get_rect().move(x,y) class Player(): def __init__(self, field = None, enemies = None): self.field = field self.enemies = enemies self.position = (200_000, 140_000) self.direction = False self.lazer = False self.sprites = pygame.sprite.Group() self.updated = True def __update_sprites(self): self.sprites.empty() image = Images.surfaces['player_00'] if self.direction: image = pygame.transform.rotate(image, 90) self.sprites.add(PlayerSprite( image, Scaling.scale_to_display(self.position) )) if not self.lazer: self.updated = False def set_position(self, pos): if not self.lazer: self.position = Scaling.scale_to_internal(pos) self.updated = True def update(self, dt): """ Update sprites basis of dt. dt = milliseconds from last update """ if self.updated: self.__update_sprites() def fire_lazer(self): if DEBUG: print("PLAYER: fire lazer") # self.lazer = True # self.updated = True hitbox = self.field.slice(self.position, self.direction, 4_500) hit = False if hitbox is not None: for enemy in self.enemies.sprites: if hitbox.colliderect(enemy.rect): hit = True break for field in self.field.sprites: for enemy in self.enemies.sprites: if enemy.rect.colliderect(field.rect): break else: field.remove(self.field.sprites) return hit def set_direction(self): if not self.lazer: self.direction = not self.direction self.updated = True