From b99e522de33bc97e193a51692db856f586921286 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Mon, 18 Mar 2024 13:47:13 +0200 Subject: gameplay & level as separate class --- src/sliceitoff/__main__.py | 30 ++++-------------------------- src/sliceitoff/game/__init__.py | 3 ++- src/sliceitoff/game/game.py | 30 ------------------------------ src/sliceitoff/game/gameplay.py | 29 +++++++++++++++++++++++++++++ src/sliceitoff/game/level.py | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 57 deletions(-) delete mode 100644 src/sliceitoff/game/game.py create mode 100644 src/sliceitoff/game/gameplay.py create mode 100644 src/sliceitoff/game/level.py diff --git a/src/sliceitoff/__main__.py b/src/sliceitoff/__main__.py index 939bda2..c4366e0 100644 --- a/src/sliceitoff/__main__.py +++ b/src/sliceitoff/__main__.py @@ -8,12 +8,8 @@ from pathlib import Path import pygame from display import Display -from status import Status -from player import Player -from field import Field -from enemies import Enemies from images import Images -from game import Game +from game import Level def sliceitoff(): @@ -23,31 +19,13 @@ def sliceitoff(): display = Display() Images.load_images( Path(__file__).parent.resolve() ) - status = Status() - field = Field() - enemies = Enemies(field = field, level = 10) - player = Player(field = field, enemies = enemies) - game = Game(player = player) + level1 = Level(display = display, level = 100, score = 10) clock = pygame.time.Clock() + dt = 0 - - while True: - + while level1.step(dt): dt = clock.tick() - for fun in (status, field, enemies, player): - fun.update(dt) - - display.update( - [ - status.sprites, - field.sprites, - enemies.sprites, - player.sprites - ]) - - if game.step(): - break pygame.quit() diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py index a2771db..87cb41b 100644 --- a/src/sliceitoff/game/__init__.py +++ b/src/sliceitoff/game/__init__.py @@ -1 +1,2 @@ -from .game import Game +from .gameplay import Gameplay +from .level import Level diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py deleted file mode 100644 index 16c42b4..0000000 --- a/src/sliceitoff/game/game.py +++ /dev/null @@ -1,30 +0,0 @@ -import pygame -from pygame.locals import * - -class Game: - def __init__(self, player = None, field = None): - self.player = player - self.field = field - - def step(self): - for event in pygame.event.get(): - match event.type: - case pygame.KEYDOWN: - match event.key: - case pygame.K_ESCAPE | pygame.K_q: - return True - case pygame.K_SPACE: - if self.player.fire_lazer(): - return True - case pygame.QUIT: - return True - case pygame.MOUSEMOTION: - self.player.set_position(pygame.mouse.get_pos()) - case pygame.MOUSEBUTTONDOWN: - self.player.set_position(pygame.mouse.get_pos()) - if event.button == 1: - if self.player.fire_lazer(): - return True - if event.button == 3: - self.player.set_direction() - return False diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py new file mode 100644 index 0000000..fc0f52b --- /dev/null +++ b/src/sliceitoff/game/gameplay.py @@ -0,0 +1,29 @@ +import pygame + +class Gameplay: + def __init__(self, player = None, field = None): + self.player = player + self.field = field + + def step(self): + for event in pygame.event.get(): + match event.type: + case pygame.KEYDOWN: + match event.key: + case pygame.K_ESCAPE | pygame.K_q: + return True + case pygame.K_SPACE: + if self.player.fire_lazer(): + return True + case pygame.QUIT: + return True + case pygame.MOUSEMOTION: + self.player.set_position(pygame.mouse.get_pos()) + case pygame.MOUSEBUTTONDOWN: + self.player.set_position(pygame.mouse.get_pos()) + if event.button == 1: + if self.player.fire_lazer(): + return True + if event.button == 3: + self.player.set_direction() + return False diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py new file mode 100644 index 0000000..97e64cf --- /dev/null +++ b/src/sliceitoff/game/level.py @@ -0,0 +1,33 @@ +from display import Display +from status import Status +from player import Player +from field import Field +from enemies import Enemies +from game import Gameplay + + +class Level: + """ One level that can be played """ + def __init__(self, level = None, score = None, display = None): + self.display = display + self.status = Status(level = level) + self.field = Field() + self.enemies = Enemies(field = self.field, level = level) + self.player = Player(field = self.field, enemies = self.enemies) + self.gameplay = Gameplay(player = self.player) + self.obj_classes = ( + self.status, + self.field, + self.enemies, + self.player) + + def step(self, dt): + for obj in self.obj_classes: + obj.update(dt) + + self.display.update( (obj.sprites for obj in self.obj_classes) ) + + if self.gameplay.step(): + return False + + return True -- cgit v1.2.3