From 95534b8aa7bc5d59ccdade950f9d966a02f74ed8 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 15:46:50 +0100 Subject: [PATCH] WIP: update --- src/plugins/typing.py | 12 ++++-- src/scenes/classique.py | 92 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 13 deletions(-) diff --git a/src/plugins/typing.py b/src/plugins/typing.py index a030329..761fa09 100644 --- a/src/plugins/typing.py +++ b/src/plugins/typing.py @@ -5,7 +5,7 @@ Definit un plugin qui crée un texte avec les touches frappées from engine import Keyboard, Scene, Text, World -class Typing: +class Typing(str): """ Marque une entité comme un texte qui s'ecrit en fonction du clavier """ @@ -18,8 +18,14 @@ def __update(world: World): keyboard = world[Keyboard] for entity in world.query(Typing, Text): text = entity[Text] - pressed = keyboard.pressed - + for key in keyboard.pressed: + if key == "backspace": + text = text[:-1] + if key.startswith("["): # pavé numerique + key = key[1] + if key in entity[Typing]: + text += key + entity[Text] = Text(text) PLUGIN = Scene( diff --git a/src/scenes/classique.py b/src/scenes/classique.py index 29f27f8..48b2e48 100644 --- a/src/scenes/classique.py +++ b/src/scenes/classique.py @@ -2,10 +2,14 @@ Définis la scène du jeu classique, sans variante. """ +import random from plugins import typing from engine import ( Centered, + Clickable, + Display, HoveredTexture, + Keyboard, Order, Position, Scene, @@ -16,35 +20,103 @@ from engine import ( ) +class RandomNumber(int): + """ + La ressource qui est le nombre a deviner. + """ + + +class TextDialogue: + """ + Le component qui declare l'entitee Text qui affiche le plus petit ou le plus grand + """ + + +class NombreEssai(int): + """ + Le component qui declare le nombre d'essai + """ + + def __initialize_world(world: World): """ Initialise le monde du menu. """ world.create_entity( - Position(1215, 964), + Position(Display.WIDTH / 2, 964), Order(1), Centered(), Texture("classique/valider.png"), HoveredTexture("classique/valider_hover.png"), + Clickable(lambda world, _: check_number(world)), ) + world.create_entity( - Position(519, 964), - Order(1), - Centered(), - Texture("classique/search_bar.png"), - ) - world.create_entity( - Position(50, 750), + Position(Display.WIDTH / 2, 750), Order(2), - Text("Salut"), + Centered(), + typing.Typing("1234567890"), + Text(""), TextSize(150), ) + # Text qui dit si ton nombre et trop grand ou trop petit + world.create_entity( + Position(Display.WIDTH / 2, 500), + Order(3), + Centered(), + TextDialogue(), + TextSize(150), + Text("Devine le nombre..."), + ) + + # Text qui affiche le nombre d'essai + world.create_entity( + Position(50, 300), + Order(4), + Centered(), + TextSize(100), + Text("il reste : 7 essai"), + ) + + world[NombreEssai] = NombreEssai(7) + world[RandomNumber] = RandomNumber(random.randint(0, 99)) + + +def check_number(world: World): + """ + Verifie si le nombre donné est le meme que celui que l'on a choisi. + """ + for entity in world.query(typing.Typing, Text): + for entity_text in world.query(TextDialogue): + number: str = entity[Text] + if number == "": + entity_text[Text] = Text("tu doit entrer un nombre !") + break + if world[RandomNumber] == int(number): + entity_text[Text] = Text("Win!") + elif world[RandomNumber] > int(number): + entity_text[Text] = Text("Trop petit") + else: + entity_text[Text] = Text("Trop grand") + + # One efface le nombre. + entity[Text] = Text("") + + +def __check_return(world: World): + """ + Verifie si la touche return est appuyée. + """ + keyboard = world[Keyboard] + if keyboard.is_key_pressed("return"): + check_number(world) + SCENE = ( Scene( [__initialize_world], - [], + [__check_return], [], ) + typing.PLUGIN