diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-29 13:52:24 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-29 13:52:24 +0200 |
commit | 063628a3c493df902af9cef4f56f726a9d4ee906 (patch) | |
tree | 3a9b95f1060cc5d4302b6ff86d2ddf9901dd361e | |
parent | 4e88912ff032f1df77480ba37c82fb7049d1ff1b (diff) |
refactor lettersrite and textpage; field explodes to letters
-rw-r--r-- | src/sliceitoff/field/field.py | 36 | ||||
-rw-r--r-- | src/sliceitoff/text/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 31 |
3 files changed, 27 insertions, 42 deletions
diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 5769bfc..6baf966 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -3,6 +3,7 @@ import pygame from random import randrange from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT +from text import LetterSprite DEBUG = os.getenv("DEBUG") @@ -24,7 +25,7 @@ class SliceSprite(FieldSprite): self.timeout = 300 self.dead = True - def update(self, dt = 0): + def update(self, dt = 0, **kwargs): if dt: self.timeout -= dt self.color = ( @@ -37,30 +38,6 @@ class SliceSprite(FieldSprite): else: self.image.fill(self.color) -class ExplodedField(FieldSprite): - def __init__(self, area: tuple): - super().__init__(area) - self.dead = True - self.color = (255,255,255,255) - self.image.fill(self.color) - self.direction = ( - Scaling.factor * (500 - randrange(1000)), - Scaling.factor * (500 - randrange(1000))) - - def update(self, dt = 0, **kwargs): - if 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) - if (self.rect.x > Scaling.resolution[0] - or self.rect.y > Scaling.resolution[1]): - self.kill() - class Field(pygame.sprite.LayeredUpdates): initial_area = (320_000, 220_000) @@ -74,6 +51,9 @@ class Field(pygame.sprite.LayeredUpdates): def calculate_current_area(self): return sum( s.area[2]*s.area[3] for s in self.active_sprites() ) + def update(self, **kwargs): + super().update(explode = True, **kwargs) + def update_stats(self): """ calculates remaining area and remaining percentage """ self.stats.percent = 100 * self.calculate_current_area() / self.area_full @@ -139,7 +119,11 @@ class Field(pygame.sprite.LayeredUpdates): sx, sy, w, h = area for x in range(int(sx),int(sx+w),8_000): for y in range(int(sy),int(sy+h),8_000): - self.add(ExplodedField((x,y,4_000,4_000))) + #self.add(ExplodedField((x,y,4_000,4_000))) + self.add(LetterSprite( + ('8x8', 8_000, 0xf), + randrange(0,0x100), + Scaling.scale_to_display((x,y)) )) def kill_if_not_colliding(self, sprites): diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index e37e2e0..6392649 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1,2 +1,2 @@ -from .text import TextPage +from .text import TextPage, LetterSprite from .fonts import Font, Fonts diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index dd9b719..198ff4f 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -7,9 +7,20 @@ from display import Scaling, CGA_COLORS scaled_fonts = {} class LetterSprite(pygame.sprite.Sprite): - def __init__(self, image, pos): + def __init__(self, font_key, ch, pos): super().__init__() - self.image = image + self.dead = True + if font_key not in scaled_fonts: + scaled_fonts[font_key]=[None for _ in range(256)] + if scaled_fonts[font_key][ch] == None: + font, w, color = font_key + scaled_fonts[font_key][ch] = pygame.transform.scale_by( + Fonts.fonts[font].get(ch), + w/8 * Scaling.factor) + scaled_fonts[font_key][ch].fill( + CGA_COLORS[color], + special_flags = pygame.BLEND_RGBA_MULT) + self.image = scaled_fonts[font_key][ch] self.rect = self.image.get_rect().move(pos) self.direction = ( Scaling.factor * (1_000 - randrange(2_000)), @@ -54,18 +65,8 @@ class TextPage(pygame.sprite.Group): if ch in range(0xe0,0xf0): color = ch - 0xe0 continue - font_key = (font, w, color) - if font_key not in scaled_fonts: - scaled_fonts[font_key]=[None for _ in range(256)] - if scaled_fonts[font_key][ch] == None: - scaled_fonts[font_key][ch] = pygame.transform.scale_by( - Fonts.fonts[font].get(ch), - size[0]/8 * Scaling.factor) - scaled_fonts[font_key][ch].fill( - CGA_COLORS[color], - special_flags = pygame.BLEND_RGBA_MULT) - image = scaled_fonts[font_key][ch] - image_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) - self.add(LetterSprite(image, image_pos)) + font_key = (font, size[0], color) + sprite_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) + self.add(LetterSprite(font_key, ch, sprite_pos)) col += 1
\ No newline at end of file |