-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbergame.py
executable file
·84 lines (70 loc) · 2.18 KB
/
numbergame.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
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import argparse
import colorsys
import random
import sys
from collections.abc import Iterator
import pygame
from espeakng import ESpeakNG
def number_gen(m: int, n: int, shuffle: bool) -> Iterator[int]:
nums = list(range(m, n + 1))
if shuffle:
random.shuffle(nums)
for i in nums:
yield i
def next_color():
return [int(x * 255) for x in colorsys.hls_to_rgb(random.random(), 0.6, 1)]
def speak(n):
speech.say(str(n), sync=True)
parser = argparse.ArgumentParser()
parser.add_argument("--min", "-m", type=int, default=0)
parser.add_argument("--max", "-n", type=int, default=9999)
parser.add_argument("--shuffle", "-s", action="store_true")
args = parser.parse_args()
pygame.init()
size = width, height = 720, 360
black = 0, 0, 0
white = 255, 255, 255
speech = ESpeakNG()
speech.voice = "fr-be"
speech.speed = 120
speech.pitch = 75
screen = pygame.display.set_mode(size)
pygame.display.toggle_fullscreen()
pygame.display.set_caption("Numbers game")
font = pygame.font.SysFont("liberationsans", 256, True, False)
gen = number_gen(args.min, args.max, args.shuffle)
n = next(gen)
color = next_color()
done = False
while not done:
played = False
for event in pygame.event.get():
if played:
continue
if event.type == pygame.QUIT or (
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
):
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
speak(n)
color = next_color()
try:
n = next(gen)
except StopIteration:
done = True
break
played = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
speak(n)
played = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
pygame.display.toggle_fullscreen()
played = True
text = font.render(str(n), True, color, black)
textRect = text.get_rect()
textRect.center = (width // 2, height // 2)
screen.fill(black)
screen.blit(text, textRect)
pygame.display.flip()