-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.py
39 lines (28 loc) · 1.05 KB
/
eventhandler.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
import pygame as pyg
class EventHandler():
def __init__(self, game_mode, keyaction, quit_callback):
self.quit_game = quit_callback
self.game_mode = game_mode
self.event_mapping = {
pyg.QUIT: self.event_quit,
pyg.KEYDOWN: self.event_keydown,
pyg.KEYUP: self.event_keyup
}
self.keyaction = keyaction
def handle_events(self, pygame_event):
""" Handles all game events. """
for event in pygame_event.get():
event_type = event.type
if event_type in self.event_mapping:
# Callback has supplied arguments.
event_callback = self.event_mapping[event_type]
event_callback(event)
def event_quit(self, _):
""" Quits the game. """
self.quit_game()
def event_keydown(self, event):
""" Hanldes key down events. """
self.keyaction.keydown_action(event)
def event_keyup(self, event):
""" Handles key up events. """
self.keyaction.keyup_action(event)