import os import pygame from display import Scaling from images import Images 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 = (20_000, 14_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 get_sprites(self): if self.updated: self.__update_sprites() return self.sprites def fire_lazer(self): # self.lazer = True # self.updated = True hitbox = self.field.slice(self.position, self.direction, 1_000) if hitbox is not None: for enemy in self.enemies.sprites: if hitbox.colliderect(enemy.rect): return True 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 False def set_direction(self): if not self.lazer: self.direction = not self.direction self.updated = True def __del__(self): pass