diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-23 22:19:00 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-23 22:19:00 +0200 |
commit | 98d91824cacec97c581ae0a4f1efd9fa3faa18ab (patch) | |
tree | 88396d13a6d8449968e305bae443d8f2c5b61cfb /src/sliceitoff/player/player.py | |
parent | 947df85053af2704ce1a17daf1fab13e273f0cc5 (diff) |
player through SingleGroup
Diffstat (limited to 'src/sliceitoff/player/player.py')
-rw-r--r-- | src/sliceitoff/player/player.py | 70 |
1 files changed, 22 insertions, 48 deletions
diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index d36691c..de49083 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -7,58 +7,32 @@ from images import Images DEBUG = os.getenv("DEBUG") class PlayerSprite(pygame.sprite.Sprite): - def __init__(self, image, pos): + def __init__(self, image): 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) + self.rect = self.image.get_rect() + + def update(self, pos = None): + if pos: + w, h = self.image.get_size() + self.rect = self.image.get_rect().move(pos[0]-w//2,pos[1]-h//2) -class Player(): - def __init__(self, field = None, enemies = None): - self.enemies = enemies - self.position = (200_000, 140_000) - self.direction = False +class Player(pygame.sprite.LayeredUpdates): + def __init__(self): + super().__init__() + self.position = (0,0) + self.direction = True self.lazer = False - self.sprites = pygame.sprite.Group() - self.updated = True - self.field = field - - 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") - 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 - return hit + self.add(PlayerSprite(image), layer = 0) + image = pygame.transform.rotate(image, 90) + self.add(PlayerSprite(image), layer = 1) - def set_direction(self): + def update(self, pos = None, direction = False, dt = 0): if not self.lazer: - self.direction = not self.direction - self.updated = True + if direction: + self.direction = not self.direction + self.switch_layer(0,1) + if pos: + super().update(pos = pos) + self.position = Scaling.scale_to_internal(pos) |