Merge pull request 'Classique' (#35) from Classique into main
Reviewed-on: #35
BIN
assets/krita/search_bar.kra
Normal file
BIN
assets/krita/valider.kra
Normal file
BIN
assets/textures/classique/arrow.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/textures/classique/arrow_hover.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
assets/textures/classique/background.png
Normal file
After Width: | Height: | Size: 143 KiB |
BIN
assets/textures/classique/play_again.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
assets/textures/classique/play_again_hover.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
assets/textures/classique/valider.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
assets/textures/classique/valider_hover.png
Normal file
After Width: | Height: | Size: 21 KiB |
|
@ -4,12 +4,13 @@ Example de l'utilisation du moteur de jeu.
|
||||||
|
|
||||||
|
|
||||||
from engine import start_game
|
from engine import start_game
|
||||||
from scenes import menu
|
from scenes import classique, menu
|
||||||
|
|
||||||
|
|
||||||
start_game(
|
start_game(
|
||||||
{
|
{
|
||||||
"menu": menu.SCENE,
|
"menu": menu.SCENE,
|
||||||
|
"classique": classique.SCENE
|
||||||
},
|
},
|
||||||
"menu",
|
"menu",
|
||||||
title="Guess The Number",
|
title="Guess The Number",
|
||||||
|
|
35
src/plugins/typing.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
"""
|
||||||
|
Definit un plugin qui crée un texte avec les touches frappées
|
||||||
|
"""
|
||||||
|
|
||||||
|
from engine import Keyboard, Scene, Text, World
|
||||||
|
|
||||||
|
|
||||||
|
class Typing(str):
|
||||||
|
"""
|
||||||
|
Marque une entité comme un texte qui s'ecrit en fonction du clavier
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def __update(world: World):
|
||||||
|
"""
|
||||||
|
Met a jour les entitées contenant le composant Typing
|
||||||
|
"""
|
||||||
|
keyboard = world[Keyboard]
|
||||||
|
for entity in world.query(Typing, Text):
|
||||||
|
text = entity[Text]
|
||||||
|
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(
|
||||||
|
[],
|
||||||
|
[__update],
|
||||||
|
[],
|
||||||
|
)
|
207
src/scenes/classique.py
Normal file
|
@ -0,0 +1,207 @@
|
||||||
|
"""
|
||||||
|
Définis la scène du jeu classique, sans variante.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
from plugins import typing
|
||||||
|
from engine import (
|
||||||
|
Centered,
|
||||||
|
Clickable,
|
||||||
|
Color,
|
||||||
|
Display,
|
||||||
|
Game,
|
||||||
|
HoveredTexture,
|
||||||
|
Keyboard,
|
||||||
|
Order,
|
||||||
|
Position,
|
||||||
|
Scene,
|
||||||
|
Text,
|
||||||
|
TextSize,
|
||||||
|
Texture,
|
||||||
|
World,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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("classique/background.png"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Bouton valider/rejouer
|
||||||
|
world.create_entity(
|
||||||
|
Position(Display.WIDTH / 2, 900),
|
||||||
|
Order(1),
|
||||||
|
Centered(),
|
||||||
|
Texture("classique/valider.png"),
|
||||||
|
HoveredTexture("classique/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"),
|
||||||
|
Text(""),
|
||||||
|
Color(61, 22, 58),
|
||||||
|
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(61, 22, 58),
|
||||||
|
Text("Devine le nombre..."),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Text qui affiche le nombre d'essai
|
||||||
|
world.create_entity(
|
||||||
|
Position(Display.WIDTH - 700, 100),
|
||||||
|
Order(4),
|
||||||
|
TextSize(100),
|
||||||
|
NombreEssaiText(),
|
||||||
|
Color(61, 22, 58),
|
||||||
|
Text("il reste : 7 essais"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Bouton pour revenir au menu
|
||||||
|
world.create_entity(
|
||||||
|
Order(11),
|
||||||
|
Position(100, 100),
|
||||||
|
Texture("classique/arrow.png"),
|
||||||
|
Clickable(lambda world, _: world[Game].change_scene("menu")),
|
||||||
|
HoveredTexture("classique/arrow_hover.png"),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Les ressources.
|
||||||
|
world[NombreEssai] = NombreEssai(7)
|
||||||
|
world[RandomNumber] = RandomNumber(random.randint(0, 99))
|
||||||
|
world[IsRunning] = IsRunning()
|
||||||
|
|
||||||
|
|
||||||
|
def _update(world: World):
|
||||||
|
"""
|
||||||
|
Verifie si le nombre donné est le meme que celui que l'on a choisi.
|
||||||
|
Boucle du jeu.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# si le jeu s'est arrete.
|
||||||
|
if IsRunning not in world:
|
||||||
|
# on relance le jeu.
|
||||||
|
world[Game].change_scene("classique")
|
||||||
|
|
||||||
|
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("tu doit entrer 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 ''}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def end_game(world: World, state: str):
|
||||||
|
"""
|
||||||
|
fonction applé quand le jeu est fini.
|
||||||
|
"""
|
||||||
|
del world[IsRunning] # le jeu est fini.
|
||||||
|
|
||||||
|
# On affiche le message de fin.
|
||||||
|
for entity_text in world.query(TextDialogue):
|
||||||
|
entity_text[Text] = Text(f"{state} !")
|
||||||
|
|
||||||
|
# On empeche de pourvoir continuer le jeu.
|
||||||
|
for entity in world.query(typing.Typing, Text):
|
||||||
|
del entity[typing.Typing]
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
# on change la texture du button submit.
|
||||||
|
for entity in world.query(Clickable, Centered):
|
||||||
|
entity[Texture] = Texture("classique/play_again.png")
|
||||||
|
entity[HoveredTexture] = HoveredTexture("classique/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"):
|
||||||
|
_update(world)
|
||||||
|
|
||||||
|
|
||||||
|
SCENE = (
|
||||||
|
Scene(
|
||||||
|
[__initialize_world],
|
||||||
|
[_check_return],
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
+ typing.PLUGIN
|
||||||
|
)
|