Ajout de score en ligne (#9)

Co-authored-by: CoCoSol007 <solois.corentin@gmail.com>
Co-authored-by: Tipragot <contact@tipragot.fr>
Reviewed-on: #9
Reviewed-by: Tipragot <contact@tipragot.fr>
Co-authored-by: Raphaël <r.lauray@outlook.fr>
Co-committed-by: Raphaël <r.lauray@outlook.fr>
This commit is contained in:
Raphaël 2024-01-07 07:32:52 +00:00 committed by Tipragot
parent cd81b0c34a
commit ea70c297b2
24 changed files with 1211 additions and 744 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

View file

@ -1,52 +1,71 @@
""" """
Un plugin permettant de connaitre le temps depuis le Un plugin permettant de connaitre le temps depuis le
lancement du jeu et le temps depuis la dernière frame. lancement du jeu et le temps depuis la dernière frame.
""" """
from time import time from time import time
from engine import GlobalPlugin, KeepAlive from typing import Callable
from engine.ecs import World from engine import GlobalPlugin, KeepAlive
from engine.ecs import Entity, World
class GlobalTime(KeepAlive, float):
""" class GlobalTime(KeepAlive, float):
Ressource qui représente le temps global de l'ordinateur sur lequel tourne le jeu. """
""" Ressource qui représente le temps global de l'ordinateur sur lequel tourne le jeu.
"""
class Time(KeepAlive, float):
""" class Time(KeepAlive, float):
Ressource qui représente le temps depuis le lancement du jeu. """
""" Ressource qui représente le temps depuis le lancement du jeu.
"""
class Delta(KeepAlive, float):
""" class Delta(KeepAlive, float):
Ressource qui détermine le temps depuis la première frame. """
""" Ressource qui détermine le temps depuis la première frame.
"""
def __initialize(world: World):
""" def __initialize(world: World):
Initialise les ressources pour la gestion du temps. """
""" Initialise les ressources pour la gestion du temps.
world.set(GlobalTime(time()), Time(0.0)) """
world.set(GlobalTime(time()), Time(0.0))
def __update(world: World):
""" class TimedEvent:
Met à jour les ressources de temps. """
""" Composant permettant d'executer un callback après un certain temps.
now = time() """
world[Delta] = delta = now - world[GlobalTime]
world[GlobalTime] = now def __init__(self, timer: float, callback: Callable[[World, Entity], object]):
world[Time] += delta self.timer = timer
self.callback = callback
PLUGIN = GlobalPlugin(
[__initialize], def __update(world: World):
[__update], """
[], Met à jour les ressources de temps.
[], """
) now = time()
world[Delta] = delta = now - world[GlobalTime]
world[GlobalTime] = now
world[Time] += delta
# On met à jour les `TimedEvent`
for entity in world.query(TimedEvent):
event = entity[TimedEvent]
event.timer -= delta
if event.timer <= 0:
del entity[TimedEvent]
event.callback(world, entity)
PLUGIN = GlobalPlugin(
[__initialize],
[__update],
[],
[],
)

63
src/plugins/writing.py Normal file
View file

@ -0,0 +1,63 @@
"""
Definit un plugin qui crée un texte avec les touches frappées
"""
from engine import Scene, World
from plugins.inputs import Pressed
from plugins.render import Text
from plugins.sound import Sound
class Writing:
"""
Marque une entité comme un texte qui s'ecrit en fonction du clavier
"""
def __init__(
self, accepted_chars: str, max_chars: int = 10, base_text: str = ""
) -> None:
self.accepted_chars = accepted_chars
self.max_chars = max_chars
self.base_text = base_text
def __update(world: World):
"""
Met a jour les entitées contenant le composant Typing
"""
pressed = world[Pressed]
for entity in world.query(Writing, Text):
writing = entity[Writing]
for key in pressed:
if key == "backspace" and entity[Text] != writing.base_text:
entity[Text] = entity[Text][:-1]
world.new_entity().set(Sound("click.wav"))
if entity[Text] == "":
entity[Text] = writing.base_text
if key.startswith("["): # pavé numerique
key = key[1]
match key:
case "6":
key = "-"
case "8":
key = "_"
case _:
pass
if key in writing.accepted_chars and (
entity[Text] == writing.base_text
or len(entity[Text]) < writing.max_chars
):
if entity[Text] == writing.base_text:
entity[Text] = key
else:
entity[Text] += key
world.new_entity().set(Sound("click.wav"))
if entity[Text] == "":
entity[Text] = writing.base_text
PLUGIN = Scene(
[],
[__update],
[],
)

File diff suppressed because it is too large Load diff

81
src/scenes/game_over.py Normal file
View file

@ -0,0 +1,81 @@
from engine import CurrentScene, KeepAlive, Scene
from engine.ecs import Entity, World
from engine.math import Vec2
from plugins import render
from plugins.click import Clickable
from plugins.hover import HoveredTexture
from plugins.render import (
SpriteBundle,
TextBundle,
)
from plugins.sound import Sound
from scenes import game, send_to_server, try_again
def __spawn_elements(world: World):
world.new_entity().set(
TextBundle(
"Game Over", 0, 100, position=Vec2(render.WIDTH / 2, 250), origin=Vec2(0.5)
)
)
world.new_entity().set(
TextBundle(
"Voulez vous enregistrer votre Score ?",
0,
50,
position=Vec2(render.WIDTH / 2, 350),
origin=Vec2(0.5),
)
)
world.new_entity().set(
TextBundle(
f"{world[game.Player1Score]}",
0,
50,
position=Vec2(render.WIDTH / 2, 450),
origin=Vec2(0.5),
)
)
button_name = ["yes", "no"]
for i, name in enumerate(button_name):
__create_button(world, i, name)
def __create_button(world: World, i: int, name: str):
"""
Ajoute un bouton au monde.
"""
world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
position=Vec2(450 + 540 * i, render.HEIGHT / 2 + render.HEIGHT / 8),
order=1,
origin=Vec2(0.5),
),
HoveredTexture(
f"button_{name}.png",
f"button_{name}_hover.png",
),
Clickable(lambda world, entity: __on_click_butons(world, entity, name)),
)
def __on_click_butons(world: World, _entity: Entity, name: str):
"""
Fonction qui s'execute quand on clique sur un bouton.
"""
match name:
case "yes":
world[CurrentScene] = send_to_server.SEND
case "no":
world[CurrentScene] = try_again.TRY_AGAIN
case _:
pass
world.new_entity().set(KeepAlive(), Sound("click.wav"))
GAME_OVER = Scene(
[__spawn_elements],
[],
[],
)

View file

@ -1,68 +1,107 @@
""" """
La scène du menu principal du jeu. La scène du menu principal du jeu.
Dans cette scène nous pouvons choisir le mode de jeu. Dans cette scène nous pouvons choisir le mode de jeu.
""" """
from plugins.sound import Loop, Sound from plugins.sound import Loop, Sound
from engine import CurrentScene, KeepAlive, Scene from engine import CurrentScene, KeepAlive, Scene
from engine.ecs import Entity, World from engine.ecs import Entity, World
from engine.math import Vec2 from engine.math import Vec2
from plugins import render from plugins import render
from plugins.click import Clickable from plugins.click import Clickable
from plugins.hover import HoveredTexture from plugins.hover import HoveredTexture
from plugins.render import SpriteBundle from plugins.render import SpriteBundle, TextBundle
from scenes import game from plugins.timing import Time
from scenes import game
import requests as rq
def __create_button(world: World, i: int, name: str):
""" IP = "pong.cocosol.fr"
Ajoute un bouton au monde.
"""
world.new_entity().set( def get_scores() -> list[tuple[int, str]]:
SpriteBundle( try:
f"button_{name}.png", return rq.get(f"https://{IP}/data").json()
position=Vec2(450 + 540 * i, render.HEIGHT / 2), except:
order=1, print("Error with the serveur")
origin=Vec2(0.5), return [(1, "")]
),
HoveredTexture(
f"button_{name}.png", def __create_button(world: World, i: int, name: str):
f"button_{name}_hover.png", """
), Ajoute un bouton au monde.
Clickable(lambda world, entity: __on_click_butons(world, entity, name)), """
) world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
def __on_click_butons(world: World, _entity: Entity, name: str): position=Vec2(450 + 540 * i, 11 * render.HEIGHT / 16),
""" order=1,
Fonction qui s'execute quand on clique sur un bouton. origin=Vec2(0.5),
""" ),
match name: HoveredTexture(
case "one_player": f"button_{name}.png",
world[CurrentScene] = game.ONE_PLAYER f"button_{name}_hover.png",
case "two_player": ),
world[CurrentScene] = game.TWO_PLAYER Clickable(lambda world, entity: __on_click_butons(world, entity, name)),
case _: )
pass
world.new_entity().set(KeepAlive(), Sound("click.wav"))
def __on_click_butons(world: World, _entity: Entity, name: str):
"""
def __spawn_elements(world: World): Fonction qui s'execute quand on clique sur un bouton.
""" """
Ajoute les éléments du menu dans le monde. match name:
""" case "one_player":
world[CurrentScene] = game.ONE_PLAYER
world.new_entity().set(Sound("music.mp3"), KeepAlive(), Loop()) case "two_player":
world[CurrentScene] = game.TWO_PLAYER
world.new_entity().set(SpriteBundle("background_menu.png", -5)) case _:
pass
scenes_name = ["one_player", "two_player"] world.new_entity().set(KeepAlive(), Sound("click.wav"))
for i, name in enumerate(scenes_name):
__create_button(world, i, name)
def __spawn_elements(world: World):
"""
MENU = Scene( Ajoute les éléments du menu dans le monde.
[__spawn_elements], """
[], if world[Time] < 1:
[], world.new_entity().set(Sound("music.mp3"), KeepAlive(), Loop())
)
world.new_entity().set(SpriteBundle("background_menu.png", -5))
scenes_name = ["one_player", "two_player"]
for i, name in enumerate(scenes_name):
__create_button(world, i, name)
__spawn_score(world)
def __spawn_score(world: World):
"""
Ajoute le score dans le monde.
"""
print(get_scores())
for i, (score, name) in enumerate(get_scores()):
world.new_entity().set(
TextBundle(
f"{name}",
position=Vec2(render.WIDTH / 2 - 350, 325 + 50 * i),
origin=Vec2(0),
order=1,
)
)
world.new_entity().set(
TextBundle(
f"{score}",
position=Vec2(render.WIDTH / 2 + 350, 325 + 50 * i),
origin=Vec2(1, 0),
order=1,
)
)
MENU = Scene(
[__spawn_elements],
[],
[],
)

View file

@ -0,0 +1,93 @@
from plugins import writing
from engine import CurrentScene, Plugin
from engine.ecs import Entity, World
from engine.math import Vec2
from plugins import render
from plugins.click import Clickable
from plugins.hover import HoveredTexture
from plugins.render import SpriteBundle, Text, TextBundle
from plugins.timing import TimedEvent
from plugins.writing import Writing
import requests as rq
from scenes import game, thanks
IP = "pong.cocosol.fr"
def new_score(world: World, e: Entity):
e.remove(Clickable)
name = world.query(Writing).pop()
try:
post = {"name": name[Text], "score": world[game.Player1Score]}
print(post)
rq.post(f"https://{IP}/new_score", post)
world.new_entity().set(
TimedEvent(
1,
lambda world, entity: world.set(CurrentScene(thanks.THANKS)),
)
)
except:
print("Error with the serveur")
def get_scores():
try:
return rq.get(f"https://{IP}/data").json()
except:
print("Error with the serveur")
def __spawn_elements(world: World):
"""
Ajoute les éléments du menu dans le monde.
"""
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(
TextBundle(
"Quel est votre pseudo ?",
0,
position=Vec2(render.WIDTH / 2, 350),
origin=Vec2(0.5),
),
)
world.new_entity().set(
TextBundle(
"...",
0,
50,
position=Vec2(render.WIDTH / 2, 475),
origin=Vec2(0.5),
),
Writing(
"azertyuiopqsdfghjklmwxcvbn0123456789_-/",
16,
"...",
),
)
world.new_entity().set(
SpriteBundle(
f"button_one_player.png",
position=Vec2(render.WIDTH / 2, 600),
order=1,
origin=Vec2(0.5),
),
HoveredTexture(
f"button_submit.png",
f"button_submit_hover.png",
),
Clickable(new_score),
)
SEND = (
Plugin(
[__spawn_elements],
[],
[],
)
+ writing.PLUGIN
)

37
src/scenes/thanks.py Normal file
View file

@ -0,0 +1,37 @@
from engine import CurrentScene, Scene
from engine.ecs import World
from engine.math import Vec2
from plugins import render
from plugins.render import SpriteBundle, TextBundle
from plugins.timing import TimedEvent
from scenes import menu
def __spawn_elements(world: World):
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(
TextBundle(
"Merci,",
0,
150,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 - 75),
origin=Vec2(0.5),
),
TimedEvent(3, lambda world, entity: world.set(CurrentScene(menu.MENU))),
)
world.new_entity().set(
TextBundle(
"Votre score a bien été envoyé !",
0,
100,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 + 75),
origin=Vec2(0.5),
),
)
THANKS = Scene(
[__spawn_elements],
[],
[],
)

73
src/scenes/try_again.py Normal file
View file

@ -0,0 +1,73 @@
"""
La scène du menu principal du jeu.
Dans cette scène nous pouvons choisir le mode de jeu.
"""
from plugins.sound import Sound
from engine import CurrentScene, KeepAlive, Scene
from engine.ecs import Entity, World
from engine.math import Vec2
from plugins import render
from plugins.click import Clickable
from plugins.hover import HoveredTexture
from plugins.render import SpriteBundle, TextBundle
from scenes import game, menu
def __create_button(world: World, i: int, name: str):
"""
Ajoute un bouton au monde.
"""
world.new_entity().set(
SpriteBundle(
f"button_{name}.png",
position=Vec2(450 + 540 * i, render.HEIGHT / 2),
order=1,
origin=Vec2(0.5),
),
HoveredTexture(
f"button_{name}.png",
f"button_{name}_hover.png",
),
Clickable(lambda world, entity: __on_click_butons(world, entity, name)),
)
def __on_click_butons(world: World, _entity: Entity, name: str):
"""
Fonction qui s'execute quand on clique sur un bouton.
"""
match name:
case "yes":
world[CurrentScene] = menu.MENU
case "no":
world[CurrentScene] = game.ONE_PLAYER
case _:
pass
world.new_entity().set(KeepAlive(), Sound("click.wav"))
def __spawn_elements(world: World):
"""
Ajoute les éléments du menu dans le monde.
"""
world.new_entity().set(SpriteBundle("background.jpg", -5))
world.new_entity().set(
TextBundle(
"Voulez vous changer\nde mode de jeu ?",
0,
position=Vec2(render.WIDTH / 2, 350),
origin=Vec2(0.5),
),
)
scenes_name = ["yes", "no"]
for i, name in enumerate(scenes_name):
__create_button(world, i, name)
TRY_AGAIN = Scene(
[__spawn_elements],
[],
[],
)