Bomber

A plane over a city: drop bombs with the space bar and flatten the city before you hit it. In an emergency, F fires a shot ahead of the plane that destroys the buildings at its height one by one (costs 100 points). A hit plays a tone, landing and crashing each play their own tune. Teaches a real-time game and sound.

Open in Tokbit Level: Expert. 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 Bomber example

Python program

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

"""A plane descends over a city; drop bombs to clear a landing. Space drops a bomb, F fires ahead of the plane (−100 points, +10 for each piece destroyed). Teaches a list of heights."""
from random import randint
# Tokbit screen functions, not in plain Python:
from tokbit import (
    cls, at, color, background, border, char_at, key, sleep, beep, note, BLACK,
    WHITE, RED, CYAN, PURPLE, GREEN, YELLOW, ORANGE, BROWN, GREY, LIGHT_BLUE,
)

def draw_city(heights: list[int]):
    """Draws the buildings from the list of heights, one per column."""
    colors: list[int] = [RED, GREEN, PURPLE, ORANGE, BROWN, GREY]
    for i in range(len(heights)):
        color(colors[i % 6])
        for j in range(heights[i]):
            at(4 + i, 23 - j)
            print("#", end="")

def build_city():
    """Black sky, a random city of 32 buildings and the runway at the bottom."""
    background(BLACK)
    border(BLACK)
    cls()
    heights: list[int] = []
    for x in range(32):
        heights.append(randint(1, 10))
    draw_city(heights)
    color(LIGHT_BLUE)
    for x in range(40):
        at(x, 24)
        print("=", end="")

def fly(plane: list[int]) -> str:
    """Moves the plane one step (down a row at the end of a line). Returns "naraz" (crash), "pristal" (landed) or ""."""
    at(plane[0], plane[1])
    print("  ", end="")
    plane[0] = plane[0] + 1
    if plane[0] > 37:
        plane[0] = 0
        plane[1] = plane[1] + 1
    if char_at(plane[0], plane[1]) != " " or char_at(plane[0] + 1, plane[1]) != " ":
        return "naraz"
    else:
        color(CYAN)
        at(plane[0], plane[1])
        print("=>", end="")
        if plane[1] == 23 and plane[0] == 37:
            return "pristal"
    return ""

def drop(bomb: list[int], plane: list[int]):
    """The bomb drops below the plane."""
    bomb[0] = 1
    bomb[1] = plane[0]
    bomb[2] = plane[1]
    bomb[3] = 0

def fall(bomb: list[int]) -> int:
    """Moves the bomb down a row and knocks out a building; returns the points. Gone after three hits or on the ground."""
    points: int = 0
    at(bomb[1], bomb[2])
    print(" ", end="")
    bomb[2] = bomb[2] + 1
    if bomb[2] >= 24:
        bomb[0] = 0
    else:
        if char_at(bomb[1], bomb[2]) == "#":
            points = 10
            bomb[3] = bomb[3] + 1
            at(bomb[1], bomb[2])
            print(" ", end="")
            beep(220, 30)
            if bomb[3] == 3:
                bomb[0] = 0
        if bomb[0] == 1:
            color(YELLOW)
            at(bomb[1], bomb[2])
            print("v", end="")
    return points

def fire(shot: list[int], plane: list[int]):
    """The shot leaves ahead of the plane at its height (at most one at a time)."""
    shot[0] = 1
    shot[1] = plane[0] + 1
    shot[2] = plane[1]
    beep(880, 40)

def move_shot(shot: list[int]) -> int:
    """The shot moves 3 columns on (the plane moves 1); it destroys the buildings it passes and returns the points (10 per piece). It disappears at the right edge."""
    points: int = 0
    for i in range(3):
        if shot[0] == 1:
            if char_at(shot[1], shot[2]) == "-":
                at(shot[1], shot[2])
                print(" ", end="")
            shot[1] = shot[1] + 1
            if shot[1] > 39:
                shot[0] = 0
            else:
                if char_at(shot[1], shot[2]) == "#":
                    points = points + 10
                    beep(440, 20)
                color(WHITE)
                at(shot[1], shot[2])
                print("-", end="")
    return points

def show_score(score: int):
    """Prints the score at the top left; the spaces after it clear digits of a longer score (it drops after a shot)."""
    color(WHITE)
    at(0, 0)
    print("Score:", score, "     ", end="")

def game_over(over: str, score: int):
    """Landing or crash: the message, a melody and the score."""
    color(WHITE)
    if over == "pristal":
        at(8, 11)
        print("YOU LANDED! The city is flat.", end="")
        note("C5", 120)
        note("E5", 120)
        note("G5", 120)
        note("C6", 400)
    else:
        at(8, 11)
        print("BOOM! You hit a building.", end="")
        note("E3", 150)
        note("C3", 150)
        note("A2", 500)
    at(8, 12)
    print("Score:", score, end="")

playing: str = "a"
while True:
    build_city()
    # The plane: column and row.
    plane: list[int] = [0, 2]
    # The bomb: falling (1/0), column, row and number of hits.
    bomb: list[int] = [0, 0, 0, 0]
    # Shot: flying (1/0), column and row.
    shot: list[int] = [0, 0, 0]
    score: int = 0
    over: str = ""
    paused: int = 60
    # Game loop: flight, bomb and hitting a building until the game ends.
    while over == "":
        paused = 60
        over = fly(plane)
        k: str = key()
        if k == " " and bomb[0] == 0:
            drop(bomb, plane)
        if bomb[0] == 1:
            points: int = fall(bomb)
            score = score + points
            if points > 0:
                paused = 30
        # Emergency shot F: costs 100 points when fired, the score never goes below 0. It flies until it reaches the edge; another F while it flies does nothing.
        if (k == "f" or k == "F") and shot[0] == 0:
            score = score - 100
            if score < 0:
                score = 0
            fire(shot, plane)
        # A flying shot destroys the buildings in its row and adds points.
        if shot[0] == 1:
            score = score + move_shot(shot)
        show_score(score)
        sleep(paused)
    game_over(over, score)
    at(8, 14)
    playing = input("Play again (y/n)?")
    if not (playing == "a" or playing == "y"):
        break

All examples