From 607c00bc57fb68572754404cb5da0e7f7a5618e5 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sat, 30 Mar 2024 12:07:49 +0200 Subject: refactor enemies to add bounchers --- src/sliceitoff/text/text.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'src/sliceitoff/text/text.py') diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index 83367e0..69a30bc 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -8,28 +8,35 @@ from .fonts import Fonts scaled_fonts = {} -class LetterSprite(pygame.sprite.Sprite): - """ Single letter at given properties hopefully from cache +def get_letter_surface(font_key, ch): + """ Get letter surface at given properties hopefully from cache args: font_key: (font name, width to scale, color) ch: 0-255 character on cp473 color: 0-15 as in CGA palette """ + font, w, color = font_key + if font not in Fonts.fonts: + return None + if font_key not in scaled_fonts: + scaled_fonts[font_key]=[None for _ in range(256)] + if scaled_fonts[font_key][ch] is None: + 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) + return scaled_fonts[font_key][ch] + + +class LetterSprite(pygame.sprite.Sprite): + """ Make sprite out of letter surface at given position """ def __init__(self, font_key, ch, pos): super().__init__() 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] is 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.image = get_letter_surface(font_key, ch) self.rect = self.image.get_rect().move(pos) self.direction = ( Scaling.factor * (1_000 - randrange(2_000)), -- cgit v1.2.3