Ajout du systeme de texte
This commit is contained in:
parent
e4abae1b52
commit
7ff6c6323f
|
@ -20,6 +20,7 @@ class RenderPlugin(Plugin):
|
|||
Initialize le système de rendu.
|
||||
"""
|
||||
world.set(TextureManager(world[Display]))
|
||||
world.set(FontManager())
|
||||
|
||||
@staticmethod
|
||||
def _render(world: World) -> None:
|
||||
|
@ -28,6 +29,7 @@ class RenderPlugin(Plugin):
|
|||
"""
|
||||
display = world[Display]
|
||||
textures = world[TextureManager]
|
||||
fonts = world[FontManager]
|
||||
|
||||
# Rendu de toutes les objects de rendu
|
||||
entities = sorted(world.query(Order, Position), key=lambda e: e[Order])
|
||||
|
@ -41,6 +43,17 @@ class RenderPlugin(Plugin):
|
|||
textures[entity[Texture]], (position.x, position.y)
|
||||
)
|
||||
|
||||
# Affichage du texte
|
||||
if Text in entity and TextSize in entity:
|
||||
color = (
|
||||
entity[TextColor]
|
||||
if TextColor in entity
|
||||
else pygame.Color(255, 255, 255)
|
||||
)
|
||||
font = fonts[entity[TextSize]]
|
||||
font_surface = font.render(entity[Text], True, color)
|
||||
display._surface.blit(font_surface, (position.x, position.y))
|
||||
|
||||
def apply(self, game: Game) -> None:
|
||||
"""
|
||||
Applique le plugin a un jeu.
|
||||
|
@ -89,3 +102,35 @@ class Texture(str):
|
|||
"""
|
||||
Composant qui represente la texture d'un sprite.
|
||||
"""
|
||||
|
||||
|
||||
class FontManager:
|
||||
"""
|
||||
Ressource qui contient les fonts du jeu.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._fonts: dict[int, pygame.font.Font] = {}
|
||||
|
||||
def __getitem__(self, size: int) -> pygame.font.Font:
|
||||
if size not in self._fonts:
|
||||
self._fonts[size] = pygame.font.Font("font.ttf", size)
|
||||
return self._fonts[size]
|
||||
|
||||
|
||||
class Text(str):
|
||||
"""
|
||||
Composant qui represente un texte.
|
||||
"""
|
||||
|
||||
|
||||
class TextSize(int):
|
||||
"""
|
||||
Composant qui represente la taille d'un texte.
|
||||
"""
|
||||
|
||||
|
||||
class TextColor(pygame.Color):
|
||||
"""
|
||||
Composant qui represente la couleur d'un texte.
|
||||
"""
|
||||
|
|
15
src/main.py
15
src/main.py
|
@ -5,7 +5,15 @@ Ceci est un exemple de comment l'on peut utiliser le moteur du jeu.
|
|||
|
||||
from engine import *
|
||||
from engine.math import Vec2
|
||||
from engine.plugins.render import Order, RenderPlugin, Position, Texture
|
||||
from engine.plugins.render import (
|
||||
Order,
|
||||
RenderPlugin,
|
||||
Position,
|
||||
Text,
|
||||
TextColor,
|
||||
TextSize,
|
||||
Texture,
|
||||
)
|
||||
from engine.plugins.timing import Delta, TimePlugin
|
||||
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
||||
from random import random
|
||||
|
@ -24,7 +32,10 @@ def spawn_sprites(world: World) -> None:
|
|||
red = random() < 0.1
|
||||
world.create_entity(
|
||||
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
|
||||
Texture("test2.png") if red else Texture("test.png"),
|
||||
# Texture("test2.png") if red else Texture("test.png"),
|
||||
Text("Hello les gens"),
|
||||
TextSize(50),
|
||||
TextColor(255, 0, 0) if red else TextColor(255, 255, 255),
|
||||
Order(1 if red else 0),
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue