Last active
June 3, 2021 17:54
-
-
Save slabko/2168039a5dbd4ab2bcf04c74c876fb5c to your computer and use it in GitHub Desktop.
quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import ion | |
import time | |
import kandinsky as ks | |
class Problem: | |
@classmethod | |
def generate_class(cls): | |
r = random.random() | |
if r < 0.4: | |
return Product | |
elif r < 0.8: | |
return Sum | |
else: | |
return Power | |
@classmethod | |
def generate_n(cls, n): | |
c = cls.generate_class() | |
return [c.generate() for _ in range(n)] | |
def __init__(self): | |
pass | |
class Product(Problem): | |
@classmethod | |
def generate(cls): | |
return cls( | |
random.randint(2, 26), | |
random.randint(2, 26) | |
) | |
def __init__(self, a, b): | |
self.a = a | |
self.b = b | |
super().__init__() | |
def __repr__(self): | |
return str(self.a) + ' * ' + str(self.b) | |
def check_answer(self, answer): | |
return self.a * self.b == answer | |
class Sum(Problem): | |
@classmethod | |
def generate(cls): | |
return cls( | |
random.randint(2, 10000), | |
random.randint(2, 10000) | |
) | |
def __init__(self, a, b): | |
self.a = a | |
self.b = b | |
super().__init__() | |
def __repr__(self): | |
return str(self.a) + ' + ' + str(self.b) | |
def check_answer(self, answer): | |
return self.a + self.b == answer | |
class Power(Problem): | |
@classmethod | |
def generate(cls): | |
return cls( | |
random.randint(2, 10), | |
random.randint(2, 3) | |
) | |
def __init__(self, a, b): | |
self.a = a | |
self.b = b | |
super().__init__() | |
def __repr__(self): | |
return str(self.a) + '^' + str(self.b) | |
def check_answer(self, answer): | |
return self.a ** self.b == answer | |
NUMBER_KEYS = [ | |
ion.KEY_ZERO, | |
ion.KEY_ONE, ion.KEY_TWO, ion.KEY_THREE, | |
ion.KEY_FOUR, ion.KEY_FIVE, ion.KEY_SIX, | |
ion.KEY_SEVEN, ion.KEY_EIGHT, ion.KEY_NINE] | |
ARROW_KEYS = [ion.KEY_LEFT, ion.KEY_UP, ion.KEY_DOWN, ion.KEY_RIGHT] | |
def read_keys(active_keys): | |
pulling_keys = active_keys + [ion.KEY_EXE] | |
while True: | |
for k in pulling_keys: | |
if ion.keydown(k): | |
if k in active_keys: | |
yield k | |
elif k == ion.KEY_EXE: | |
return | |
# Don't read continiusly | |
while ion.keydown(k): | |
pass | |
time.sleep(0.01) | |
light_gray = ks.color(220, 220, 220) | |
n = 9 | |
problems = Problem.generate_n(n) | |
answers = ['' for _ in range(n)] | |
results = ['' for _ in range(n)] | |
cursor = 1 | |
def draw(i): | |
ks.fill_rect(0, 20 + i*20, 320, 20, 'white') | |
ks.draw_string(str(problems[i]), 20, 20 + i*20) | |
ks.draw_string(results[i], 300, 20 + i*20) | |
if cursor == i: | |
ks.draw_string(answers[i] or ' ', 180, 20 + i*20, 'black', light_gray) | |
else: | |
ks.draw_string(answers[i] or ' ', 180, 20 + i*20, 'black', 'white') | |
ks.fill_rect(0,0, 320,240, 'white') | |
for i in range(n): | |
draw(i) | |
for k in read_keys([ion.KEY_UP, ion.KEY_DOWN, ion.KEY_BACKSPACE, ion.KEY_EXE] + NUMBER_KEYS): | |
last_cursor = cursor | |
if k == ion.KEY_UP and cursor > 0: | |
cursor -= 1 | |
elif k == ion.KEY_DOWN and cursor < 8: | |
cursor += 1 | |
elif k in NUMBER_KEYS: | |
number = NUMBER_KEYS.index(k) | |
answers[cursor] += str(number) | |
elif k == ion.KEY_BACKSPACE: | |
answers[cursor] = answers[cursor][:-1] | |
elif k == ion.KEY_EXE: | |
if not answers[cursor]: | |
continue | |
answer = int(answers[cursor]) | |
if problems[cursor].check_answer(answer): | |
results[cursor] = 'V' | |
if cursor < 8: | |
cursor += 1 | |
else: | |
results[cursor] = 'X' | |
answers[cursor] = '' | |
draw(cursor) | |
if cursor != last_cursor: | |
draw(last_cursor) | |
if all(r == 'V' for r in results): | |
ks.fill_rect(40, 60, 240, 90, light_gray) | |
ks.draw_string('Done!', 130, 100, 'black', light_gray) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment