Aquarium

Six fish swim, bounce off the edges and turn around. Teaches many sprites at once, position lists and looping over indexes.

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

Flowchart of the Aquarium example

Python program

The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.

"""Six fish swim around. Teaches lists of coordinates, many sprites at once and functions over lists."""
# Tokbit screen functions, not in plain Python:
from tokbit import (
    cls, at, color, background, border, sleep, sprite_at, sprite_color,
    sprite_shape, WHITE, PURPLE, BLUE, YELLOW, ORANGE, LIGHT_RED, LIGHT_GREEN,
    LIGHT_BLUE,
)

def draw_tank():
    """Blue water, the title at the top and sand at the bottom."""
    background(BLUE)
    border(BLUE)
    cls()
    color(WHITE)
    at(1, 0)
    print("Aquarium: press Stop to end", end="")
    color(YELLOW)
    at(0, 23)
    print("." * 40, end="")
    at(0, 24)
    print(":" * 39, end="")

def draw_bubbles():
    """Bubbles at fixed places in the water."""
    color(LIGHT_BLUE)
    at(5, 18)
    print("o", end="")
    at(6, 15)
    print("o", end="")
    at(5, 12)
    print("o", end="")
    at(30, 20)
    print("o", end="")
    at(31, 16)
    print("o", end="")
    at(12, 21)
    print("o", end="")
    at(24, 9)
    print("o", end="")

def turn(i: int, direction: int, right: list[str], left: list[str]):
    """Gives fish i its shape: facing right when it swims right (direction > 0), otherwise left."""
    if direction > 0:
        sprite_shape(i, right)
    else:
        sprite_shape(i, left)

def swim(i: int, x: list[int], y: list[int], dx: list[int], dy: list[int], right: list[str], left: list[str]):
    """One step of fish i: move, turn round at the edge (shape too) and the new sprite position."""
    x[i] = x[i] + dx[i]
    y[i] = y[i] + dy[i]
    # At the edge a fish turns: both direction and shape flip.
    if x[i] <= 0 or x[i] >= 304:
        dx[i] = -dx[i]
        turn(i, dx[i], right, left)
    if y[i] <= 16 or y[i] >= 160:
        dy[i] = -dy[i]
    sprite_at(i, x[i], y[i])

draw_tank()
draw_bubbles()
right: list[str] = ["................", "................", "................", "........###.....", "......######....", "#...#########...", "##.###########..", "###########.###.", "###############.", "##.###########..", "#...#########...", "......######....", "........###.....", "................", "................", "................"]
left: list[str] = ["................", "................", "................", ".....###........", "....######......", "...#########...#", "..###########.##", ".###.###########", ".###############", "..###########.##", "...#########...#", "....######......", ".....###........", "................", "................", "................"]
x: list[int] = [20, 80, 150, 220, 40, 260]
y: list[int] = [30, 60, 90, 120, 140, 50]
dx: list[int] = [2, -3, 1, -2, 3, -1]
dy: list[int] = [1, -1, 2, 1, -2, 1]
colors: list[int] = [ORANGE, YELLOW, LIGHT_RED, LIGHT_GREEN, WHITE, PURPLE]
for i in range(6):
    turn(i, dx[i], right, left)
    sprite_color(i, colors[i])
    sprite_at(i, x[i], y[i])
# Every frame moves all six fish by their step.
while True:
    for i in range(6):
        swim(i, x, y, dx, dy, right, left)
    sleep(30)

Follow-up tasks

Tasks in which you change the example (with a model solution):

All examples