From 398eebe7829622c6983cb28528f2208b4d596f32 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Wed, 20 Mar 2024 16:59:13 +0200 Subject: text to srites. cache scaled font sprites... --- src/sliceitoff/__main__.py | 7 ++++- src/sliceitoff/assets/fonts.lst | 3 ++- src/sliceitoff/display/display.py | 12 ++++++++- src/sliceitoff/display/scaling.py | 32 ++++++++++++++++++++++- src/sliceitoff/display/static.py | 4 +-- src/sliceitoff/field/field.py | 4 +-- src/sliceitoff/images/fonts.py | 2 +- src/sliceitoff/screens/__init__.py | 2 ++ src/sliceitoff/screens/levelup.py | 27 ++++++++++++++++++++ src/sliceitoff/screens/welcome.py | 15 +++++++++++ src/sliceitoff/stats/stats.py | 3 ++- src/sliceitoff/status/status.py | 33 ++++++------------------ src/sliceitoff/text/__init__.py | 1 + src/sliceitoff/text/text.py | 52 ++++++++++++++++++++++++++++++++++++++ 14 files changed, 162 insertions(+), 35 deletions(-) create mode 100644 src/sliceitoff/screens/__init__.py create mode 100644 src/sliceitoff/screens/levelup.py create mode 100644 src/sliceitoff/screens/welcome.py create mode 100644 src/sliceitoff/text/__init__.py create mode 100644 src/sliceitoff/text/text.py diff --git a/src/sliceitoff/__main__.py b/src/sliceitoff/__main__.py index 295bce9..717403e 100644 --- a/src/sliceitoff/__main__.py +++ b/src/sliceitoff/__main__.py @@ -11,6 +11,7 @@ from display import Display from images import Images, Fonts from game import Level from stats import Stats +from screens import Welcome def sliceitoff(): @@ -23,7 +24,11 @@ def sliceitoff(): current_path = Path(__file__).parent.resolve() Images.load_images( current_path ) Fonts.load_fonts( current_path ) - + + welcome = Welcome() + display.update( [welcome.sprites] ) + sleep(2) + clock = pygame.time.Clock() dt = 0 diff --git a/src/sliceitoff/assets/fonts.lst b/src/sliceitoff/assets/fonts.lst index f832c79..f5b4bd3 100644 --- a/src/sliceitoff/assets/fonts.lst +++ b/src/sliceitoff/assets/fonts.lst @@ -1,3 +1,4 @@ lcd assets gnufonts UTIL GNUFONTS LCD.FNT -script assets gnufonts UTIL GNUFONTS SCRIPT.FNT +deco assets gnufonts UTIL GNUFONTS DECO.FNT 8x8 assets gnufonts UTIL GNUFONTS 8X8.FNT +computer assets gnufonts UTIL GNUFONTS COMPUTER.FNT diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index 54b1586..2d6605f 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -1,9 +1,12 @@ +import os import pygame from stats import Stats from .scaling import Scaling +DEBUG = os.getenv("DEBUG") + class Display(): def __init__(self): @@ -14,10 +17,17 @@ class Display(): pygame.FULLSCREEN | pygame.SCALED, vsync = 1 ) Scaling.update_scaling(self.screen.get_size()) + self.screen.fill(Stats.bordercolor) + if DEBUG: + print( + "DISPLAY: \n" + f" {Scaling.active = }\n" + f" {Scaling.border = }\n" + f" {Scaling.factor = }\n") def update(self, groups = None): """ Updates the screen: clear, blit gropus and flip """ - self.screen.fill(Stats.bgcolor) + self.screen.fill(Stats.bgcolor, rect=Scaling.active) for group in groups: group.draw(self.screen) pygame.display.flip() diff --git a/src/sliceitoff/display/scaling.py b/src/sliceitoff/display/scaling.py index af307d1..f40d33c 100644 --- a/src/sliceitoff/display/scaling.py +++ b/src/sliceitoff/display/scaling.py @@ -6,6 +6,8 @@ class Scaling(): factor = 0.02 left = 0 top = 0 + borders = (pygame.Rect(0,0,0,0), pygame.Rect(0,0,0,0)) + active = pygame.Rect(0,0,0,0) @staticmethod def area_to_rect(area: tuple) -> pygame.Rect: @@ -40,8 +42,36 @@ class Scaling(): __class__.factor = size[0] / INTERNAL_WIDTH __class__.left = 0 __class__.top = (size[1] - INTERNAL_HEIGHT * __class__.factor) // 2 + __class__.border = ( + pygame.Rect( + 0, + 0, + size[0], + __class__.top), + pygame.Rect( + 0, + size[1] - __class__.top, + size[0], + __class__.top), + ) else: __class__.factor = size[1] / INTERNAL_HEIGHT __class__.left = (size[0] - INTERNAL_WIDTH * __class__.factor) // 2 __class__.top = 0 - \ No newline at end of file + __class__.border = ( + pygame.Rect( + 0, + 0, + __class__.left, + size[1]), + pygame.Rect( + size[0] - __class__.left, + 0, + __class__.left, + size[1]), + ) + __class__.active = pygame.Rect( + __class__.left, + __class__.top, + INTERNAL_WIDTH * __class__.factor, + INTERNAL_HEIGHT * __class__.factor) diff --git a/src/sliceitoff/display/static.py b/src/sliceitoff/display/static.py index af1cc67..a9f487a 100644 --- a/src/sliceitoff/display/static.py +++ b/src/sliceitoff/display/static.py @@ -1,2 +1,2 @@ -INTERNAL_WIDTH = 400_000 -INTERNAL_HEIGHT = 300_000 +INTERNAL_WIDTH = 320_000 +INTERNAL_HEIGHT = 240_000 diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 161cfcd..5d2a573 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -1,6 +1,6 @@ import os - import pygame + from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT from stats import Stats @@ -20,7 +20,7 @@ class FieldSprite(pygame.sprite.Sprite): class Field(): - initial_area = (400_000, 280_000) + initial_area = (320_000, 220_000) def __init__(self): self.sprites = pygame.sprite.Group() diff --git a/src/sliceitoff/images/fonts.py b/src/sliceitoff/images/fonts.py index 98aa6e6..a1687b6 100644 --- a/src/sliceitoff/images/fonts.py +++ b/src/sliceitoff/images/fonts.py @@ -36,4 +36,4 @@ class Font: self.surfaces.append(surface) def get(self, ch): - return self.surfaces[ord(ch)%256] + return self.surfaces[ch%256] diff --git a/src/sliceitoff/screens/__init__.py b/src/sliceitoff/screens/__init__.py new file mode 100644 index 0000000..a03f76b --- /dev/null +++ b/src/sliceitoff/screens/__init__.py @@ -0,0 +1,2 @@ +#from .levelup import LevelUp +from .welcome import Welcome diff --git a/src/sliceitoff/screens/levelup.py b/src/sliceitoff/screens/levelup.py new file mode 100644 index 0000000..178160c --- /dev/null +++ b/src/sliceitoff/screens/levelup.py @@ -0,0 +1,27 @@ +import os +import pygame + +from display import Scaling +from images import Images, Fonts +from stats import Stats + +class Status(): + def __init__(self): + self.sprites = pygame.sprite.Group() + + def update(self, dt): + """ Update sprites basis of dt. dt = milliseconds from last update """ + + score_str = ( + "{:02d}\x12 {:02d}\xfe {:02d}\x03 " + "{:02d}\x0e {:08d}\x0f").format( + Stats.level, + 99 if Stats.percent == 100 else int(Stats.percent), + Stats.lives, + Stats.bonus // 1000, + Stats.score) + self.sprites = TextGroup( + score_str, + (0, 282_000), + size = 16_000, + font = '8x8') diff --git a/src/sliceitoff/screens/welcome.py b/src/sliceitoff/screens/welcome.py new file mode 100644 index 0000000..ddfd997 --- /dev/null +++ b/src/sliceitoff/screens/welcome.py @@ -0,0 +1,15 @@ +from text import TextPage +from display import Scaling + +class Welcome(): + def __init__(self): + self.sprites = TextPage( + " Slice it off!\n" + "\n" + "* Do not hit the balls\n" + "* Slice off empty areas\n" + "* Slice off empty areas\n", + font = 'computer', + size = (8_000, 16_000), + grid = (9_000, 16_000) ) + \ No newline at end of file diff --git a/src/sliceitoff/stats/stats.py b/src/sliceitoff/stats/stats.py index 7fa39c2..22345d7 100644 --- a/src/sliceitoff/stats/stats.py +++ b/src/sliceitoff/stats/stats.py @@ -4,7 +4,8 @@ class Stats: bonus = 0 lives = 0 percent = 0 - bgcolor = (0,0,0,255) + bgcolor = (64,0,0,255) + bordercolor = (0,0,0,255) @staticmethod def new_game(): diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py index 9b40955..a337a5b 100644 --- a/src/sliceitoff/status/status.py +++ b/src/sliceitoff/status/status.py @@ -4,24 +4,7 @@ import pygame from display import Scaling from images import Images, Fonts from stats import Stats - -class LetterSprite(pygame.sprite.Sprite): - def __init__(self, image, pos): - super().__init__() - self.image = image - self.rect = self.image.get_rect().move(pos) - -class TextGroup(pygame.sprite.Group): - def __init__(self, text, pos, size = 8_000, spacing = None, font = 'lcd'): - super().__init__() - if spacing == None: - spacing = size - for i in range(len(text)): - image = pygame.transform.scale_by( - Fonts.fonts[font].get(text[i]), - size/8 * Scaling.factor) - image_pos = Scaling.scale_to_display( (pos[0]+i*spacing, pos[1]) ) - self.add(LetterSprite(image, image_pos)) +from text import TextPage class Status(): def __init__(self): @@ -31,15 +14,15 @@ class Status(): """ Update sprites basis of dt. dt = milliseconds from last update """ score_str = ( - "{:02d}\x12 {:02d}\xfe {:02d}\x03 " - "{:02d}\x0e {:08d}\x0f").format( + "\x12{:1d} \x03{:1d} \x0e{:02d} \xfe{:02d} " + "\x0f{:08d}").format( Stats.level, - 99 if Stats.percent == 100 else int(Stats.percent), Stats.lives, Stats.bonus // 1000, + 99 if Stats.percent == 100 else int(Stats.percent), Stats.score) - self.sprites = TextGroup( + self.sprites = TextPage( score_str, - (0, 282_000), - size = 16_000, - font = '8x8') + pos = (0, 220_000), + size = (12_000, 0), + font = 'lcd') diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py new file mode 100644 index 0000000..eb17c6e --- /dev/null +++ b/src/sliceitoff/text/__init__.py @@ -0,0 +1 @@ +from .text import TextPage diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py new file mode 100644 index 0000000..f01e517 --- /dev/null +++ b/src/sliceitoff/text/text.py @@ -0,0 +1,52 @@ +import pygame +from random import randrange + +from images import Fonts +from display import Scaling + +scaled_fonts = {} + +class LetterSprite(pygame.sprite.Sprite): + def __init__(self, image, pos): + super().__init__() + self.image = image + self.rect = self.image.get_rect().move(pos) + self.direction = ( + Scaling.factor * 40 * 1_000 + randrange(4_000), + Scaling.factor * 40 * 1_000 + randrange(4_000)) + +class TextPage(pygame.sprite.Group): + def __init__( + self, + text, + pos = (0,0), + size = (8_000,16_000), + grid = None, + font = 'lcd'): + super().__init__() + if grid == None: + grid = size + col, row = 0, 0 + x, y = pos + w, h = grid + for ch_txt in text: + if ch_txt == '\n': + row += 1 + col = 0 + continue + if ch_txt == '\t': + col = (col + 4) % 4 + continue + font_key = (font, w) + ch = ord(ch_txt) + 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) + image = scaled_fonts[font_key][ch] + image_pos = Scaling.scale_to_display( (x+col*w, y+row*h) ) + self.add(LetterSprite(image, image_pos)) + col += 1 + \ No newline at end of file -- cgit v1.2.3