gtn/src/main.py

106 lines
2.8 KiB
Python
Raw Normal View History

"""
2023-10-28 00:42:01 +00:00
https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unused-wildcard-import.htmlCeci est un exemple de comment l'on peut utiliser le moteur du jeu.
"""
2023-10-28 00:42:01 +00:00
from engine import Game, World
from engine.math import Vec2
2023-10-27 22:08:34 +00:00
from engine.plugins.animation import AnimationPlugin
from engine.plugins.clickable import ClickablePlugin
2023-10-27 22:00:29 +00:00
from engine.plugins.hover import HoverPlugin, Hoverable
2023-10-26 15:29:15 +00:00
from engine.plugins.render import (
Order,
RenderPlugin,
Position,
Texture,
2023-10-26 15:29:15 +00:00
)
2023-10-27 16:17:29 +00:00
from engine.plugins.timing import Delta, TimePlugin
2023-10-28 00:42:01 +00:00
from engine.plugins.display import Display, Keyboard, DisplayPlugin
2023-10-23 10:14:28 +00:00
# Initialisation
2023-10-27 13:26:37 +00:00
game = Game(
TimePlugin(),
2023-10-28 00:42:01 +00:00
DisplayPlugin("Guess The Number"),
RenderPlugin(),
HoverPlugin(),
ClickablePlugin(),
2023-10-27 16:17:29 +00:00
AnimationPlugin(),
2023-10-27 13:26:37 +00:00
)
# On créer une tache pour afficher des sprites
def spawn_sprites(world: World) -> None:
"""
Ajoute des sprites au monde.
"""
2023-10-27 22:00:29 +00:00
def new_button(position: Vec2, file_name: str) -> None:
world.create_entity(
2023-10-27 22:08:34 +00:00
Position(position),
Order(0),
Texture(file_name + ".png"),
Hoverable(
2023-10-27 22:27:28 +00:00
enter_callback=lambda _world, entity: entity.set(
2023-10-27 22:08:34 +00:00
Texture(file_name + "_hover.png"),
2023-10-27 22:00:29 +00:00
),
2023-10-27 22:08:34 +00:00
exit_callback=lambda _world, entity: entity.set(
Texture(file_name + ".png"),
),
),
)
2023-10-27 22:15:14 +00:00
for i in range(4):
2023-10-27 22:00:29 +00:00
if i == 0:
file_name = "button_classique"
elif i == 1:
2023-10-27 22:08:34 +00:00
file_name = "button_histoire"
2023-10-27 22:15:14 +00:00
elif i == 2:
2023-10-27 22:00:29 +00:00
file_name = "button_tricheur"
2023-10-27 22:15:14 +00:00
else:
file_name = "button_menteur"
new_button(Vec2(Display.WIDTH / 4 * i, 20), file_name)
2023-10-27 16:17:29 +00:00
# On ajoutant la tache
game.add_startup_tasks(spawn_sprites)
2023-10-28 00:42:01 +00:00
def move_sprites(world: World):
"""
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
2023-10-28 00:42:01 +00:00
def salutations(world: World):
"""
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
2023-10-23 11:15:50 +00:00
game.run()