settings + squash game_over et send_to_server

This commit is contained in:
Raphaël 2024-01-07 20:05:52 +01:00
parent 101188c17e
commit 7a4587ac44
10 changed files with 340 additions and 138 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4 KiB

View file

@ -36,13 +36,6 @@ def __update(world: World):
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

View file

@ -676,7 +676,7 @@ def __animation(world: World, number: int):
world.new_entity().set(
TextBundle(
str(number),
2,
10,
5000,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2),
origin=Vec2(0.5),

View file

@ -1,16 +1,23 @@
from engine import CurrentScene, KeepAlive, Scene
import os
from engine import CurrentScene, KeepAlive, Plugin
from engine.ecs import Entity, World
from engine.math import Vec2
from plugins import render
from plugins import writing
from plugins.click import Clickable
from plugins.hover import HoveredTexture
from plugins.inputs import Pressed
from plugins.render import (
SpriteBundle,
Text,
TextBundle,
)
from plugins.sound import Sound
from scenes import game, send_to_server
from plugins.writing import Writing
from scenes import game, thanks
import requests as rq
IP = "pong.cocosol.fr"
def __spawn_elements(world: World):
@ -21,23 +28,49 @@ def __spawn_elements(world: World):
)
world.new_entity().set(
TextBundle(
"Voulez vous enregistrer votre Score ?",
f"Votre score est de {world[game.Player1Score]}",
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),
if not os.path.exists("username.txt"):
world.new_entity().set(
TextBundle(
"Quel est votre pseudo ?",
0,
50,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2),
origin=Vec2(0.5),
)
)
)
__create_button(world, "continue")
world.new_entity().set(
SpriteBundle(
"background.jpg",
-5,
Vec2(render.WIDTH / 2, render.HEIGHT / 2 + 100),
Vec2(600, 70),
Vec2(0.5),
)
)
world.new_entity().set(
TextBundle(
"",
0,
50,
position=Vec2(render.WIDTH / 2, render.HEIGHT / 2 + 100),
origin=Vec2(0.5),
),
Writing(
"azertyuiopqsdfghjklmwxcvbn0123456789/",
16,
"...",
),
)
__create_button(world, "submit")
def __create_button(world: World, name: str):
@ -55,16 +88,26 @@ def __create_button(world: World, name: str):
f"button_{name}.png",
f"button_{name}_hover.png",
),
Clickable(lambda world, entity: __on_click_butons(world, entity, name)),
Clickable(new_score),
)
def __on_click_butons(world: World, _entity: Entity, name: str):
"""
Fonction qui s'execute quand on clique sur un bouton.
"""
world[CurrentScene] = send_to_server.SEND
world.new_entity().set(KeepAlive(), Sound("click.wav"))
def new_score(world: World, e: Entity):
e.remove(Clickable)
if os.path.exists("username.txt"):
with open("username.txt", "r") as f:
name = f.read()
else:
name = world.query(Writing).pop()
with open("username.txt", "w") as f:
f.write(name[Text])
try:
post = {"name": name, "score": world[game.Player1Score]}
rq.post(f"https://{IP}/new_score", post)
world.set(CurrentScene(thanks.THANKS))
except Exception as error:
print("Error with the serveur:", error)
def __enter_to_submit(world: World):
@ -76,8 +119,11 @@ def __enter_to_submit(world: World):
world.new_entity().set(KeepAlive(), Sound("click.wav"))
GAME_OVER = Scene(
[__spawn_elements],
[__enter_to_submit],
[],
GAME_OVER = (
Plugin(
[__spawn_elements],
[__enter_to_submit],
[],
)
+ writing.PLUGIN
)

View file

@ -12,7 +12,7 @@ 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
from scenes import game, settings
import requests as rq
IP = "pong.cocosol.fr"
@ -76,6 +76,25 @@ def __spawn_elements(world: World):
__spawn_score(world)
world.new_entity().set(
SpriteBundle(
f"button_settings_icon.png",
1,
Vec2(100, 100),
Vec2(100, 100),
Vec2(0.5),
),
HoveredTexture(
f"button_settings_icon.png",
f"button_settings_icon_hover.png",
),
Clickable(__go_to_settings),
)
def __go_to_settings(world: World, _e: Entity):
world[CurrentScene] = settings.SETTINGS
def __spawn_score(world: World):
"""

View file

@ -1,106 +0,0 @@
import os
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.inputs import Pressed
from plugins.render import SpriteBundle, Text, TextBundle
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]}
rq.post(f"https://{IP}/new_score", post)
with open("username.txt", "w") as f:
f.write(name[Text])
world.set(CurrentScene(thanks.THANKS))
except Exception as error:
print("Error with the serveur:", error)
def get_scores():
try:
return rq.get(f"https://{IP}/data").json()
except Exception as error:
print("Error with the serveur:", error)
def __spawn_elements(world: World):
"""
Ajoute les éléments du menu dans le monde.
"""
if os.path.exists("username.txt"):
with open("username.txt", "r") as f:
name = f.read()
post = {"name": name, "score": world[game.Player1Score]}
rq.post(f"https://{IP}/new_score", post)
world.set(CurrentScene(thanks.THANKS))
return
world.new_entity().set(SpriteBundle("background.png", -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, 800),
order=1,
origin=Vec2(0.5),
),
HoveredTexture(
f"button_submit.png",
f"button_submit_hover.png",
),
Clickable(new_score),
)
def __enter_to_submit(world: World):
"""
Envoit le score losre que l'utilisateur appuie sur entrée.
"""
if "return" in world[Pressed] and len(world.query(Clickable)) > 0:
new_score(world, world.query(Clickable).pop())
SEND = (
Plugin(
[__spawn_elements],
[__enter_to_submit],
[],
)
+ writing.PLUGIN
)

250
src/scenes/settings.py Normal file
View file

@ -0,0 +1,250 @@
import random
from engine import CurrentScene, 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.inputs import Held
from plugins.physics import Solid, Velocity
from plugins.render import Origin, Position, Scale, SpriteBundle, TextBundle, Texture
from plugins.timing import Delta
from scenes import menu
class Player1Up(str):
"""
Ressource qui definit la touche pour monter le joueur 1
"""
class Player1Down(str):
"""
Ressource qui definit la touche pour descendre le joueur 1
"""
class Player2Up(str):
"""
Ressource qui definit la touche pour monter le joueur 2
"""
class Player2Down(str):
"""
Ressource qui definit la touche pour descendre le joueur 1
"""
class UpKey(str):
"""
Composant qui indique la touche pour faire monter le joueur
"""
class DownKey(str):
"""
Composant qui indique la touche pour faire descender le joueur
"""
class Speed(int):
"""
Composant qui represente la vitesse de l'entité.
"""
def __spawn_elements(world: World):
world.set(
Player1Up("z"),
Player1Down("s"),
Player2Up("up"),
Player2Down("down"),
)
world.new_entity().set(SpriteBundle(("background.png"), -5))
world.new_entity().set(TextBundle("Commandes", 0, 60, position=Vec2(250, 150)))
world.new_entity().set(TextBundle("Joueur 1", 0, 50, position=Vec2(350, 250)))
world.new_entity().set(TextBundle(f"Monter", 0, 40, position=Vec2(450, 325)))
world.new_entity().set(
TextBundle(
f"{world[Player1Up]}",
0,
40,
position=Vec2(800, 325),
origin=Vec2(1, 0),
)
)
world.new_entity().set(TextBundle(f"Descendre", 0, 40, position=Vec2(450, 375)))
world.new_entity().set(
TextBundle(
f"{world[Player1Down]}",
0,
40,
position=Vec2(800, 375),
origin=Vec2(1, 0),
)
)
world.new_entity().set(TextBundle("Joueur 2", 0, 50, position=Vec2(350, 475)))
world.new_entity().set(TextBundle("Monter", 0, 40, position=Vec2(450, 550)))
world.new_entity().set(
TextBundle(
f"{world[Player2Up]}",
0,
40,
position=Vec2(800, 550),
origin=Vec2(1, 0),
)
)
world.new_entity().set(TextBundle("Descendre", 0, 40, position=Vec2(450, 600)))
world.new_entity().set(
TextBundle(
f"{world[Player2Down]}",
0,
40,
position=Vec2(800, 600),
origin=Vec2(1, 0),
)
)
world.new_entity().set(
SpriteBundle(
f"button_menu_icon.png",
1,
Vec2(100, 100),
Vec2(100, 100),
Vec2(0.5),
),
HoveredTexture(
f"button_menu_icon.png",
f"button_menu_icon_hover.png",
),
Clickable(__go_to_menu),
)
def __move_up(world: World):
"""
La fonction permet de faire bouger les entitees qui possedent UpKey vers le haut.
"""
held = world[Held]
for entity in world.query(UpKey):
if entity[UpKey] in held:
if entity[Position].y > 125 + entity[Scale].y * entity[Origin].y:
entity[Position] = Vec2(
entity[Position].x,
(entity[Position].y - entity[Speed] * world[Delta]),
)
def __move_down(world: World):
"""
La fonction permet de faire bouger les entitees qui possedent DownKey vers le bas.
"""
held = world[Held]
for entity in world.query(DownKey):
if entity[DownKey] in held:
if (
entity[Position].y
< render.HEIGHT - 125 - entity[Scale].y * entity[Origin].y
):
entity[Position] = Vec2(
entity[Position].x,
(entity[Position].y + entity[Speed] * world[Delta]),
)
def __update_move(world: World):
"""
La fontion permet de faire bouger les entitees vers le haut ou vers le bas.
"""
__move_down(world)
__move_up(world)
def __spawn_little_game(world: World):
# Mon mur de gauche
world.new_entity().set(
Origin(Vec2(1, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(150, 0)),
Solid(),
)
# Mon mur du RN
world.new_entity().set(
Origin(Vec2(0, 0)),
Scale(Vec2(10, render.HEIGHT)),
Position(Vec2(render.WIDTH - 150, 0)),
Solid(),
)
# Mon mur du bas
world.new_entity().set(
Origin(Vec2(0, 0)),
Scale(Vec2(render.WIDTH, 10)),
Position(Vec2(0, render.HEIGHT - 125)),
Solid(),
)
# Mon mur du haut
world.new_entity().set(
Origin(Vec2(0, 1)),
Scale(Vec2(render.WIDTH, 10)),
Position(Vec2(0, 125)),
Solid(),
)
# Joueur 1
world.new_entity().set(
SpriteBundle(
"player_1.png",
0,
Vec2(225, render.HEIGHT / 2),
Vec2(44, 250),
Vec2(1, 0.5),
),
UpKey("z"),
DownKey("s"),
Speed(500),
Solid(),
)
# Joueur 2
world.new_entity().set(
SpriteBundle(
"player_2.png",
0,
Vec2(render.WIDTH - 225, render.HEIGHT / 2),
Vec2(44, 250),
Vec2(0, 0.5),
),
UpKey("up"),
DownKey("down"),
Speed(500),
Solid(),
)
velocity = Vec2(2 * random.randint(100, 200), random.randint(100, 200))
world.new_entity().set(
SpriteBundle(
"ball.png",
0,
Vec2(render.WIDTH / 2, render.HEIGHT / 2),
Vec2(40, 40),
Vec2(0.5),
),
Velocity(velocity),
)
def __go_to_menu(world: World, _e: Entity):
world[CurrentScene] = menu.MENU
SETTINGS = Scene(
[__spawn_elements, __spawn_little_game],
[__update_move],
[],
)