ecs #58
|
@ -7,7 +7,6 @@ from plugins.inputs import Pressed
|
||||||
from plugins.text import Text
|
from plugins.text import Text
|
||||||
|
|
||||||
|
|
||||||
class Typing:
|
|
||||||
class Typing:
|
class Typing:
|
||||||
"""
|
"""
|
||||||
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
|
||||||
|
@ -17,10 +16,6 @@ class Typing:
|
||||||
self.accepted_chars = accepted_chars
|
self.accepted_chars = accepted_chars
|
||||||
self.max_chars = max_chars
|
self.max_chars = max_chars
|
||||||
|
|
||||||
def __init__(self, accepted_chars: str, max_chars: int = 10) -> None:
|
|
||||||
self.accepted_chars = accepted_chars
|
|
||||||
self.max_chars = max_chars
|
|
||||||
|
|
||||||
|
|
||||||
def __update(world: World):
|
def __update(world: World):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,235 +0,0 @@
|
||||||
"""
|
|
||||||
Définis la scène du jeu en mode tricheur, sans variante.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import random
|
|
||||||
from plugins import typing
|
|
||||||
from engine import (
|
|
||||||
Centered,
|
|
||||||
Clickable,
|
|
||||||
Color,
|
|
||||||
Display,
|
|
||||||
Entity,
|
|
||||||
Game,
|
|
||||||
HoveredTexture,
|
|
||||||
Keyboard,
|
|
||||||
Order,
|
|
||||||
Position,
|
|
||||||
Scene,
|
|
||||||
Sound,
|
|
||||||
Text,
|
|
||||||
TextSize,
|
|
||||||
Texture,
|
|
||||||
World,
|
|
||||||
)
|
|
||||||
|
|
||||||
COLOR_TEXT = Color(135, 53, 27)
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class NombreEssaiText:
|
|
||||||
"""
|
|
||||||
Le component qui affiche le nombre d'essai
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class IsRunning:
|
|
||||||
"""
|
|
||||||
Le component qui indique si le jeu est en cours
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def __initialize_world(world: World):
|
|
||||||
"""
|
|
||||||
Initialise le monde du menu.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Fond d'ecran
|
|
||||||
world.create_entity(
|
|
||||||
Position(),
|
|
||||||
Order(0),
|
|
||||||
Texture("tricheur/background.png"),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Bouton valider/rejouer
|
|
||||||
world.create_entity(
|
|
||||||
Position(Display.WIDTH / 2, 875),
|
|
||||||
Order(1),
|
|
||||||
Centered(),
|
|
||||||
Texture("tricheur/valider.png"),
|
|
||||||
HoveredTexture("tricheur/valider_hover.png"),
|
|
||||||
Clickable(lambda world, _: _update(world)),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Zone de saisie
|
|
||||||
world.create_entity(
|
|
||||||
Position(Display.WIDTH / 2, 750),
|
|
||||||
Order(2),
|
|
||||||
Centered(),
|
|
||||||
typing.Typing("1234567890", 2),
|
|
||||||
Text(""),
|
|
||||||
COLOR_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),
|
|
||||||
COLOR_TEXT,
|
|
||||||
Text("Devine le nombre..."),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Text qui affiche le nombre d'essai
|
|
||||||
world.create_entity(
|
|
||||||
Position(Display.WIDTH / 2 - 100, 150),
|
|
||||||
Order(4),
|
|
||||||
TextSize(100),
|
|
||||||
NombreEssaiText(),
|
|
||||||
COLOR_TEXT,
|
|
||||||
Text("il reste : 10 essais"),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Bouton pour revenir au menu
|
|
||||||
world.create_entity(
|
|
||||||
Order(11),
|
|
||||||
Position(150, 150),
|
|
||||||
Texture("tricheur/arrow.png"),
|
|
||||||
Clickable(on_menu_button),
|
|
||||||
HoveredTexture("tricheur/arrow_hover.png"),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Les ressources.
|
|
||||||
world[NombreEssai] = NombreEssai(10)
|
|
||||||
world[RandomNumber] = RandomNumber(random.randint(0, 99))
|
|
||||||
world[IsRunning] = IsRunning()
|
|
||||||
|
|
||||||
|
|
||||||
def on_menu_button(world: World, entity: Entity):
|
|
||||||
"""
|
|
||||||
Fonction qui s'execute quand on clique sur un bouton.
|
|
||||||
"""
|
|
||||||
world[Game].change_scene("menu")
|
|
||||||
entity[Sound] = Sound("click")
|
|
||||||
|
|
||||||
|
|
||||||
def _update(world: World):
|
|
||||||
"""
|
|
||||||
Verifie si le nombre donné est le meme que celui que l'on a choisi.
|
|
||||||
Boucle du jeu.
|
|
||||||
"""
|
|
||||||
|
|
||||||
world.create_entity(Sound("menu_click.wav"))
|
|
||||||
|
|
||||||
# si le jeu s'est arrete.
|
|
||||||
if IsRunning not in world:
|
|
||||||
# on relance le jeu.
|
|
||||||
world[Game].change_scene("tricheur")
|
|
||||||
|
|
||||||
for entity in world.query(typing.Typing, Text):
|
|
||||||
# One efface le nombre.
|
|
||||||
number: str = entity[Text]
|
|
||||||
entity[Text] = Text("")
|
|
||||||
|
|
||||||
# On gere le l'input de l'utilisateur.
|
|
||||||
for entity_text in world.query(TextDialogue):
|
|
||||||
if number == "": # si il a rien evoyé.
|
|
||||||
entity_text[Text] = Text("Entrez un nombre !")
|
|
||||||
return
|
|
||||||
if world[RandomNumber] == int(number): # si il a trouve le nombre.
|
|
||||||
end_game(world, "Gagné")
|
|
||||||
return
|
|
||||||
elif world[NombreEssai] <= 1: # si il n'a plus d'essai.
|
|
||||||
end_game(world, "Perdu")
|
|
||||||
return
|
|
||||||
elif world[RandomNumber] > int(number): # si le nombre est trop petit.
|
|
||||||
entity_text[Text] = Text("Plus grand...")
|
|
||||||
else: # si le nombre est trop grand.
|
|
||||||
entity_text[Text] = Text("Plus petit...")
|
|
||||||
|
|
||||||
# on update l'affichage du nombre d'essai.
|
|
||||||
world[NombreEssai] = NombreEssai(world[NombreEssai] - 1)
|
|
||||||
for entity in world.query(NombreEssaiText):
|
|
||||||
entity[Text] = Text(
|
|
||||||
f"il reste : {world[NombreEssai]} essai{'s' if world[NombreEssai] != 1 else ''}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# ajout de la mechanique de triche
|
|
||||||
world[RandomNumber] = max(
|
|
||||||
0, min(99, RandomNumber(world[RandomNumber] + random.randint(-3, 3)))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def end_game(world: World, state: str):
|
|
||||||
"""
|
|
||||||
fonction applé quand le jeu est fini.
|
|
||||||
"""
|
|
||||||
del world[IsRunning] # le jeu est fini.
|
|
||||||
|
|
||||||
# On joue le son
|
|
||||||
if state == "Gagné":
|
|
||||||
world.create_entity(Sound("win_sound.wav"))
|
|
||||||
else:
|
|
||||||
world.create_entity(Sound("lose_sound.wav"))
|
|
||||||
|
|
||||||
# On affiche le message de fin.
|
|
||||||
for entity_text in world.query(TextDialogue):
|
|
||||||
entity_text[Text] = Text(f"{state} !")
|
|
||||||
|
|
||||||
if state == "Gagné":
|
|
||||||
for entity in world.query(NombreEssaiText):
|
|
||||||
entity[Text] = Text("")
|
|
||||||
else:
|
|
||||||
for entity in world.query(NombreEssaiText):
|
|
||||||
entity[Text] = Text(" plus d'essais")
|
|
||||||
for entity in world.query(typing.Typing):
|
|
||||||
entity[Position] = Position(Display.WIDTH / 2, 650)
|
|
||||||
entity[Text] = Text("Le nombre etait : " + str(world[RandomNumber]))
|
|
||||||
|
|
||||||
# On empeche de pourvoir continuer le jeu.
|
|
||||||
for entity in world.query(typing.Typing, Text):
|
|
||||||
del entity[typing.Typing]
|
|
||||||
|
|
||||||
# on change la texture du button submit.
|
|
||||||
for entity in world.query(Clickable, Centered):
|
|
||||||
entity[Texture] = Texture("tricheur/play_again.png")
|
|
||||||
entity[HoveredTexture] = HoveredTexture("tricheur/play_again_hover.png")
|
|
||||||
|
|
||||||
|
|
||||||
def _check_return(world: World):
|
|
||||||
"""
|
|
||||||
Verifie si la touche entrée est appuyée.
|
|
||||||
"""
|
|
||||||
keyboard = world[Keyboard]
|
|
||||||
if keyboard.is_key_pressed("return") or keyboard.is_key_pressed("enter"):
|
|
||||||
_update(world)
|
|
||||||
|
|
||||||
|
|
||||||
SCENE = (
|
|
||||||
Scene(
|
|
||||||
[__initialize_world],
|
|
||||||
[_check_return],
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
+ typing.PLUGIN
|
|
||||||
)
|
|
Loading…
Reference in a new issue