diff --git a/assets/textures/button_menu_icon.png b/assets/textures/button_menu_icon.png new file mode 100644 index 0000000..9679078 Binary files /dev/null and b/assets/textures/button_menu_icon.png differ diff --git a/assets/textures/button_menu_icon_hover.png b/assets/textures/button_menu_icon_hover.png new file mode 100644 index 0000000..061f334 Binary files /dev/null and b/assets/textures/button_menu_icon_hover.png differ diff --git a/assets/textures/button_settings_icon.png b/assets/textures/button_settings_icon.png new file mode 100644 index 0000000..51e67b9 Binary files /dev/null and b/assets/textures/button_settings_icon.png differ diff --git a/assets/textures/button_settings_icon_hover.png b/assets/textures/button_settings_icon_hover.png new file mode 100644 index 0000000..4f7f284 Binary files /dev/null and b/assets/textures/button_settings_icon_hover.png differ diff --git a/src/plugins/writing.py b/src/plugins/writing.py index f9cbbbc..2061add 100644 --- a/src/plugins/writing.py +++ b/src/plugins/writing.py @@ -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 diff --git a/src/scenes/game.py b/src/scenes/game.py index 7c2ae00..99f896c 100644 --- a/src/scenes/game.py +++ b/src/scenes/game.py @@ -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), diff --git a/src/scenes/game_over.py b/src/scenes/game_over.py index 1021949..8c7893e 100644 --- a/src/scenes/game_over.py +++ b/src/scenes/game_over.py @@ -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 ) diff --git a/src/scenes/menu.py b/src/scenes/menu.py index 98cee7c..c1dd9f2 100644 --- a/src/scenes/menu.py +++ b/src/scenes/menu.py @@ -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): """ diff --git a/src/scenes/send_to_server.py b/src/scenes/send_to_server.py deleted file mode 100644 index baf5eb3..0000000 --- a/src/scenes/send_to_server.py +++ /dev/null @@ -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 -) diff --git a/src/scenes/settings.py b/src/scenes/settings.py new file mode 100644 index 0000000..ef9cb13 --- /dev/null +++ b/src/scenes/settings.py @@ -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], + [], +)