ecs #58

Merged
raphael merged 70 commits from ecs into main 2023-11-03 15:29:36 +00:00
Showing only changes of commit 9294292365 - Show all commits

275
src/scenes/basic_game.py Normal file
View file

@ -0,0 +1,275 @@
"""
Définit 3 scènes de jeu basique
"""
from enum import Enum
import random
import pygame
from engine import CurrentScene, Scene, Plugin
from engine.ecs import World
from engine.math import Vec2
from plugins import assets
from plugins import render
from plugins import writing
from plugins.assets import Assets
from plugins.hover import HoveredTexture
from plugins.inputs import Pressed
from plugins.render import Sprite
from plugins.text import Text
from plugins.writing import Writing
from plugins.click import Clickable
from scenes import menu
class GameMode(Enum):
"""
Definit un ressource represantant le mode de jeu actuel.
"""
CLASSIC = 0
LIAR = 1
CHEATER = 2
class RemainingAttempts(int):
"""
Definit une ressources qui represente le nombre d'essais restants du joueur
"""
class Attempts:
"""
Definit un composant qui representes le texte du nombre d'essai
"""
class Response:
"""
Definit un composant qui representes le texte de reponse
"""
class Number(int):
""" "
Definit une ressource contenant le nombre a trouver
"""
class IsRunning:
"""
Ressource qui defint si le jeux est en cour ou non
"""
class PlayAgain:
"""
Definit un composant qui identifie le bouton Valider/PlayAgain
"""
def __initialize_world(world: World):
world.set(Number(random.randint(0, 99)))
world.set(IsRunning())
print(world[Number])
world.new_entity().set(Sprite(world[Assets].get_texture("background")))
world.new_entity().set(
Sprite(world[Assets].get_texture("arrow"), Vec2(150), 0),
HoveredTexture(
world[Assets].get_texture("arrow"), world[Assets].get_texture("arrow_hover")
),
Clickable(lambda world, entity: world.set(CurrentScene(menu.SCENE))),
)
world.new_entity().set(
Sprite(
world[Assets].get_texture("valider"),
Vec2(render.WIDTH / 2, 850),
0,
origin=Vec2(0.5),
),
HoveredTexture(
world[Assets].get_texture("valider"),
world[Assets].get_texture("valider_hover"),
),
Clickable(lambda world, entity: __update(world)),
PlayAgain(),
)
match world[GameMode]:
case GameMode.CLASSIC:
world.set(RemainingAttempts(7))
text_color = pygame.Color(64, 37, 146)
case GameMode.LIAR:
world.set(RemainingAttempts(15))
text_color = pygame.Color(57, 160, 0)
case GameMode.CHEATER:
world.set(RemainingAttempts(10))
text_color = pygame.Color(133, 51, 25)
match world[GameMode]:
case GameMode.CLASSIC:
world.set(RemainingAttempts(7))
case GameMode.LIAR:
world.set(RemainingAttempts(15))
case GameMode.CHEATER:
world.set(RemainingAttempts(10))
world.new_entity().set(
Text(
f"Il reste : {world[RemainingAttempts]} essais",
100,
text_color,
Vec2(650, 150),
),
Attempts(),
)
world.new_entity().set(
Text(
"Devines le nombre !",
150,
text_color,
Vec2(render.WIDTH / 2, 450),
Vec2(0.5),
),
Response(),
)
world.new_entity().set(
Text(
"",
150,
text_color,
Vec2(render.WIDTH / 2, 650),
Vec2(0.5),
),
Writing("0123456789", 2),
)
def __update(world: World):
# Gestion du pluriel pour le nombre d'essais restants
pluriel = "s"
if int(world[RemainingAttempts]) == 2:
pluriel: str = ""
else:
pluriel = "s"
lying = False
if world[GameMode] == GameMode.LIAR:
lie = random.randint(1, 4)
if lie == 4:
lying = True
else:
lying = False
print(lying)
# Si le jeu est fini
if IsRunning not in world:
# On relance la scene/jeu
match world[GameMode]:
case GameMode.CLASSIC:
world.set(CurrentScene(CLASSIC))
case GameMode.LIAR:
world.set(CurrentScene(LIAR))
case GameMode.CHEATER:
world.set(CurrentScene(CHEATER))
for entity in world.query(Text, Writing):
if entity[Text].text != "":
if int(entity[Text].text) == world[Number]:
for response in world.query(Text, Response):
response[Text].text = "Gagné !"
entity[Text].text = ""
del world[IsRunning]
del entity[Writing]
elif int(entity[Text].text) > world[Number]:
if world[GameMode] != GameMode.LIAR or not lying:
for response in world.query(Text, Response):
response[Text].text = "Plus petit"
else:
for response in world.query(Text, Response):
response[Text].text = "Plus grand"
world[RemainingAttempts] -= 1
for attempts in world.query(Text, Attempts):
attempts[
Text
].text = f"Il reste : {world[RemainingAttempts]} essai{pluriel}"
entity[Text].text = ""
else:
if world[GameMode] != GameMode.LIAR or not lying:
for response in world.query(Text, Response):
response[Text].text = "Plus grand"
else:
for response in world.query(Text, Response):
response[Text].text = "Plus petit"
world[RemainingAttempts] -= 1
for attempts in world.query(Text, Attempts):
attempts[
Text
].text = f"Il reste : {world[RemainingAttempts]} essai{pluriel}"
entity[Text].text = ""
else:
for response in world.query(Text, Response):
response[Text].text = "Veuillez entrer un nombre."
if world[RemainingAttempts] == 0:
for response in world.query(Text, Response):
response[Text].text = "Perdu !"
entity[Text].text = f"Le nombre etait {world[Number]}"
for play_again in world.query(PlayAgain):
play_again[Sprite].texture = world[Assets].get_texture("play_again")
play_again[HoveredTexture].normal = world[Assets].get_texture(
"play_again"
)
play_again[HoveredTexture].hovered = world[Assets].get_texture(
"play_again_hover"
)
del entity[Writing]
del world[IsRunning]
if world[GameMode] == GameMode.CHEATER:
world[Number] += random.randint(-3, 3)
__SCENE = (
Scene(
[__initialize_world],
[__detect_keys],
[],
)
+ writing.PLUGIN
)
CLASSIC = assets.loading_scene(
Plugin(
[lambda world: world.set(GameMode.CLASSIC)],
[],
[],
)
+ __SCENE,
"base_game/classic",
)
LIAR = assets.loading_scene(
Plugin(
[lambda world: world.set(GameMode.LIAR)],
[],
[],
)
+ __SCENE,
"base_game/liar",
)
CHEATER = assets.loading_scene(
Plugin(
[lambda world: world.set(GameMode.CHEATER)],
[],
[],
)
+ __SCENE,
"base_game/cheater",
)