-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_func.py
52 lines (40 loc) · 1.64 KB
/
input_func.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
import pygame
import unicodedata
'''
Potential issues:
It seems that the InputFunc class is meant to be used as a utility class,
with all of its methods being static. Therefore, it may be better to declare
them as such by adding the @staticmethod decorator.
The get_key_pressed_if_any method only returns the uppercase representation
of the key pressed, which may not be desirable in all cases. It may be better
to return a tuple containing both the uppercase and lowercase representation
of the key pressed, to allow for more flexibility.
The unicodedata module may not work as expected for all languages and character
sets, so it may be necessary to consider other approaches for filtering input.
'''
class InputFunc:
@staticmethod
def is_exit_event(events):
for event in events:
if event.type == pygame.QUIT:
return True
return False
@staticmethod
def is_escape_event(events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return True
def get_key_pressed_if_any(events):
'''
We only want letters.
unicodedata.category() function returns a string that starts with a letter
indicating the category of the character.
"L" : Letters
"N" : Numbers
'''
for event in events:
if event.type == pygame.KEYDOWN:
if event.unicode and unicodedata.category(event.unicode)[0] in "LN":
return pygame.key.name(event.key).upper()
return False