-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_handler.py
38 lines (34 loc) · 1.2 KB
/
event_handler.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
import pygame
import json
def parse(events):
parsed_events = {"left": False,
"right": False,
"up": False,
"down": False
}
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
parsed_events["up"] = True
elif event.key == pygame.K_DOWN:
parsed_events["down"] = True
elif event.key == pygame.K_LEFT:
parsed_events["left"] = True
elif event.key == pygame.K_RIGHT:
parsed_events["right"] = True
parsed_events["mouse_pos"] = {"x": pygame.mouse.get_pos()[0],
"y": pygame.mouse.get_pos()[1]
}
parsed = json.dumps(parsed_events)
with open("events.json", "w") as json_file:
json_file.write(parsed)
def unparse():
with open("events.json", "r") as json_file:
try:
return json.load(json_file)
except ValueError:
return {"left": False,
"right": False,
"up": False,
"down": False
}