-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_listener.py
52 lines (39 loc) · 1.39 KB
/
event_listener.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import Optional
import pygame
from config.settings import (
ENTERED_WRONG_ANSWER,
MATCH_DETECTED,
RESUME_GAME,
START_GAME,
)
from models.game_types import GameAction
class EventListener():
""" Handles events in the game. """
def process_events(self, events: list[pygame.event.Event]) -> Optional[GameAction]:
""" Primary method that listens to and processes a list of events and returns a game action.
Args:
events (list[pygame.event.Event]): A list of pygame events.
"""
for event in events:
if (event.type == pygame.ACTIVEEVENT):
if (event.gain == 1):
return GameAction.MOUSE_ENTERED_WINDOW
else:
return GameAction.MOUSE_EXITED_WINDOW
if event.type == pygame.QUIT:
return GameAction.QUIT
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return GameAction.OPEN_MENU
# on left click
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
return GameAction.ITEM_SELECTED
if event.type == MATCH_DETECTED:
return GameAction.MATCH_DETECTED
if event.type == ENTERED_WRONG_ANSWER:
return GameAction.WRONG_ITEM_SELECTED
if event.type == START_GAME:
return GameAction.START_NEW_GAME
if event.type == RESUME_GAME:
return GameAction.RESUME_GAME