diff options
author | Viljami Ilola <+@hix.fi> | 2024-04-21 00:03:22 +0300 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-04-21 00:03:22 +0300 |
commit | 9f808fa0e43ae903d6159848d1331dcccb91434d (patch) | |
tree | 4e45858383aff0399184395875966765bb9964cb | |
parent | 360a680cdb03df60673581a8f187c4074a78dd21 (diff) |
fonts as object
-rw-r--r-- | src/sliceitoff/game/game.py | 8 | ||||
-rw-r--r-- | src/sliceitoff/game/gameplay.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/game/mainmenu.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/game/show.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/text/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/text/fonts.py | 16 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 8 | ||||
-rw-r--r-- | tests/test_enemies.py | 6 | ||||
-rw-r--r-- | tests/test_player.py | 4 | ||||
-rw-r--r-- | tests/test_screens.py | 4 | ||||
-rw-r--r-- | tests/test_text.py | 4 |
11 files changed, 33 insertions, 25 deletions
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 7a91ede..a5009c5 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -1,5 +1,5 @@ -""" Slice It Off! - Game where you slice the area where enemies reside to - the minimum +""" game.game - Slice It Off! - Game where you slice the area where smily + faces reside to the minimum. """ from pathlib import Path @@ -7,13 +7,13 @@ from pathlib import Path import pygame from sliceitoff.display import Display -from sliceitoff.text import Fonts from sliceitoff.stats import Stats from sliceitoff.screens import ( hiscores_screen, instructions1_screen, instructions2_screen) from sliceitoff.hiscores import HiScores +from sliceitoff.text import fonts from sliceitoff.sfx import sfx from .mainmenu import Mainmenu, MenuItems @@ -27,11 +27,11 @@ class Game: assets_path = Path(__file__).parent.parent.resolve().joinpath('assets') pygame.init() sfx.init(assets_path) + fonts.init(assets_path) self.clock = pygame.time.Clock() self.display = Display() self.stats = None self.hiscores = HiScores() - Fonts.load_fonts(assets_path) pygame.mouse.set_visible(False) def __del__(self): diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index c756183..fe66f50 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -1,4 +1,4 @@ -""" Reads user input and does actions when game play is on. """ +""" game.gameplay - Reads user input and does actions when game play is on.""" import pygame from sliceitoff.sfx import sfx diff --git a/src/sliceitoff/game/mainmenu.py b/src/sliceitoff/game/mainmenu.py index b4287d7..edb1594 100644 --- a/src/sliceitoff/game/mainmenu.py +++ b/src/sliceitoff/game/mainmenu.py @@ -1,4 +1,4 @@ -""" mainmenu.mainmenu - Let's user choose """ +""" game.mainmenu - Let's user choose """ from enum import IntEnum import pygame diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index 496fc4a..32a87cf 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -1,4 +1,4 @@ -""" Sprite group that show sprites and skips if key is pressed """ +""" game.show - Sprite group that show sprites and skips if key is pressed """ from .anykey import anykey from .explodeout import ExplodeOutGroup diff --git a/src/sliceitoff/text/__init__.py b/src/sliceitoff/text/__init__.py index aed5723..3ec8752 100644 --- a/src/sliceitoff/text/__init__.py +++ b/src/sliceitoff/text/__init__.py @@ -1,4 +1,4 @@ """ text - fonts, letters, texts and related """ from .text import TextPage, LetterSprite, get_letter_surface -from .fonts import Font, Fonts +from .fonts import Font, fonts from .explode import ExplodingSprite diff --git a/src/sliceitoff/text/fonts.py b/src/sliceitoff/text/fonts.py index 9f267b3..8776530 100644 --- a/src/sliceitoff/text/fonts.py +++ b/src/sliceitoff/text/fonts.py @@ -6,17 +6,17 @@ DEBUG = os.getenv("DEBUG") class Fonts: """ Fonts - static class to store loaded fonts """ - fonts = {} + def __init__(self): + self.fonts = {} - @staticmethod - def load_fonts(base_path): + def init(self, base_path): """ loads fonts from list """ filename_fontlist = os.path.join(base_path, "fonts.lst") with open(filename_fontlist, "r", encoding="utf-8") 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) + self.fonts[name] = Font(filename) return True @@ -42,3 +42,11 @@ class Font: def get(self, ch): """ Just get surface of the font size 8x16 max """ return self.surfaces[ch%256] + +# Initialize only one time +try: + # pylint: disable = used-before-assignment + # This is intented behaviour + fonts +except NameError: + fonts = Fonts() diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index 3dafa64..cd5b88a 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -2,7 +2,7 @@ import pygame from sliceitoff.display import Scaling, CGA_COLORS -from .fonts import Fonts +from .fonts import fonts from .explode import ExplodingSprite scaled_fonts = {} @@ -16,13 +16,13 @@ def get_letter_surface(font_key, ch): color: 0-15 as in CGA palette """ font, w, color = font_key - if font not in Fonts.fonts: + 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), + fonts.fonts[font].get(ch), w/8 * Scaling.factor) scaled_fonts[font_key][ch].fill( CGA_COLORS[color], @@ -47,7 +47,7 @@ class TextPage(pygame.sprite.Group): pos Position of right top corner in internal cooordinates size Single character size (w,h) grid Space for a character (w,h) - font Font loaded in Fonts.fonts dict + font Font loaded in fonts.fonts dict """ # pylint: disable = too-many-arguments # all argumets necessaary diff --git a/tests/test_enemies.py b/tests/test_enemies.py index c77faea..0ba2f53 100644 --- a/tests/test_enemies.py +++ b/tests/test_enemies.py @@ -7,11 +7,11 @@ from pathlib import Path from sliceitoff.enemies.enemies import EnemyBall, Enemies from sliceitoff.display import Scaling -from sliceitoff.text import Fonts +from sliceitoff.text import fonts class TestEnemyBall(unittest.TestCase): def setUp(self): - Fonts.load_fonts( Path(__file__).parent.parent.resolve() + fonts.init( Path(__file__).parent.parent.resolve() .joinpath('src').joinpath('sliceitoff').joinpath('assets')) def test_can_create(self): @@ -21,7 +21,7 @@ class TestEnemyBall(unittest.TestCase): class TestEnemies(unittest.TestCase): def setUp(self): Scaling.update_scaling((640,400)) - Fonts.load_fonts( Path(__file__).parent.parent.resolve() + fonts.init( Path(__file__).parent.parent.resolve() .joinpath('src').joinpath('sliceitoff').joinpath('assets')) def test_can_create(self): diff --git a/tests/test_player.py b/tests/test_player.py index bf3647a..82da106 100644 --- a/tests/test_player.py +++ b/tests/test_player.py @@ -7,7 +7,7 @@ import unittest import pygame from sliceitoff.player import Player, Life -from sliceitoff.text import Fonts +from sliceitoff.text import fonts from sliceitoff.display import Scaling class TestPlayer(unittest.TestCase): @@ -31,7 +31,7 @@ class TestPlayer(unittest.TestCase): class TestLife(unittest.TestCase): def setUp(self): Scaling.update_scaling((640,480)) - Fonts.load_fonts( Path(__file__).parent.parent.resolve() + fonts.init( Path(__file__).parent.parent.resolve() .joinpath('src').joinpath('sliceitoff').joinpath('assets')) self.life = Life() diff --git a/tests/test_screens.py b/tests/test_screens.py index 8e5ab34..00cf694 100644 --- a/tests/test_screens.py +++ b/tests/test_screens.py @@ -3,7 +3,7 @@ import unittest from pathlib import Path -from sliceitoff.text import Fonts, TextPage +from sliceitoff.text import fonts, TextPage from sliceitoff.screens import ( gameover_screen, @@ -17,7 +17,7 @@ from sliceitoff.screens import ( class TestScreens(unittest.TestCase): def setUp(self): - Fonts.load_fonts( Path(__file__).parent.parent.resolve() + fonts.init( Path(__file__).parent.parent.resolve() .joinpath('src').joinpath('sliceitoff').joinpath('assets')) def test_can_run_gameover(self): diff --git a/tests/test_text.py b/tests/test_text.py index 6cd8c5a..5228782 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -3,11 +3,11 @@ import unittest from pathlib import Path -from sliceitoff.text import Fonts, TextPage +from sliceitoff.text import fonts, TextPage class TestText(unittest.TestCase): def setUp(self): - Fonts.load_fonts( Path(__file__).parent.parent.resolve() + fonts.init( Path(__file__).parent.parent.resolve() .joinpath('src').joinpath('sliceitoff').joinpath('assets')) def test_can_create(self): |