Simon
A memory game: the computer plays a growing sequence of coloured fields with tones and you repeat it with keys 1–4. Teaches a list as memory, a loop over a list and sound.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""A memory game: repeat a growing sequence of colours and tones. Teaches a list as memory and a loop over a list."""
from random import choice
# Tokbit screen functions, not in plain Python:
from tokbit import (
cls, at, color, background, border, key, sleep, beep, note, BLACK, WHITE,
RED, GREEN, BLUE, YELLOW, BROWN, LIGHT_RED, DARK_GREY, GREY, LIGHT_GREEN,
LIGHT_BLUE,
)
def draw_pads(pad: int, lit: bool):
"""Draws one of the four fields from characters; a lit field in a lighter colour. Field 1 top, 2 left, 3 right, 4 bottom."""
columns: list[int] = [16, 6, 26, 16]
lines: list[int] = [4, 9, 9, 14]
dark: list[int] = [RED, GREEN, BLUE, BROWN]
light: list[int] = [LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE, YELLOW]
if lit:
color(light[pad - 1])
else:
color(dark[pad - 1])
for r in range(4):
at(columns[pad - 1], lines[pad - 1] + r)
print("########", end="")
color(WHITE)
at(columns[pad - 1] + 3, lines[pad - 1] + 1)
print(" " + str(pad) + " ", end="")
def light_up(pad: int, ms: int):
"""Lights a field and plays its tone."""
tones: list[str] = ["C4", "E4", "G4", "C5"]
draw_pads(pad, True)
# Each field has its own tone; note waits until it has played.
note(tones[pad - 1], ms)
draw_pads(pad, False)
def draw_field():
"""The start screen: title, four fields and a hint."""
background(BLACK)
border(DARK_GREY)
cls()
color(YELLOW)
at(15, 1)
print("S I M O N", end="")
for p in range(4):
draw_pads(p + 1, False)
color(GREY)
at(1, 21)
print("Repeat with keys 1 to 4 or arrows.", end="")
def show_status(text: str, score: int):
"""The game status at the bottom: what is happening and the score."""
color(WHITE)
at(1, 23)
print(text + " " + "Score: " + str(score) + " ", end="")
def play_back(sequence: list[int], ms: int):
"""Plays the whole sequence; ms is the length of one flash (it gets shorter, the game speeds up)."""
for i in range(len(sequence)):
light_up(sequence[i], ms)
sleep(ms // 3)
def pad_from_key(k: str) -> int:
"""The field number for a key (1–4 or an arrow), any other key gives 0."""
keys: list[str] = ["1", "2", "3", "4"]
arrows: list[str] = ["up", "left", "right", "down"]
for i in range(4):
if k == keys[i] or k == arrows[i]:
return i + 1
return 0
def wait_for_pad() -> int:
"""Waits until the player picks a field."""
pad: int = 0
# key() does not wait: we keep asking until the player presses a valid key.
while pad == 0:
pad = pad_from_key(key())
sleep(20)
return pad
def repeat_back(sequence: list[int]) -> bool:
"""The player repeats the sequence; returns False at the first mistake."""
for i in range(len(sequence)):
pad: int = wait_for_pad()
light_up(pad, 200)
# Comparing input with memory: the i-th key must match the i-th item of the sequence.
if pad != sequence[i]:
beep(110, 500)
return False
return True
def game_over(score: int):
"""Game over: the score and falling tones."""
color(LIGHT_RED)
at(1, 23)
print("Wrong! You remembered a sequence of" + " " + str(score) + ".", end="")
for i in range(4):
beep(440 - i * 80, 120)
draw_field()
# The sequence of fields the player has to remember.
sequence: list[int] = []
score: int = 0
playing: bool = True
# The game loop: every round adds one field, plays the sequence and the player repeats it.
while playing:
sequence.append(choice([1, 2, 3, 4]))
show_status("Listen...", score)
sleep(500)
play_back(sequence, max(150, 450 - len(sequence) * 25))
show_status("Repeat!", score)
playing = repeat_back(sequence)
if playing:
score = len(sequence)
beep(1200, 60)
game_over(score)
Follow-up tasks
Tasks in which you change the example (with a model solution):