diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-07 23:25:09 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-07 23:25:09 +0300 |
commit | 6ba20c61a04c688120161f6c5fbb56102f8082ef (patch) | |
tree | 33291a2c494b8fe5e126e31b5ce719aa0df0feec | |
parent | d2e42bfa99d8bb417d8eaf365d290a071ad49b7f (diff) |
exploding sprites and groups as their own. more refactoring.
-rw-r--r-- | pyproject.toml | 17 | ||||
-rw-r--r-- | src/sliceitoff/display/static.py | 29 | ||||
-rw-r--r-- | src/sliceitoff/game/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/game/explodeout.py | 34 | ||||
-rw-r--r-- | src/sliceitoff/game/initials.py | 25 | ||||
-rw-r--r-- | src/sliceitoff/game/show.py | 29 | ||||
-rw-r--r-- | src/sliceitoff/mainmenu/mainmenu.py | 77 | ||||
-rw-r--r-- | src/sliceitoff/text/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/text/explode.py | 27 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 20 |
10 files changed, 144 insertions, 116 deletions
diff --git a/pyproject.toml b/pyproject.toml index ea19274..7d8cf50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sliceitoff" -version = "0.2" +version = "0.3-alpha" description = "Arcade game where one slices play area off avoiding enemies." authors = ["Viljami Ilola <+@hix.fi>"] readme = "README.md" @@ -23,15 +23,24 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] sliceitoff = 'sliceitoff.main:main' + + [tool.pytest.ini_options] pythonpath = [ "src/sliceitoff" ] + + [tool.pylint.main] recursive = true source-roots = ["src/sliceitoff"] extension-pkg-whitelist = ["pygame"] [tool.pylint."messages control"] -disable = [ - "too-few-public-methods" # pygame sprites and groups -] +disable = [ "too-few-public-methods" ] # pygame sprites and groups + +# From given .pylintrc +[tool.pylint.design] +max-args = 7 +max-attributes = 8 +max-statements = 20 +min-public-methods = 0 diff --git a/src/sliceitoff/display/static.py b/src/sliceitoff/display/static.py index 8791fb0..054e6b8 100644 --- a/src/sliceitoff/display/static.py +++ b/src/sliceitoff/display/static.py @@ -3,21 +3,22 @@ INTERNAL_WIDTH = 320_000 INTERNAL_HEIGHT = 240_000 +# https://forum.vcfed.org/index.php?threads/representing-ibm-5153-color-output-more-accurately.1234458/ CGA_COLORS=[ (0x00,0x00,0x00,0xFF), - (0x00,0x00,0xAA,0xFF), - (0x00,0xAA,0x00,0xFF), - (0x00,0xAA,0xAA,0xFF), - (0xAA,0x00,0x00,0xFF), - (0xAA,0x00,0xAA,0xFF), - (0xAA,0x55,0x00,0xFF), - (0xAA,0xAA,0xAA,0xFF), - (0x55,0x55,0x55,0xFF), - (0x55,0x55,0xFF,0xFF), - (0x55,0xFF,0x55,0xFF), - (0x55,0xFF,0xFF,0xFF), - (0xFF,0x55,0x55,0xFF), - (0xFF,0x55,0xFF,0xFF), - (0xFF,0xFF,0x55,0xFF), + (0x00,0x00,0xC4,0xFF), + (0x00,0xC4,0x00,0xFF), + (0x00,0xC4,0xC4,0xFF), + (0xC4,0x00,0x00,0xFF), + (0xC4,0x00,0xC4,0xFF), + (0xC4,0x7E,0x00,0xFF), + (0xC4,0xC4,0xC4,0xFF), + (0x4E,0x4E,0x4E,0xFF), + (0x4E,0x4E,0xDC,0xFF), + (0x4E,0xDC,0x4E,0xFF), + (0x4E,0xF3,0xF3,0xFF), + (0xDC,0x4E,0x4E,0xFF), + (0xF3,0x4E,0xF3,0xFF), + (0xF3,0xF3,0x4E,0xFF), (0xFF,0xFF,0xFF,0xFF) ] diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py index 7785623..221acd3 100644 --- a/src/sliceitoff/game/__init__.py +++ b/src/sliceitoff/game/__init__.py @@ -1,2 +1,3 @@ """ game - All the game logic and user inputs """ from .game import Game +from .explodeout import ExplodeOutGroup diff --git a/src/sliceitoff/game/explodeout.py b/src/sliceitoff/game/explodeout.py new file mode 100644 index 0000000..34bd9fe --- /dev/null +++ b/src/sliceitoff/game/explodeout.py @@ -0,0 +1,34 @@ +""" game.explodeout - For showing explogind effect and waiting for a key """ +import pygame + +from .anykey import anykey + +class ExplodeOutGroup(pygame.sprite.Group): + """ Sprite group that just counts down feadeout/explosion or a key """ + def __init__(self, active = True): + super().__init__() + self.explode = False + self.active = active + self.fadeout = 1_000 + + def update(self, dt = 0, **kwargs): + """ Just does the explosion and marks group inactive """ + if not self.active: + return False + + super().update(dt = dt, explode = self.explode, **kwargs) + + if self.explode: + if self.fadeout <= 0: + self.active = False + else: + if anykey(): + self.fadeout = 0 + self.active = False + self.fadeout -= dt + return True + return True + + def do_fadeout(self): + """ Just kicks off exploding phase """ + self.explode = True diff --git a/src/sliceitoff/game/initials.py b/src/sliceitoff/game/initials.py index 7811afa..9fb5a68 100644 --- a/src/sliceitoff/game/initials.py +++ b/src/sliceitoff/game/initials.py @@ -3,45 +3,30 @@ import pygame from screens import initials_screen -from .anykey import anykey +from .explodeout import ExplodeOutGroup -class Initials(pygame.sprite.Group): +class Initials(ExplodeOutGroup): """ Sprite group that asks initials to self.name from user """ def __init__(self): super().__init__() self.add(initials_screen("")) self.name = "" - self.explode = False - self.active = True - self.fadeout = 1_000 def update(self, dt = 0, **kwargs): """ Does it all. Reads keyboard and updates screen """ - if not self.active: - return - - super().update(dt = dt, explode = self.explode, **kwargs) - - if self.explode: - if self.fadeout <= 0: - self.active = False - else: - if anykey(): - self.fadeout = 0 - self.active = False - self.fadeout -= dt + if not super().update(dt = dt, **kwargs): return for event in pygame.event.get(): if event.type == pygame.QUIT: - self.explode = True + self.do_fadeout() break if event.type == pygame.KEYDOWN: if event.key in ( pygame.K_ESCAPE, pygame.K_KP_ENTER, pygame.K_RETURN): - self.explode = True + self.do_fadeout() break if event.key in ( pygame.K_RSHIFT, diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index 0fb6e05..eaf3658 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -1,32 +1,23 @@ """ Sprite group that show sprites and skips if key is pressed """ -import pygame from .anykey import anykey +from .explodeout import ExplodeOutGroup -class Show(pygame.sprite.Group): +class Show(ExplodeOutGroup): """ To show some sprites and quit on any key """ def __init__(self, sprites = None, active = True): - super().__init__() + super().__init__(active = active) self.add(sprites) - self.active = active - self.explode = False - self.fadeout = 1_000 self.timeout = 15_000 def update(self, dt = 0, **kwargs): """ First timeout then fadeout and then inactivity """ - if self.fadeout <= 0: - self.active = False - elif self.timeout <= 0: - if anykey(): - self.fadeout = 0 - self.fadeout -= dt - self.explode = True - else: - if anykey(): - self.timeout = 0 - self.timeout -= dt - for sprite in self.sprites(): - sprite.update(dt = dt, explode = self.explode, **kwargs) + if not super().update(dt = dt, **kwargs): + return + if anykey(): + self.do_fadeout() + if self.timeout <= 0: + self.do_fadeout() + self.timeout -= dt def sprites(self): """ Return sprites only when active """ diff --git a/src/sliceitoff/mainmenu/mainmenu.py b/src/sliceitoff/mainmenu/mainmenu.py index 551e7b5..7d52cd8 100644 --- a/src/sliceitoff/mainmenu/mainmenu.py +++ b/src/sliceitoff/mainmenu/mainmenu.py @@ -5,7 +5,7 @@ import pygame from screens import mainmenu_screen from display import Scaling -from game.anykey import anykey +from game.explodeout import ExplodeOutGroup MOUSE_TRESHOLD = 100 @@ -16,63 +16,58 @@ class MenuItems(IntEnum): INSTRUCT = 2 QUIT = 3 -class Mainmenu(pygame.sprite.Group): +class Mainmenu(ExplodeOutGroup): """ sprite group with imputs to make selection """ def __init__(self): super().__init__() self.add(mainmenu_screen(0)) - self.explode = False - self.active = True - self.fadeout = 1_000 self.selection = 0 self.mousey = 0 - def update(self, dt = 0): + def update(self, dt = 0, **kwargs): """ Does it all. Reads keyboard and updates screen """ - if not self.active: - return - - if self.explode: - for sprite in self.sprites(): - sprite.update(dt = dt, explode = self.explode) - if self.fadeout <= 0: - self.active = False - else: - if anykey(): - self.fadeout = 0 - self.active = False - self.fadeout -= dt + if not super().update(dt = dt, **kwargs) or self.explode: return for event in pygame.event.get(): if event.type == pygame.QUIT: self.selection = MenuItems.QUIT - self.explode = True + self.do_fadeout() break if event.type == pygame.MOUSEBUTTONDOWN and event.button <= 3: - self.explode = True + self.do_fadeout() break if event.type == pygame.KEYDOWN: - match event.key: - case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: - self.explode = True - break - case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: - self.selection = MenuItems.QUIT - self.explode = True - break - case pygame.K_UP: - self.selection -= 1 - self.selection %= len(MenuItems) - case pygame.K_DOWN: - self.selection += 1 - self.selection %= len(MenuItems) + if self.process_key(event.key): + break elif event.type == pygame.MOUSEMOTION: - self.mousey += pygame.mouse.get_rel()[1] - pygame.mouse.set_pos(Scaling.center) - if abs(self.mousey) > MOUSE_TRESHOLD: - self.selection += 1 if self.mousey > 0 else -1 - self.selection %= len(MenuItems) - self.mousey = 0 + self.process_mouse_motion() self.empty() self.add(mainmenu_screen(self.selection)) + + def process_mouse_motion(self): + """ Mouse movement up or down moves menu selection """ + self.mousey += pygame.mouse.get_rel()[1] + pygame.mouse.set_pos(Scaling.center) + if abs(self.mousey) > MOUSE_TRESHOLD: + self.selection += 1 if self.mousey > 0 else -1 + self.selection %= len(MenuItems) + self.mousey = 0 + + def process_key(self, key): + """ Processes known key presses """ + match key: + case pygame.K_KP_ENTER | pygame.K_RETURN | pygame.K_RIGHT: + self.do_fadeout() + return True + case pygame.K_ESCAPE | pygame.K_q | pygame.K_LEFT: + self.selection = MenuItems.QUIT + self.do_fadeout() + return True + case pygame.K_UP: + self.selection -= 1 + self.selection %= len(MenuItems) + case pygame.K_DOWN: + self.selection += 1 + self.selection %= len(MenuItems) + return False diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index 59385be..aed5723 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1,3 +1,4 @@ """ text - fonts, letters, texts and related """ from .text import TextPage, LetterSprite, get_letter_surface from .fonts import Font, Fonts +from .explode import ExplodingSprite diff --git a/src/sliceitoff/text/explode.py b/src/sliceitoff/text/explode.py new file mode 100644 index 0000000..4925398 --- /dev/null +++ b/src/sliceitoff/text/explode.py @@ -0,0 +1,27 @@ +""" text.explode - Exploding effect movements and updates for the sprite """ +from random import randrange + +import pygame + +from display import Scaling + +class ExplodingSprite(pygame.sprite.Sprite): + """ Just adds exloding movement to the sprite """ + def __init__(self): + super().__init__() + self.rect = None + self.direction = ( + Scaling.factor * (1_000 - randrange(2_000)), + Scaling.factor * (1_000 - randrange(2_000))) + + def update(self, dt = 0, explode = 0): + """ Exploding movement """ + if explode and dt: + self.rect = pygame.Rect( + self.rect.x + self.direction[0] * dt, + self.rect.y + self.direction[1] * dt, + self.rect.w, + self.rect.h) + self.direction = ( + self.direction[0] * 0.95, + self.direction[1] * 0.95 + 0.3) diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index c429110..69fee77 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -1,10 +1,9 @@ """ text.text - letters, texts and scaling and coloring of fonts """ -from random import randrange - import pygame from display import Scaling, CGA_COLORS from .fonts import Fonts +from .explode import ExplodingSprite scaled_fonts = {} @@ -31,28 +30,13 @@ def get_letter_surface(font_key, ch): return scaled_fonts[font_key][ch] -class LetterSprite(pygame.sprite.Sprite): +class LetterSprite(ExplodingSprite): """ Make sprite out of letter surface at given position """ def __init__(self, font_key, ch, pos): super().__init__() self.dead = True self.image = get_letter_surface(font_key, ch) self.rect = self.image.get_rect().move(pos) - self.direction = ( - Scaling.factor * (1_000 - randrange(2_000)), - Scaling.factor * (1_000 - randrange(2_000))) - - def update(self, dt = 0, explode = 0): - """ All the movements for letters """ - if explode and dt: - self.rect = pygame.Rect( - self.rect.x + self.direction[0] * dt, - self.rect.y + self.direction[1] * dt, - self.rect.w, - self.rect.h) - self.direction = ( - self.direction[0] * 0.95, - self.direction[1] * 0.95 + 0.3) class TextPage(pygame.sprite.Group): """ Creates sprite group out of given text and parameters |