Save unfinished work

This commit is contained in:
Tipragot 2024-01-07 08:32:37 +01:00
parent 1e02eda918
commit b393fad8a2
8 changed files with 1211 additions and 1211 deletions

View file

@ -1,71 +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 typing import Callable from typing import Callable
from engine import GlobalPlugin, KeepAlive from engine import GlobalPlugin, KeepAlive
from engine.ecs import Entity, World 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))
class TimedEvent: class TimedEvent:
""" """
Composant permettant d'executer un callback après un certain temps. Composant permettant d'executer un callback après un certain temps.
""" """
def __init__(self, timer: float, callback: Callable[[World, Entity], object]): def __init__(self, timer: float, callback: Callable[[World, Entity], object]):
self.timer = timer self.timer = timer
self.callback = callback self.callback = callback
def __update(world: World): def __update(world: World):
""" """
Met à jour les ressources de temps. Met à jour les ressources de temps.
""" """
now = time() now = time()
world[Delta] = delta = now - world[GlobalTime] world[Delta] = delta = now - world[GlobalTime]
world[GlobalTime] = now world[GlobalTime] = now
world[Time] += delta world[Time] += delta
# On met à jour les `TimedEvent` # On met à jour les `TimedEvent`
for entity in world.query(TimedEvent): for entity in world.query(TimedEvent):
event = entity[TimedEvent] event = entity[TimedEvent]
event.timer -= delta event.timer -= delta
if event.timer <= 0: if event.timer <= 0:
del entity[TimedEvent] del entity[TimedEvent]
event.callback(world, entity) event.callback(world, entity)
PLUGIN = GlobalPlugin( PLUGIN = GlobalPlugin(
[__initialize], [__initialize],
[__update], [__update],
[], [],
[], [],
) )

View file

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

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

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

View file

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

View file

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