Ajout de la detection de cliques à l'écran
This commit is contained in:
parent
910ef1039b
commit
f2fa0dd81f
|
@ -1,4 +1,5 @@
|
||||||
import math
|
import math
|
||||||
|
from types import FunctionType
|
||||||
|
|
||||||
from pygame import event
|
from pygame import event
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
@ -8,9 +9,21 @@ import src.engine.engine as engine
|
||||||
|
|
||||||
class EventHandler:
|
class EventHandler:
|
||||||
"""Classe utilisée pour traiter les pygame.event.get() et gérer les interactions avec le reste du programme."""
|
"""Classe utilisée pour traiter les pygame.event.get() et gérer les interactions avec le reste du programme."""
|
||||||
|
|
||||||
def __init__(self, core: 'engine.Engine'):
|
def __init__(self, core: 'engine.Engine'):
|
||||||
self.engine = core
|
self.engine = core
|
||||||
self.key_pressed = []
|
self.key_pressed = []
|
||||||
|
self.buttons_area = []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_click_collision(rect: tuple[int, int, int, int], point: tuple[int, int]):
|
||||||
|
"""Vérifie si le point et le rectangle donné sont en collision."""
|
||||||
|
return rect[0] < point[0] < rect[0] + rect[2] and rect[1] < point[1] < rect[1] + rect[3]
|
||||||
|
|
||||||
|
def register_button_area(self, rect: tuple[int, int, int, int], callback: FunctionType | classmethod | staticmethod):
|
||||||
|
"""Enregistre une zone comme bouton. La fonction donnée sera donc executé lorsque la zone sur la fenêtre
|
||||||
|
sera cliqué."""
|
||||||
|
self.buttons_area.append((rect, callback))
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Vérifie s'il y a de nouvelles interactions et les traites."""
|
"""Vérifie s'il y a de nouvelles interactions et les traites."""
|
||||||
|
@ -24,6 +37,12 @@ class EventHandler:
|
||||||
elif e.type == KEYUP:
|
elif e.type == KEYUP:
|
||||||
if e.key in self.key_pressed:
|
if e.key in self.key_pressed:
|
||||||
self.key_pressed.remove(e.key)
|
self.key_pressed.remove(e.key)
|
||||||
|
elif e.type == MOUSEBUTTONDOWN:
|
||||||
|
# Vérifie si une des zones enregistrées comme bouton n'a pas été cliqué
|
||||||
|
if e.button == 1:
|
||||||
|
for area in self.buttons_area:
|
||||||
|
if self.get_click_collision(area[0], e.pos):
|
||||||
|
area[1]()
|
||||||
|
|
||||||
if self.engine.entity_manager.player_entity_name:
|
if self.engine.entity_manager.player_entity_name:
|
||||||
if K_RIGHT in self.key_pressed:
|
if K_RIGHT in self.key_pressed:
|
||||||
|
@ -44,8 +63,8 @@ class EventHandler:
|
||||||
self.engine.entity_manager.get_by_name("player").take_damages(1)
|
self.engine.entity_manager.get_by_name("player").take_damages(1)
|
||||||
if K_p in self.key_pressed:
|
if K_p in self.key_pressed:
|
||||||
self.engine.renderer.emit_particles(math.floor(self.engine.entity_manager.get_by_name("player").x),
|
self.engine.renderer.emit_particles(math.floor(self.engine.entity_manager.get_by_name("player").x),
|
||||||
math.floor(self.engine.entity_manager.get_by_name("player").y),
|
math.floor(self.engine.entity_manager.get_by_name("player").y),
|
||||||
16, 16, 16, 1, 8, 0, 1, 0.2, 1., (0, 200, 200))
|
16, 16, 16, 1, 8, 0, 1, 0.2, 1., (0, 200, 200))
|
||||||
if K_o in self.key_pressed:
|
if K_o in self.key_pressed:
|
||||||
print(f"Player pos: X = {self.engine.entity_manager.get_by_name('player').x} "
|
print(f"Player pos: X = {self.engine.entity_manager.get_by_name('player').x} "
|
||||||
f"Y = {self.engine.entity_manager.get_by_name('player').y}")
|
f"Y = {self.engine.entity_manager.get_by_name('player').y}")
|
||||||
|
@ -54,4 +73,3 @@ class EventHandler:
|
||||||
self.engine.camera.target_zoom *= 1.01
|
self.engine.camera.target_zoom *= 1.01
|
||||||
if K_c in self.key_pressed:
|
if K_c in self.key_pressed:
|
||||||
self.engine.camera.target_zoom *= 0.99
|
self.engine.camera.target_zoom *= 0.99
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,8 @@ class Game(Engine):
|
||||||
|
|
||||||
self.renderer.dialogs_box = pygame.image.load("assets/textures/GUI/dialogs_box.png").convert_alpha()
|
self.renderer.dialogs_box = pygame.image.load("assets/textures/GUI/dialogs_box.png").convert_alpha()
|
||||||
|
|
||||||
|
self.event_handler.register_button_area((0, 0, 20, 20), lambda : print("salut"))
|
||||||
|
|
||||||
def create_player_entity(self):
|
def create_player_entity(self):
|
||||||
"""Crée une entité joueur."""
|
"""Crée une entité joueur."""
|
||||||
anim = Anim(0.5)
|
anim = Anim(0.5)
|
||||||
|
|
Loading…
Reference in a new issue