-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimestable.py
executable file
·122 lines (103 loc) · 3.3 KB
/
timestable.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2024 C C Magnus Gustavsson
# Released under the GNU General Public License
"""Simple tool to practice the multiplication table.
Every number from 2 to 9 occurs LENGTH times and total time is measured.
Escape to quit.
"""
from random import shuffle
from time import sleep, time
import sys
import pygame
LENGTH = 2
# Define the colors (RGB)
BLACK = ( 16, 16, 16)
BLUE = (128, 128, 192)
GREEN = (128, 192, 128)
RED = (192, 96, 96)
WHITE = (255, 255, 255)
# Initialize graphics
SCREEN = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# Set sizes
INFO = pygame.display.Info()
WIDTH, HEIGHT = INFO.current_w, INFO.current_h
CENTER = WIDTH // 2, HEIGHT // 2
# Initialize pygame
pygame.init()
# Create font
FONT_SIZE = min(WIDTH, HEIGHT) // 4
FONT = pygame.font.Font('freesansbold.ttf', FONT_SIZE)
def get_key(any_key=False):
"""Get a numeric key, or any key."""
while True:
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if any_key:
return None
if event.type == pygame.TEXTINPUT:
if event.text.isdigit():
return event.text
def show(text, background_color=WHITE):
"""Write text on the screen."""
image = FONT.render(text, True, BLACK)
rect = image.get_rect()
rect.center = CENTER
SCREEN.fill(background_color)
SCREEN.blit(image, rect)
pygame.display.flip()
def show_task(first, second, result, color=WHITE):
"""Write task text on the screen."""
text = f'{first} × {second} = {result if result else "?"}'
show(text.ljust(10), color)
def show_sequence(seq, color=WHITE, time_show=1, time_between=0.2):
"""Show a sequence on the screen, one element at a time."""
item = None
for item in seq:
show(str(item), color)
sleep(time_show)
show(None, color)
sleep(time_between)
show(str(item), color)
def get_numbers():
"""Get a list of numbers from 2 to 9."""
return LENGTH * list(range(2, 10))
def get_pairs():
"""Get a list of unique number pairs in random order."""
first_list = get_numbers()
shuffle(first_list)
second_list = get_numbers()
size = 0
# Until the sizes match the pairs are not unique
while size < len(first_list):
shuffle(second_list)
pairs = set(zip(first_list, second_list))
size = len(pairs)
return list(pairs)
def do_tasks():
"""Get correct answers for each task and show total time."""
start = time()
for first, second in get_pairs():
answer = str(first * second)
correct = False
entered = ''
while not correct:
show_task(first, second, entered)
entered += get_key()
if len(entered) == len(answer):
if entered == answer:
correct = True
show_task(first, second, entered, GREEN)
else:
show_task(first, second, entered, RED)
sleep(1)
entered = ''
duration = time() - start
show_sequence(2 * [f'{duration:.1f} s'], BLUE)
show_sequence([3, 2, 1], BLUE)
while True:
do_tasks()
get_key(any_key=True)