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 /src/sliceitoff/text | |
parent | d2e42bfa99d8bb417d8eaf365d290a071ad49b7f (diff) |
exploding sprites and groups as their own. more refactoring.
Diffstat (limited to 'src/sliceitoff/text')
-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 |
3 files changed, 30 insertions, 18 deletions
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 |