""" Ceci est un exemple de comment l'on peut utiliser le moteur du jeu. """ from engine import * from engine.math import Vec2 from engine.plugins.animation import AnimationPlugin from engine.plugins.clickable import ClickablePlugin from engine.plugins.hover import HoverPlugin, Hoverable from engine.plugins.render import ( Order, RenderPlugin, Position, Texture, ) from engine.plugins.timing import Delta, TimePlugin from engine.plugins.pygame import Display, Keyboard, PygamePlugin # Initialisation game = Game( TimePlugin(), PygamePlugin("Guess The Number"), RenderPlugin(), HoverPlugin(), ClickablePlugin(), AnimationPlugin(), ) # On créer une tache pour afficher des sprites def spawn_sprites(world: World) -> None: """ Ajoute des sprites au monde. """ def new_button(position: Vec2, file_name: str) -> None: world.create_entity( Position(position), Order(0), Texture(file_name + ".png"), Hoverable( entry_callback=lambda _world, entity: entity.set( Texture(file_name + "_hover.png"), ), exit_callback=lambda _world, entity: entity.set( Texture(file_name + ".png"), ), ), ) for i in range(3): if i == 0: file_name = "button_classique" elif i == 1: file_name = "button_histoire" else: file_name = "button_tricheur" new_button(Vec2(Display.WIDTH / 3 * i, 20), file_name) # On ajoutant la tache game.add_startup_tasks(spawn_sprites) 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) # 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) # On lance la boucle game.run()