浏览代码

texts module changed to widgets and bugs fixed

Zero! Studio 1 年之前
父节点
当前提交
bc429ea601
共有 6 个文件被更改,包括 51 次插入31 次删除
  1. 1 1
      jogai/__init__.py
  2. 3 4
      jogai/character.py
  3. 12 12
      jogai/videogame.py
  4. 21 13
      jogai/widgets.py
  5. 13 1
      poetry.lock
  6. 1 0
      pyproject.toml

+ 1 - 1
jogai/__init__.py

@@ -4,8 +4,8 @@ from jogai import settings
 from jogai.character import Character
 from jogai.group import *
 from jogai.scene import Scene
-from jogai.texts import *
 from jogai.videogame import *
+from jogai.widgets import *
 
 if not isinstance(settings.LEVELBARS_MAX_PARTS, int):
     raise TypeError(_('LEVELBARS_MAX_PARTS must be an integer.'))

+ 3 - 4
jogai/character.py

@@ -4,7 +4,7 @@ import pygame
 
 import jogai
 import jogai._globals as _globals
-from jogai import logger, settings, texts, utils
+from jogai import logger, settings, utils, widgets
 from jogai.translations import gettext as _
 from jogai.utils import _get_keys
 
@@ -554,7 +554,7 @@ class Character(pygame.sprite.Sprite):
         """
 
         if self.lives is None:
-            self.lives = texts.LevelBar(lives)
+            self.lives = widgets.LevelBar(lives)
             _globals.vg_levelbars.append(self.lives)
         self.lives.set(lives)
 
@@ -615,7 +615,7 @@ class Character(pygame.sprite.Sprite):
             raise TypeError(_('The score must be an integer.'))
         scoreboard_pos = len(_globals.vg_scoreboards)
         if self.score is None:
-            self.score = texts.Scoreboard(
+            self.score = widgets.Scoreboard(
                 f'{self.name} ',
                 score=score,
                 position=(
@@ -627,7 +627,6 @@ class Character(pygame.sprite.Sprite):
         else:
             self.score.set(score)
 
-
     def set_speed(self, speed: int):
         """
         Set the character speed.

+ 12 - 12
jogai/videogame.py

@@ -8,7 +8,7 @@ import pygame
 import jogai
 import jogai._globals as _globals
 import jogai.settings as settings
-from jogai import character, group, logger, scene, texts, utils
+from jogai import character, group, logger, scene, utils, widgets
 from jogai.translations import gettext as _
 from jogai.utils import _get_events, _get_keys
 
@@ -165,7 +165,7 @@ def add_group(new_group: 'group.SquareGroup'):
     _globals.vg_groups.append(new_group)
 
 
-def add_levelbar(new_levelbar: 'texts.LevelBar'):
+def add_levelbar(new_levelbar: 'widgets.LevelBar'):
     """
     Add a new level bar to the videogame.
 
@@ -173,15 +173,15 @@ def add_levelbar(new_levelbar: 'texts.LevelBar'):
         new_levelbar: the level bar to be added
 
     Examples:
-        >>> lb = texts.LevelBar(3)
+        >>> lb = widgets.LevelBar(3)
         >>> add_levelbar(lb) # doctest: +SKIP
     """
-    if not isinstance(new_levelbar, texts.LevelBar):
+    if not isinstance(new_levelbar, widgets.LevelBar):
         raise TypeError(_('Only LevelBar instances can be added.'))
     _globals.vg_levelbars.append(new_levelbar)
 
 
-def add_scoreboard(new_text: 'texts.Text'):
+def add_scoreboard(new_text: 'widgets.Text'):
     """
     Add a new scoreboard to the videogame.
 
@@ -189,15 +189,15 @@ def add_scoreboard(new_text: 'texts.Text'):
         new_scoreboard: the scoreboard to be added
 
     Examples:
-        >>> sc = texts.Scoreboard('Test')
+        >>> sc = widgets.Scoreboard('Test')
         >>> add_scoreboard(sc) # doctest: +SKIP
     """
-    if not isinstance(new_scoreboard, texts.Scoreboard):
+    if not isinstance(new_scoreboard, widgets.Scoreboard):
         raise TypeError(_('Only Scoreboard instances can be added.'))
     _globals.vg_scobreboards.append(new_scoreboard)
 
 
-def add_text(new_text: 'texts.Text'):
+def add_text(new_text: 'widgets.Text'):
     """
     Add a new text to the videogame.
 
@@ -205,12 +205,12 @@ def add_text(new_text: 'texts.Text'):
         new_text: the text to be added
 
     Examples:
-        >>> text = texts.Text('Test')
+        >>> text = widgets.Text('Test')
         >>> add_text(text) # doctest: +SKIP
     """
-    if not isinstance(new_text, texts.Text):
+    if not isinstance(new_text, widgets.Text):
         raise TypeError(_('Only Text instances can be added.'))
-    _globals.vg_texts.append(new_text)
+    _globals.vg_widgets.append(new_text)
 
 
 def init():
@@ -290,7 +290,7 @@ def get_color(x: int, y: int) -> Tuple[int, int, int]:
 def paint(
     sprite: Type['scene.Scene']
     | Type['character.Character']
-    | Type['texts.Text']
+    | Type['widgets.Text'],
 ):
     """
     Paint both a scene and a character.

+ 21 - 13
jogai/texts.py → jogai/widgets.py

@@ -5,6 +5,7 @@ import pygame
 from jogai import _globals, settings
 from jogai.translations import gettext as _
 
+
 class Text:
     def __init__(
         self,
@@ -17,6 +18,8 @@ class Text:
         italic: bool = False,
         shadow: tuple = (),
     ):
+        if not pygame.font.get_init():
+            pygame.font.init()
         self.color = color
         self.font = pygame.font.Font(join(settings.FONTS_DIR, font), size)
         self.font.set_bold(bold)
@@ -47,7 +50,6 @@ class Text:
         self.rect = self.image.get_rect()
         self.rect.x, self.rect.y = position
 
-
     def set_message(self, message: str, key: str = ''):
         """
         Set a new message for the text.
@@ -83,7 +85,7 @@ class Scoreboard(Text):
             raise TypeError(_('The prefix text must be a string.'))
         super().__init__(**kwargs)
         self._prefix = prefix
-        self.set(score)
+        self.set_score(score)
         if 'position' in kwargs:
             self.render(position=kwargs['position'])
         else:
@@ -106,7 +108,7 @@ class Scoreboard(Text):
     def message(self):
         return self._message
 
-    def set(self, score: int):
+    def set_score(self, score: int):
         """
         Set the character score.
 
@@ -120,10 +122,10 @@ class Scoreboard(Text):
         if not isinstance(score, int):
             raise TypeError(_('Score must be an integer.'))
         self._score = score
-        self._message = f'{self._prefix}{self.score}'
+        self._message = f'{self._prefix}{self._score}'
         self.render()
 
-    def add(self, score: int):
+    def add_score(self, score: int):
         """
         Increment the character score.
 
@@ -132,11 +134,11 @@ class Scoreboard(Text):
 
         Examples:
             >>> scoreboard = Scoreboard('My character: ')
-            >>> scoreboard.add(5)
+            >>> scoreboard.add_score(5)
         """
         if not isinstance(score, int):
             raise TypeError(_('Score must be an integer.'))
-        self.score += score
+        self._score += score
 
 
 class LevelBar(pygame.Surface):
@@ -207,13 +209,15 @@ class LevelBar(pygame.Surface):
             increase: amount to increase
 
         Examples:
-            >>> lifes = LevelBar(5)
+            >>> lifes = LevelBar(5, 2)
             >>> lifes.increase(1)
         """
         if not isinstance(increase, int):
             raise TypeError(_('The increase level must be an integer.'))
         if self._level + increase > self._parts:
-            raise ValueError(_('The increase cannot exceed the maximum level.'))
+            raise ValueError(
+                _('The increase cannot exceed the maximum level.')
+            )
         self.set(self._level + increase)
 
     def set(self, level):
@@ -266,7 +270,7 @@ class LevelBar(pygame.Surface):
                 _globals._window,
                 self.border_color,
                 (position[0] + division * part, position[1]),
-                (position[0] + division * (part+1), position[1]),
+                (position[0] + division * (part + 1), position[1]),
                 self._thick,
             )
             # inside
@@ -276,10 +280,11 @@ class LevelBar(pygame.Surface):
                     self.fill_color,
                     (
                         position[0] + division * part + self._thick / 2,
-                        position[1] + int(self._height / 2)),
+                        position[1] + int(self._height / 2),
+                    ),
                     (
                         position[0] + division * (part + 1) - self._thick / 2,
-                        position[1] + int(self._height / 2)
+                        position[1] + int(self._height / 2),
                     ),
                     self._height,
                 )
@@ -288,7 +293,10 @@ class LevelBar(pygame.Surface):
                 _globals._window,
                 self.border_color,
                 (position[0] + division * part, position[1] + self._height),
-                (position[0] + division * (part + 1), position[1] + self._height),
+                (
+                    position[0] + division * (part + 1),
+                    position[1] + self._height,
+                ),
                 self._thick,
             )
         self.rect = self.get_rect()

+ 13 - 1
poetry.lock

@@ -798,6 +798,18 @@ files = [
 dev = ["pre-commit", "tox"]
 testing = ["coverage", "pytest", "pytest-benchmark"]
 
+[[package]]
+name = "polib"
+version = "1.2.0"
+description = "A library to manipulate gettext files (po and mo files)."
+optional = false
+python-versions = "*"
+groups = ["dev"]
+files = [
+    {file = "polib-1.2.0-py2.py3-none-any.whl", hash = "sha256:1c77ee1b81feb31df9bca258cbc58db1bbb32d10214b173882452c73af06d62d"},
+    {file = "polib-1.2.0.tar.gz", hash = "sha256:f3ef94aefed6e183e342a8a269ae1fc4742ba193186ad76f175938621dbfc26b"},
+]
+
 [[package]]
 name = "psutil"
 version = "6.1.1"
@@ -1249,4 +1261,4 @@ watchmedo = ["PyYAML (>=3.10)"]
 [metadata]
 lock-version = "2.1"
 python-versions = ">=3.11,<4.0"
-content-hash = "b1884f81a02dc0ea5ed060ac2589dbc1d9ad44974d58c803ea1759be0e232a8c"
+content-hash = "270d9aa0b331de80c37225620983d01731a9d87c7effa409e3e1065d7010402e"

+ 1 - 0
pyproject.toml

@@ -23,6 +23,7 @@ isort = "^6.0.1"
 blue = "^0.9.1"
 taskipy = "^1.14.1"
 pygame = "^2.6.1"
+polib = "^1.2.0"
 
 
 [tool.poetry.group.doc.dependencies]