moved the fps counter from a widget to the GameWindow features, window can now react to event after all scenes got their corresponding event called by adding "_after" in the name of the method event

This commit is contained in:
Faraphel 2023-02-18 20:05:44 +01:00
parent 446e377b19
commit 15161764ad
10 changed files with 46 additions and 56 deletions

25
NOTE.md
View file

@ -1,22 +1,5 @@
A faire :
Widgets:
- Grille (bataille navale)
Réseau :
- Connexion entre les joueurs
- Envoie des paramètres à l'autre joueur (largeur longueur du plateau, nombre de bateau et leur taille, ...)
- Phase de placement des bateaux
- Confirmer l'emplacement des bateaux (envoie des données signalant la confirmation)
- Joueur 1 sélectionne une case (envoie des données à l'autre joueur)
- Joueur 2 envoie la valeur de retour de board.bomb après les données de joueur 1
- Joueur 2 sélectionne une case
- Joueur 1 envoie la valeur de retour également
...
- Joueur 1 gagne (envoie des données de victoire à l'autre joueur)
- Joueur 1 et Joueur 2 s'envoie mutuellement leurs plateaux pour voir les cases restantes de l'autre
- Proposition de rejouer ?
- Optimiser les textures dans un dictionnaires
- Optimiser les textures similaire dans un atlas
- Toutes les scènes devrait proposer un compteur de fps
- Permettre de forcer la taille de la fenêtre à un ratio

View file

@ -1,11 +1,11 @@
import pyglet
from source.gui.scene import MainMenu
from source.gui.window import Window
from source.gui.window import GameWindow
# Create a new window
window = Window(resizable=True, vsync=True)
window = GameWindow(resizable=True, vsync=True)
window.add_scene(MainMenu)
# Start the event loop

View file

@ -4,7 +4,6 @@ import pyglet
from source.gui.scene.abc import Scene
from source.gui import widget, scene
from source.gui.widget.debug import FPSDisplay
if TYPE_CHECKING:
from source.gui.window import Window
@ -77,13 +76,9 @@ class MainMenu(Scene):
self.settings.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.Settings))
self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 180))
def on_draw(self):
self.background.draw()
self.title.draw()
self.game_create.draw()
self.game_join.draw()
self.settings.draw()
self.fps_display.draw()

View file

@ -24,9 +24,9 @@ class Scene(ABC):
def add_widget(self, widget_class: Type["Widget"], **widget_kwargs):
"""
Add a widget to the scene.
:widget_class: the class of the widget to add.
:widget_args: args for the creation of the widget object.
:widget_kwargs: kwargs for the creation of the widget object.
:param widget_class: the class of the widget to add.
:param widget_args: args for the creation of the widget object.
:param widget_kwargs: kwargs for the creation of the widget object.
:return: the new created widget.
"""
@ -37,7 +37,7 @@ class Scene(ABC):
def remove_widget(self, widget: "Widget") -> None:
"""
Remove a widget from the scene.
:scene: the widget to remove.
:param scene: the widget to remove.
"""
self._widgets.remove(widget)

View file

@ -1,22 +0,0 @@
from source.gui.widget.abc import Widget
from typing import TYPE_CHECKING
import pyglet
if TYPE_CHECKING:
from source.gui.scene.abc import Scene
class FPSDisplay(Widget):
"""
A widget that display the current FPS of the scene's window
"""
def __init__(self, scene: "Scene", **kwargs):
super().__init__(scene)
self.fps_display = pyglet.window.FPSDisplay(scene.window, **kwargs)
def draw(self):
self.fps_display.draw()

View file

@ -1 +0,0 @@
from .FPSDisplay import FPSDisplay

View file

@ -0,0 +1,24 @@
import pyglet.window
from source.gui.window import Window
from source.type import Color
class GameWindow(Window): # NOQA
"""
Similar to the normal Window, but add small feature useful for a game like a fps counter.
"""
def __init__(self,
fps_color: Color = (255, 255, 255, 200),
fps_enable: bool = True,
*args, **kwargs):
super().__init__(*args, **kwargs)
self._fps_counter = pyglet.window.FPSDisplay(self, color=fps_color)
self.fps_enable = fps_enable
def on_draw_after(self):
if self.fps_enable: self._fps_counter.draw()

View file

@ -76,15 +76,24 @@ class Window(pyglet.window.Window): # NOQA
:return: une fonction appelant l'événement original ainsi que ceux des scènes.
"""
# try to get the original function
func = None
try: func = super().__getattribute__(item)
except AttributeError: pass
# try to get a function that would get executed after everything else
func_after = None
try: func_after = super().__getattribute__(item + "_after")
except AttributeError: pass
def _func(*args, **kwargs) -> None:
if func is not None: func(*args, **kwargs)
for scene in self._scenes:
getattr(scene, item)(*args, **kwargs)
if func_after is not None: func_after(*args, **kwargs)
return _func
def __getattribute__(self, item: str) -> Any:

View file

@ -1 +1,2 @@
from .Window import Window
from .GameWindow import GameWindow

View file

@ -3,7 +3,8 @@ from typing import Union, Callable, Any
Point2D = tuple[int, int]
BBox = tuple[int, int, int, int]
Percentage = float # a percentage, represented as a number between 0 and 1
Color = tuple[int, int, int, int]
DistanceFunction = Callable[[Any], int] # a function that return a distance
# a distance, represented either by a whole number, a percentage or a function
Distance = Union[Percentage, int, DistanceFunction]
Distance = Union[Percentage, int, DistanceFunction]