From 27c7e16e4bd808ce1176f18b7a78c02ff4fa88ee Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Thu, 28 Mar 2024 23:34:19 +0200 Subject: fonts under text, more sprite.Groups --- src/sliceitoff/display/__init__.py | 2 +- src/sliceitoff/display/colors.py | 2 +- src/sliceitoff/display/display.py | 3 +-- src/sliceitoff/enemies/enemies.py | 2 +- src/sliceitoff/game/anykey.py | 2 ++ src/sliceitoff/game/game.py | 6 +++--- src/sliceitoff/game/initials.py | 36 +++++++++++++++++++++++++++-------- src/sliceitoff/game/level.py | 20 ++++++++----------- src/sliceitoff/game/show.py | 6 ++++-- src/sliceitoff/images/__init__.py | 2 -- src/sliceitoff/images/fonts.py | 39 -------------------------------------- src/sliceitoff/images/images.py | 32 ------------------------------- src/sliceitoff/player/player.py | 2 +- src/sliceitoff/text/__init__.py | 1 + src/sliceitoff/text/fonts.py | 39 ++++++++++++++++++++++++++++++++++++++ src/sliceitoff/text/text.py | 6 +++--- 16 files changed, 93 insertions(+), 107 deletions(-) delete mode 100644 src/sliceitoff/images/__init__.py delete mode 100644 src/sliceitoff/images/fonts.py delete mode 100644 src/sliceitoff/images/images.py create mode 100644 src/sliceitoff/text/fonts.py diff --git a/src/sliceitoff/display/__init__.py b/src/sliceitoff/display/__init__.py index 42c1b12..92f4d75 100644 --- a/src/sliceitoff/display/__init__.py +++ b/src/sliceitoff/display/__init__.py @@ -1,4 +1,4 @@ from .scaling import Scaling from .display import Display from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT -from .colors import EGA_COLORS +from .colors import CGA_COLORS diff --git a/src/sliceitoff/display/colors.py b/src/sliceitoff/display/colors.py index 18ccfc9..f8747c0 100644 --- a/src/sliceitoff/display/colors.py +++ b/src/sliceitoff/display/colors.py @@ -1,6 +1,6 @@ """ text.colors - colors are defined here """ -EGA_COLORS=[ +CGA_COLORS=[ (0x00,0x00,0x00,0xFF), (0x00,0x00,0xAA,0xFF), (0x00,0xAA,0x00,0xFF), diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index c2a3e44..2febdc2 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -30,8 +30,7 @@ class Display(): self.screen.fill(Stats.bgcolor, rect=Scaling.active) #(g.draw(self.screen) for g in groups if g) for group in groups: - if group: - group.draw(self.screen) + group.draw(self.screen) self.screen.fill(Stats.bordercolor, rect=Scaling.borders[0]) self.screen.fill(Stats.bordercolor, rect=Scaling.borders[1]) pygame.display.flip() diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index b1c415d..658c1aa 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -2,7 +2,7 @@ import pygame from random import randrange from display import Scaling -from images import Fonts +from text import Fonts from stats import Stats class EnemySprite(pygame.sprite.Sprite): diff --git a/src/sliceitoff/game/anykey.py b/src/sliceitoff/game/anykey.py index 6fd48a2..bd2a3c7 100644 --- a/src/sliceitoff/game/anykey.py +++ b/src/sliceitoff/game/anykey.py @@ -1,6 +1,8 @@ +""" game.anykey - Event waiting. Used for skipping screens. """ import pygame def anykey(): + """ Anything but mouse movement gives True """ for event in pygame.event.get(): if event.type in (pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN, pygame.QUIT): return True diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 982466b..146e34e 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -7,7 +7,7 @@ from pathlib import Path import pygame from display import Display -from images import Images, Fonts +from text import Fonts from stats import Stats from screens import welcome_screen, hiscores_screen from hiscores import HiScores @@ -56,8 +56,8 @@ class Game: def initials(self): initials = Initials() dt = 0 - while initials.step(): - dt = self.clock.tick() + while initials.active: + initials.update(dt = self.clock.tick()) self.display.update([initials]) return initials.name diff --git a/src/sliceitoff/game/initials.py b/src/sliceitoff/game/initials.py index a317dd9..49db3d6 100644 --- a/src/sliceitoff/game/initials.py +++ b/src/sliceitoff/game/initials.py @@ -1,26 +1,47 @@ +""" game.initials - Use will be asked for initials """ import pygame from screens import initials_screen +from .anykey import anykey + class Initials(pygame.sprite.Group): + """ 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, **kwargs): - super().update(**kwargs) + def update(self, dt = 0, **kwargs): + 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 + return - def step(self): for event in pygame.event.get(): if event.type == pygame.QUIT: - return False + self.explode = True + break if event.type == pygame.KEYDOWN: if event.key in ( pygame.K_ESCAPE, pygame.K_KP_ENTER, pygame.K_RETURN): - return False + self.explode = True + break if event.key in ( pygame.K_RSHIFT, pygame.K_LSHIFT, @@ -39,6 +60,5 @@ class Initials(pygame.sprite.Group): elif pygame.key.name(event.key): self.name += pygame.key.name(event.key)[0].upper() self.name = self.name[:3] - self.empty() - self.add(initials_screen(self.name)) - return True + self.empty() + self.add(initials_screen(self.name)) diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index f0bb871..ada29d2 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -16,16 +16,15 @@ class Level: """ One level that can be played """ def __init__(self, display = None): self.display = display - self.ended = False self.status = Status() self.field = Field() self.enemies = Enemies() self.player = Player() self.player_single = pygame.sprite.Group() - self.level_info = Show(level_screen()) - self.game_over = Show(gameover_screen(), active = False) - self.levelup = Show(levelup_screen(), active = False) + + self.ended = False + self.endscreen = pygame.sprite.Group() self.gameplay = Gameplay( player = self.player, @@ -36,8 +35,6 @@ class Level: self.status, self.field, self.enemies, - self.game_over, - self.levelup, self.level_info] def step(self, dt): @@ -50,10 +47,8 @@ class Level: self.level_info.update(dt = dt) elif self.ended: - if self.game_over.active: - self.game_over.update(dt = dt) - elif self.levelup.active: - self.levelup.update(dt = dt) + if self.endscreen.active: + self.endscreen.update(dt = dt) else: return False @@ -65,9 +60,10 @@ class Level: self.player_single = pygame.sprite.Group() self.ended = True if Stats.lives: - self.levelup.active = True + self.endscreen = Show(levelup_screen()) else: - self.game_over.active = True + self.endscreen = Show(gameover_screen()) + self.obj_classes.append(self.endscreen) self.display.update(self.obj_classes+[self.player_single]) diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index 018e555..b9b684b 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -1,4 +1,4 @@ -""" Reads user input and does actions when game play is on. """ +""" Sprite group that show sprites and skips if key is pressed """ import pygame from .anykey import anykey @@ -13,6 +13,7 @@ class Show(pygame.sprite.Group): self.timeout = 15_000 def update(self, dt = 0, **kwargs): + """ First timeout then fadeout and then inactivity """ super().update(dt = dt, explode = self.explode, **kwargs) if self.fadeout <= 0: self.active = False @@ -27,4 +28,5 @@ class Show(pygame.sprite.Group): self.timeout -= dt def sprites(self): - return super().sprites() if self.active else None \ No newline at end of file + """ Return sprites only when active """ + return super().sprites() if self.active else [] \ No newline at end of file diff --git a/src/sliceitoff/images/__init__.py b/src/sliceitoff/images/__init__.py deleted file mode 100644 index c11d718..0000000 --- a/src/sliceitoff/images/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .images import Images -from .fonts import Fonts, Font diff --git a/src/sliceitoff/images/fonts.py b/src/sliceitoff/images/fonts.py deleted file mode 100644 index a1687b6..0000000 --- a/src/sliceitoff/images/fonts.py +++ /dev/null @@ -1,39 +0,0 @@ -import os -import pygame - -DEBUG = os.getenv("DEBUG") - -class Fonts: - fonts = {} - - @staticmethod - def load_fonts(base_path): - filename_fontlist = os.path.join(base_path, "assets", "fonts.lst") - with open( filename_fontlist ) as fontlist_file: - for line in fontlist_file: - name, *path = line.strip().split() - filename = os.path.join(base_path, *path) - __class__.fonts[name] = Font(filename) - return True - - -class Font: - def __init__(self, filename, height = 16): - if DEBUG: - print(f"Loading font {filename = }") - self.surfaces = [] - with open(filename, mode="rb") as fnt_file: - for _ in range(256): - surface = pygame.Surface((8,height), pygame.SRCALPHA) - for line in range(16): - byte = fnt_file.read(1)[0] - if line >= height: - continue - for bit in range(8): - if byte & 0x80: - surface.set_at((bit,line),"white") - byte <<= 1 - self.surfaces.append(surface) - - def get(self, ch): - return self.surfaces[ch%256] diff --git a/src/sliceitoff/images/images.py b/src/sliceitoff/images/images.py deleted file mode 100644 index 7ae1f6d..0000000 --- a/src/sliceitoff/images/images.py +++ /dev/null @@ -1,32 +0,0 @@ -import os -import pygame - -from display import Scaling - -DEBUG = os.getenv("DEBUG") - -class Images: - surfaces = {} - - @staticmethod - def reload_images(base_path): - __class__.surfaces = {} - filename_imagelist = os.path.join(base_path, "assets", "images.lst") - with open( filename_imagelist ) as imagelist_file: - for line in imagelist_file: - name, *path = line.strip().split() - filename = os.path.join(base_path, *path) - if DEBUG: - print(f"Loading images {name = }, {filename = }") - image = pygame.image.load(filename) - rgba = pygame.Surface.convert_alpha(image) - scaled = pygame.transform.smoothscale_by(rgba, Scaling.factor*10) - __class__.surfaces[name] = scaled - return True - - @staticmethod - def load_images(base_path): - if __class__.surfaces: - return False - return __class__.reload_images(base_path) - \ No newline at end of file diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index 9235dc3..002594f 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -4,7 +4,7 @@ import pygame from random import randrange from display import Scaling -from images import Fonts +from text import Fonts DEBUG = os.getenv("DEBUG") diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index eb17c6e..e37e2e0 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1 +1,2 @@ from .text import TextPage +from .fonts import Font, Fonts diff --git a/src/sliceitoff/text/fonts.py b/src/sliceitoff/text/fonts.py new file mode 100644 index 0000000..a1687b6 --- /dev/null +++ b/src/sliceitoff/text/fonts.py @@ -0,0 +1,39 @@ +import os +import pygame + +DEBUG = os.getenv("DEBUG") + +class Fonts: + fonts = {} + + @staticmethod + def load_fonts(base_path): + filename_fontlist = os.path.join(base_path, "assets", "fonts.lst") + with open( filename_fontlist ) as fontlist_file: + for line in fontlist_file: + name, *path = line.strip().split() + filename = os.path.join(base_path, *path) + __class__.fonts[name] = Font(filename) + return True + + +class Font: + def __init__(self, filename, height = 16): + if DEBUG: + print(f"Loading font {filename = }") + self.surfaces = [] + with open(filename, mode="rb") as fnt_file: + for _ in range(256): + surface = pygame.Surface((8,height), pygame.SRCALPHA) + for line in range(16): + byte = fnt_file.read(1)[0] + if line >= height: + continue + for bit in range(8): + if byte & 0x80: + surface.set_at((bit,line),"white") + byte <<= 1 + self.surfaces.append(surface) + + def get(self, ch): + return self.surfaces[ch%256] diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index da48f6c..dd9b719 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -1,8 +1,8 @@ import pygame from random import randrange -from images import Fonts -from display import Scaling, EGA_COLORS +from .fonts import Fonts +from display import Scaling, CGA_COLORS scaled_fonts = {} @@ -62,7 +62,7 @@ class TextPage(pygame.sprite.Group): Fonts.fonts[font].get(ch), size[0]/8 * Scaling.factor) scaled_fonts[font_key][ch].fill( - EGA_COLORS[color], + 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) ) -- cgit v1.2.3