summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-03-23 22:19:00 +0200
committerViljami Ilola <+@hix.fi>2024-03-23 22:19:00 +0200
commit98d91824cacec97c581ae0a4f1efd9fa3faa18ab (patch)
tree88396d13a6d8449968e305bae443d8f2c5b61cfb /src
parent947df85053af2704ce1a17daf1fab13e273f0cc5 (diff)
player through SingleGroup
Diffstat (limited to 'src')
-rw-r--r--src/sliceitoff/field/field.py1
-rw-r--r--src/sliceitoff/game/gameplay.py19
-rw-r--r--src/sliceitoff/game/level.py19
-rw-r--r--src/sliceitoff/game/show.py2
-rw-r--r--src/sliceitoff/player/player.py70
-rw-r--r--src/sliceitoff/status/status.py26
-rw-r--r--src/sliceitoff/text/text.py8
7 files changed, 69 insertions, 76 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py
index 9e04b4f..4fb4eff 100644
--- a/src/sliceitoff/field/field.py
+++ b/src/sliceitoff/field/field.py
@@ -2,7 +2,6 @@ import os
import pygame
from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT
-
from stats import Stats
DEBUG = os.getenv("DEBUG")
diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py
index e691b84..12daeaf 100644
--- a/src/sliceitoff/game/gameplay.py
+++ b/src/sliceitoff/game/gameplay.py
@@ -17,7 +17,18 @@ class Gameplay:
self.enemies = enemies
def fire(self):
- if self.player.fire_lazer():
+ hitbox = self.field.slice(
+ self.player.position,
+ self.player.direction,
+ 4_500)
+ hit = False
+ if hitbox is not None:
+ for enemy in self.enemies.sprites():
+ if hitbox.colliderect(enemy.rect):
+ hit = True
+ break
+
+ if hit:
if Stats.lose_life():
return True
self.field.kill_if_not_colliding(self.enemies.sprites())
@@ -33,14 +44,14 @@ class Gameplay:
for event in pygame.event.get():
match event.type:
case pygame.MOUSEMOTION:
- self.player.set_position(pygame.mouse.get_pos())
+ self.player.update(pos = pygame.mouse.get_pos())
case pygame.MOUSEBUTTONDOWN:
- self.player.set_position(pygame.mouse.get_pos())
+ self.player.update(pos = pygame.mouse.get_pos())
if event.button == 1:
if self.fire():
return True
if event.button == 3:
- self.player.set_direction()
+ self.player.update(direction = True)
case pygame.KEYDOWN:
match event.key:
case pygame.K_ESCAPE | pygame.K_q:
diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py
index 7c58b86..25c34b9 100644
--- a/src/sliceitoff/game/level.py
+++ b/src/sliceitoff/game/level.py
@@ -1,3 +1,5 @@
+import pygame
+
from display import Display
from status import Status
from player import Player
@@ -13,9 +15,13 @@ class Level:
self.display = display
self.status = Status()
self.field = Field()
- self.enemies = Enemies(field = self.field)
- self.player = Player(field = self.field, enemies = self.enemies)
- self.gameplay = Gameplay(player = self.player, status = self.status, field = self.field, enemies = self.enemies)
+ self.enemies = Enemies()
+ self.player = Player()
+ self.gameplay = Gameplay(
+ player = self.player,
+ status = self.status,
+ field = self.field,
+ enemies = self.enemies)
self.obj_classes = (
self.status,
self.field,
@@ -31,10 +37,13 @@ class Level:
Stats.update_bonus(dt)
self.display.update( (
- self.status.sprites,
+ self.status,
self.field,
self.enemies,
- self.player.sprites) )
+ pygame.sprite.GroupSingle(
+ sprite = self.player.get_top_sprite())))
+
+
if self.gameplay.step():
return False
diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py
index f81c211..c3fe436 100644
--- a/src/sliceitoff/game/show.py
+++ b/src/sliceitoff/game/show.py
@@ -5,7 +5,7 @@ class Show:
""" To show some sprites and quit on any key """
def __init__(self, sprites = []):
self.sprites = sprites
- self.fadeout = 500
+ self.fadeout = 1_000
self.timeout = 5_000
def step(self, dt):
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)
diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py
index a337a5b..cacc84a 100644
--- a/src/sliceitoff/status/status.py
+++ b/src/sliceitoff/status/status.py
@@ -1,18 +1,16 @@
-import os
import pygame
-from display import Scaling
-from images import Images, Fonts
from stats import Stats
from text import TextPage
-class Status():
+class Status(pygame.sprite.Group):
def __init__(self):
- self.sprites = pygame.sprite.Group()
+ super().__init__()
+ self.old_srt = None
- def update(self, dt):
- """ Update sprites basis of dt. dt = milliseconds from last update """
-
+ def update(self, **kwargs):
+ """ Rebuilds statusline if needed """
+ super().update(**kwargs)
score_str = (
"\x12{:1d} \x03{:1d} \x0e{:02d} \xfe{:02d} "
"\x0f{:08d}").format(
@@ -21,8 +19,10 @@ class Status():
Stats.bonus // 1000,
99 if Stats.percent == 100 else int(Stats.percent),
Stats.score)
- self.sprites = TextPage(
- score_str,
- pos = (0, 220_000),
- size = (12_000, 0),
- font = 'lcd')
+ if self.old_srt != score_str:
+ self.empty()
+ self.add( TextPage(
+ score_str,
+ pos = (0, 220_000),
+ size = (12_000, 0),
+ font = 'lcd') )
diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py
index e8ccb57..9bb7c9a 100644
--- a/src/sliceitoff/text/text.py
+++ b/src/sliceitoff/text/text.py
@@ -15,16 +15,16 @@ class LetterSprite(pygame.sprite.Sprite):
Scaling.factor * (1_000 - randrange(2_000)),
Scaling.factor * (1_000 - randrange(2_000)))
- def update(self, explode=0):
+ def update(self, explode = 0, **kwargs):
if explode:
self.rect = pygame.Rect(
self.rect.x + self.direction[0] * explode,
self.rect.y + self.direction[1] * explode,
self.rect.w,
self.rect.h)
- self.direction = (
- self.direction[0] * 0.8,
- self.direction[1] * 0.8 + 1)
+ self.direction = (
+ self.direction[0] * 1,
+ self.direction[1] * 1 + 0.5)
class TextPage(pygame.sprite.Group):