Ajout d'un systeme de Hover, savoir si la souris est au dessu du truc, de la texture, non ? #18
|
@ -1,3 +1,5 @@
|
||||||
[MESSAGES CONTROL]
|
[MESSAGES CONTROL]
|
||||||
disable=all
|
disable=all
|
||||||
enable= missing-docstring
|
enable=
|
||||||
|
missing-docstring,
|
||||||
|
unused-import,
|
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
|
@ -8,5 +8,10 @@
|
||||||
"python": true,
|
"python": true,
|
||||||
"mypy.ini": true,
|
"mypy.ini": true,
|
||||||
".pylintrc": true,
|
".pylintrc": true,
|
||||||
}
|
},
|
||||||
|
"explorer.excludeGitIgnore": true,
|
||||||
|
"mypy.dmypyExecutable": "${workspaceFolder}/.venv/Scripts/dmypy.exe",
|
||||||
|
"pylint.interpreter": [
|
||||||
|
"${workspaceFolder}/.venv/Scripts/python.exe"
|
||||||
|
],
|
||||||
}
|
}
|
BIN
icon.png
Normal file
BIN
icon.png
Normal file
Binary file not shown.
Before Width: | Height: | Size: 379 KiB After Width: | Height: | Size: 379 KiB |
|
@ -4,7 +4,7 @@ Définit un plugin qui gère les évenements pygame.
|
||||||
|
|
||||||
from engine import *
|
from engine import *
|
||||||
from engine.math import Vec2
|
from engine.math import Vec2
|
||||||
import pygame
|
import pygame, os
|
||||||
|
|
||||||
|
|
||||||
class PygamePlugin(Plugin):
|
class PygamePlugin(Plugin):
|
||||||
|
@ -12,6 +12,9 @@ class PygamePlugin(Plugin):
|
||||||
Plugin qui gère les évenements pygame.
|
Plugin qui gère les évenements pygame.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, title: str = "Game") -> None:
|
||||||
|
self.title = title
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _find_surface_rect() -> tuple[float, float, float, float]:
|
def _find_surface_rect() -> tuple[float, float, float, float]:
|
||||||
width, height = pygame.display.get_surface().get_size()
|
width, height = pygame.display.get_surface().get_size()
|
||||||
|
@ -26,11 +29,14 @@ class PygamePlugin(Plugin):
|
||||||
return rect
|
return rect
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _initialize(world: World) -> None:
|
def _initialize(world: World, title: str) -> None:
|
||||||
"""
|
"""
|
||||||
Initialize pygame et les ressources.
|
Initialize pygame et les ressources.
|
||||||
"""
|
"""
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
if os.path.exists("icon.png"):
|
||||||
|
pygame.display.set_icon(pygame.image.load("icon.png"))
|
||||||
|
pygame.display.set_caption(title)
|
||||||
pygame.display.set_mode((800, 600), pygame.RESIZABLE)
|
pygame.display.set_mode((800, 600), pygame.RESIZABLE)
|
||||||
|
|
||||||
# Initialisation des ressources
|
# Initialisation des ressources
|
||||||
|
@ -112,7 +118,9 @@ class PygamePlugin(Plugin):
|
||||||
Paramètres:
|
Paramètres:
|
||||||
game: Le jeu auquel appliquer le plugin.
|
game: Le jeu auquel appliquer le plugin.
|
||||||
"""
|
"""
|
||||||
game.add_pre_startup_tasks(self._initialize)
|
game.add_pre_startup_tasks(
|
||||||
|
lambda world: PygamePlugin._initialize(world, self.title)
|
||||||
|
)
|
||||||
game.add_pre_update_tasks(self._check_events)
|
game.add_pre_update_tasks(self._check_events)
|
||||||
game.add_post_render_tasks(self._update_display)
|
game.add_post_render_tasks(self._update_display)
|
||||||
game.add_post_shutdown_tasks(self._terminate)
|
game.add_post_shutdown_tasks(self._terminate)
|
||||||
|
|
|
@ -4,7 +4,7 @@ Définis un plugin permettant d'afficher des choses a l'écran.
|
||||||
|
|
||||||
from engine import *
|
from engine import *
|
||||||
from engine.math import Vec2
|
from engine.math import Vec2
|
||||||
import pygame, os
|
import pygame, glob
|
||||||
|
|
||||||
from engine.plugins.pygame import Display
|
from engine.plugins.pygame import Display
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ class RenderPlugin(Plugin):
|
||||||
Initialize le système de rendu.
|
Initialize le système de rendu.
|
||||||
"""
|
"""
|
||||||
world.set(TextureManager(world[Display]))
|
world.set(TextureManager(world[Display]))
|
||||||
|
world.set(FontManager())
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _render(world: World) -> None:
|
def _render(world: World) -> None:
|
||||||
|
@ -28,6 +29,7 @@ class RenderPlugin(Plugin):
|
||||||
"""
|
"""
|
||||||
display = world[Display]
|
display = world[Display]
|
||||||
textures = world[TextureManager]
|
textures = world[TextureManager]
|
||||||
|
fonts = world[FontManager]
|
||||||
|
|
||||||
# Rendu de toutes les objects de rendu
|
# Rendu de toutes les objects de rendu
|
||||||
entities = sorted(world.query(Order, Position), key=lambda e: e[Order])
|
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)
|
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:
|
def apply(self, game: Game) -> None:
|
||||||
"""
|
"""
|
||||||
Applique le plugin a un jeu.
|
Applique le plugin a un jeu.
|
||||||
|
@ -71,10 +84,12 @@ class TextureManager:
|
||||||
|
|
||||||
def __init__(self, display: Display) -> None:
|
def __init__(self, display: Display) -> None:
|
||||||
self._textures: dict[str, pygame.Surface] = {}
|
self._textures: dict[str, pygame.Surface] = {}
|
||||||
for file in os.listdir("textures"):
|
for file in glob.iglob("textures/**/*.png", recursive=True):
|
||||||
self._textures[file] = pygame.image.load(f"textures/{file}").convert(
|
self._textures[file[9:]] = pygame.image.load(file).convert_alpha(
|
||||||
display._surface
|
display._surface
|
||||||
)
|
)
|
||||||
|
for file in glob.iglob("textures/**/*.jpg", recursive=True):
|
||||||
|
self._textures[file[9:]] = pygame.image.load(file).convert(display._surface)
|
||||||
error_texture = pygame.Surface((256, 256))
|
error_texture = pygame.Surface((256, 256))
|
||||||
error_texture.fill((0, 0, 0))
|
error_texture.fill((0, 0, 0))
|
||||||
pygame.draw.rect(error_texture, (255, 0, 255), (0, 0, 128, 128))
|
pygame.draw.rect(error_texture, (255, 0, 255), (0, 0, 128, 128))
|
||||||
|
@ -89,3 +104,35 @@ class Texture(str):
|
||||||
"""
|
"""
|
||||||
Composant qui represente la texture d'un sprite.
|
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.
|
||||||
|
"""
|
||||||
|
|
18
src/main.py
18
src/main.py
|
@ -6,14 +6,23 @@ Ceci est un exemple de comment l'on peut utiliser le moteur du jeu.
|
||||||
from engine import *
|
from engine import *
|
||||||
from engine.math import Vec2
|
from engine.math import Vec2
|
||||||
from engine.plugins.hover import HoverPlugin
|
from engine.plugins.hover import HoverPlugin
|
||||||
from engine.plugins.render import Order, RenderPlugin, Position, Texture
|
from engine.plugins.render import (
|
||||||
|
Order,
|
||||||
|
RenderPlugin,
|
||||||
|
Position,
|
||||||
|
Text,
|
||||||
|
TextColor,
|
||||||
|
TextSize,
|
||||||
|
)
|
||||||
from engine.plugins.timing import Delta, TimePlugin
|
from engine.plugins.timing import Delta, TimePlugin
|
||||||
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
||||||
from random import random
|
from random import random
|
||||||
|
|
||||||
|
|
||||||
# Initialisation
|
# Initialisation
|
||||||
game = Game(TimePlugin(), PygamePlugin(), RenderPlugin(), HoverPlugin())
|
game = Game(
|
||||||
|
TimePlugin(), PygamePlugin("Guess The Number"), RenderPlugin(), HoverPlugin()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# On créer une tache pour afficher des sprites
|
# On créer une tache pour afficher des sprites
|
||||||
|
@ -25,7 +34,10 @@ def spawn_sprites(world: World) -> None:
|
||||||
red = random() < 0.1
|
red = random() < 0.1
|
||||||
world.create_entity(
|
world.create_entity(
|
||||||
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
|
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),
|
Order(1 if red else 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue