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
lancement du jeu et le temps depuis la dernière frame.
"""
from time import time
from typing import Callable
from engine import GlobalPlugin, KeepAlive
from engine.ecs import Entity, World
class GlobalTime(KeepAlive, float):
"""
Ressource qui représente le temps global de l'ordinateur sur lequel tourne le jeu.
"""
class Time(KeepAlive, float):
"""
Ressource qui représente le temps depuis le lancement du jeu.
"""
class Delta(KeepAlive, float):
"""
Ressource qui détermine le temps depuis la première frame.
"""
def __initialize(world: World):
"""
Initialise les ressources pour la gestion du temps.
"""
world.set(GlobalTime(time()), Time(0.0))
class TimedEvent:
"""
Composant permettant d'executer un callback après un certain temps.
"""
def __init__(self, timer: float, callback: Callable[[World, Entity], object]):
self.timer = timer
self.callback = callback
def __update(world: World):
"""
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],
[],
[],
)
"""
Un plugin permettant de connaitre le temps depuis le
lancement du jeu et le temps depuis la dernière frame.
"""
from time import time
from typing import Callable
from engine import GlobalPlugin, KeepAlive
from engine.ecs import Entity, World
class GlobalTime(KeepAlive, float):
"""
Ressource qui représente le temps global de l'ordinateur sur lequel tourne le jeu.
"""
class Time(KeepAlive, float):
"""
Ressource qui représente le temps depuis le lancement du jeu.
"""
class Delta(KeepAlive, float):
"""
Ressource qui détermine le temps depuis la première frame.
"""
def __initialize(world: World):
"""
Initialise les ressources pour la gestion du temps.
"""
world.set(GlobalTime(time()), Time(0.0))
class TimedEvent:
"""
Composant permettant d'executer un callback après un certain temps.
"""
def __init__(self, timer: float, callback: Callable[[World, Entity], object]):
self.timer = timer
self.callback = callback
def __update(world: World):
"""
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],
[],
[],
)

View file

@ -1,63 +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],
[],
)
"""
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

View file

@ -1,81 +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],
[],
[],
)
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,107 +1,107 @@
"""
La scène du menu principal du jeu.
Dans cette scène nous pouvons choisir le mode de jeu.
"""
from plugins.sound import Loop, 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 plugins.timing import Time
from scenes import game
import requests as rq
IP = "pong.cocosol.fr"
def get_scores() -> list[tuple[int, str]]:
try:
return rq.get(f"https://{IP}/data").json()
except:
print("Error with the serveur")
return [(1, "")]
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, 11 * render.HEIGHT / 16),
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 "one_player":
world[CurrentScene] = game.ONE_PLAYER
case "two_player":
world[CurrentScene] = game.TWO_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.
"""
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],
[],
[],
)
"""
La scène du menu principal du jeu.
Dans cette scène nous pouvons choisir le mode de jeu.
"""
from plugins.sound import Loop, 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 plugins.timing import Time
from scenes import game
import requests as rq
IP = "pong.cocosol.fr"
def get_scores() -> list[tuple[int, str]]:
try:
return rq.get(f"https://{IP}/data").json()
except:
print("Error with the serveur")
return [(1, "")]
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, 11 * render.HEIGHT / 16),
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 "one_player":
world[CurrentScene] = game.ONE_PLAYER
case "two_player":
world[CurrentScene] = game.TWO_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.
"""
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

@ -1,93 +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
)
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
)

View file

@ -1,37 +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],
[],
[],
)
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],
[],
[],
)

View file

@ -1,73 +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],
[],
[],
)
"""
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],
[],
[],
)