-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_keys.py
56 lines (49 loc) · 1.82 KB
/
get_keys.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
import pygame
import config as cfg
def get_player_keys(num_players, screen, color=cfg.white, font=cfg.font):
player_keys = []
for i in range(cfg.num_ai_players, num_players):
keys = {}
keys["left"] = None
keys["right"] = None
while not all(keys.values()):
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if not keys["left"]:
keys["left"] = pygame.key.name(event.key)
elif not keys["right"]:
keys["right"] = pygame.key.name(event.key)
# Draw screen
screen.fill(color)
text = font.render(f"Player {i+1}, select keys:", True, (0, 0, 0))
screen.blit(
text,
(
cfg.screen_width / 2 - text.get_width() / 2,
cfg.screen_height / 2 - text.get_height() / 2 - 50,
),
)
# get up and down keys
text = font.render(f"Left: {keys['left']}", True, (0, 0, 0))
screen.blit(
text,
(
cfg.screen_width / 2 - text.get_width() / 2,
cfg.screen_height / 2 - text.get_height() / 2 + 20,
),
)
text = font.render(f"Right: {keys['right']}", True, (0, 0, 0))
screen.blit(
text,
(
cfg.screen_width / 2 - text.get_width() / 2,
cfg.screen_height / 2 - text.get_height() / 2 + 50,
),
)
pygame.display.update()
player_keys.append(keys)
return player_keys