2023-10-24 09:58:43 +00:00
|
|
|
"""
|
|
|
|
Ceci est un exemple de comment l'on peut utiliser le moteur du jeu.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-10-24 12:11:58 +00:00
|
|
|
from engine import *
|
2023-10-25 15:37:15 +00:00
|
|
|
from engine.math import Vec2
|
2023-10-27 15:17:51 +00:00
|
|
|
from engine.plugins.clickable import Clickable, ClickablePlugin
|
|
|
|
from engine.plugins.hover import Hover, HoverPlugin
|
2023-10-26 15:29:15 +00:00
|
|
|
from engine.plugins.render import (
|
|
|
|
Order,
|
|
|
|
RenderPlugin,
|
|
|
|
Position,
|
2023-10-27 15:17:51 +00:00
|
|
|
Offset,
|
|
|
|
Texture,
|
2023-10-26 15:29:15 +00:00
|
|
|
)
|
2023-10-27 15:17:51 +00:00
|
|
|
from engine.plugins.timing import Time, Delta, TimePlugin
|
2023-10-25 17:41:03 +00:00
|
|
|
from engine.plugins.pygame import Display, Keyboard, PygamePlugin
|
2023-10-25 15:37:15 +00:00
|
|
|
from random import random
|
2023-10-22 17:26:06 +00:00
|
|
|
|
2023-10-23 10:14:28 +00:00
|
|
|
|
2023-10-24 10:59:00 +00:00
|
|
|
# Initialisation
|
2023-10-27 13:26:37 +00:00
|
|
|
game = Game(
|
2023-10-27 15:17:51 +00:00
|
|
|
TimePlugin(),
|
|
|
|
PygamePlugin("Guess The Number"),
|
|
|
|
RenderPlugin(),
|
|
|
|
HoverPlugin(),
|
|
|
|
ClickablePlugin(),
|
2023-10-27 13:26:37 +00:00
|
|
|
)
|
2023-10-25 15:37:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# On créer une tache pour afficher des sprites
|
|
|
|
def spawn_sprites(world: World) -> None:
|
|
|
|
"""
|
|
|
|
Ajoute des sprites au monde.
|
|
|
|
"""
|
|
|
|
for i in range(100):
|
|
|
|
red = random() < 0.1
|
2023-10-27 15:17:51 +00:00
|
|
|
entity = world.create_entity(
|
2023-10-25 15:37:15 +00:00
|
|
|
Position(random() * Display.WIDTH, random() * Display.HEIGHT),
|
2023-10-27 15:17:51 +00:00
|
|
|
Texture("directory.png") if red else Texture("test.png"),
|
2023-10-25 15:37:15 +00:00
|
|
|
Order(1 if red else 0),
|
|
|
|
)
|
2023-10-27 15:17:51 +00:00
|
|
|
if red:
|
|
|
|
entity.set(Clickable(lambda world, entity: print("click")))
|
2023-10-25 15:37:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# On ajoutant la tache
|
|
|
|
game.add_startup_tasks(spawn_sprites)
|
|
|
|
|
|
|
|
|
2023-10-27 15:17:51 +00:00
|
|
|
# On créer un tache pour vérifier le système d'hover
|
|
|
|
def change_on_hover(world: World) -> None:
|
|
|
|
"""
|
|
|
|
Change la texture du sprite quand on passe la souris sur lui.
|
|
|
|
"""
|
|
|
|
for entity in world.query(Position, Texture):
|
|
|
|
if Hover in entity:
|
|
|
|
entity.set(
|
|
|
|
Texture(
|
|
|
|
f"anim/edmond_serch_directory/{(int(world[Time] * 60) % 270):04}.png"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
entity.set(Offset(-48, -176))
|
|
|
|
else:
|
|
|
|
entity.set(Texture("directory.png"))
|
|
|
|
entity.remove(Offset)
|
|
|
|
|
|
|
|
|
|
|
|
# On ajoute la tache
|
|
|
|
game.add_update_tasks(change_on_hover)
|
|
|
|
|
|
|
|
|
2023-10-25 15:37:15 +00:00
|
|
|
def move_sprites(world: World) -> None:
|
|
|
|
"""
|
|
|
|
Change la position des sprites.
|
|
|
|
"""
|
|
|
|
move = Vec2(
|
|
|
|
(-1.0 if world[Keyboard].is_key("q") else 0.0)
|
|
|
|
+ (1.0 if world[Keyboard].is_key("d") else 0.0),
|
|
|
|
(-1.0 if world[Keyboard].is_key("z") else 0.0)
|
|
|
|
+ (1.0 if world[Keyboard].is_key("s") else 0.0),
|
|
|
|
)
|
|
|
|
for entity in world.query(Position):
|
|
|
|
if entity[Order] == 1:
|
|
|
|
continue
|
|
|
|
entity.set(Position(entity[Position] + (move * world[Delta] * 1000.0)))
|
|
|
|
|
|
|
|
|
|
|
|
# On ajoute la tache
|
|
|
|
game.add_update_tasks(move_sprites)
|
2023-10-24 15:02:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
# On créer une tache pour tester si les plugins fonctionnent
|
|
|
|
def salutations(world: World) -> None:
|
|
|
|
"""
|
|
|
|
Affiche "Bonjour" si la touche B est pressé et "Au revoir" si la touche B est relachée.
|
|
|
|
"""
|
|
|
|
if world[Keyboard].is_key_pressed("b"):
|
|
|
|
print("Bonjour")
|
|
|
|
|
|
|
|
if world[Keyboard].is_key_released("b"):
|
|
|
|
print("Au revoir")
|
|
|
|
|
|
|
|
|
|
|
|
# On ajoute la tache de test
|
|
|
|
game.add_update_tasks(salutations)
|
2023-10-24 10:59:00 +00:00
|
|
|
|
|
|
|
# On lance la boucle
|
2023-10-23 11:15:50 +00:00
|
|
|
game.run()
|