-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelisa_4_keymap.py
76 lines (58 loc) · 1.91 KB
/
elisa_4_keymap.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# auth: christian bitter
# desc: in this tutorial we register key events and display some text accordingly
# specifically, we are going to listen for the 4 arrow keys
import pygame
from pygame.locals import K_LEFT, K_RIGHT, K_DOWN, K_UP, QUIT
if not pygame.font:
print("Pygame - fonts not loaded")
if not pygame.mixer:
print("Pygame - audio not loaded")
def update_keytext(pressed, keymap):
if not pressed:
return "None"
txt = ""
if keymap[K_UP]:
txt += "UP"
if keymap[K_DOWN]:
txt += " DOWN"
if keymap[K_LEFT]:
txt += " LEFT"
if keymap[K_RIGHT]:
txt += " RIGHT"
return txt.lstrip()
# init pygame - create the main window, and a background surface
pygame.init()
w, h, t = 640, 480, "Elisa 4 - key map and input handling"
c_white = (250, 250, 250)
c_blue = (0, 0, 255)
screen_buffer = pygame.display.set_mode(size=(w, h))
pygame.display.set_caption(t)
pygame.mouse.set_visible(True)
back_buffer: pygame.Surface = pygame.Surface(screen_buffer.get_size())
back_buffer = back_buffer.convert()
back_buffer.fill(c_white)
# FPS watcher
fps_watcher = pygame.time.Clock()
is_done = False
# Display some text
font = pygame.font.Font(None, 36)
text = font.render("You pressed: <PLACEHOLDER>", 1, c_blue)
key_map = pygame.key.get_pressed()
pressed = False
while not is_done:
fps_watcher.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
is_done = True
else:
pressed = event.type == pygame.KEYDOWN
key_map = pygame.key.get_pressed()
text_help = font.render("Press any of the arrow keys ...", 1, c_blue)
text = font.render(
"You pressed: {}".format(update_keytext(pressed, key_map)), 1, c_blue
)
back_buffer.fill(c_white)
back_buffer.blit(text_help, (100, 100))
back_buffer.blit(text, (100, 150))
screen_buffer.blit(back_buffer, (0, 0))
pygame.display.flip()