Python lesson 31

Capstone. Build a Real Game

Everything you have learned, in one program you can keep.

How a Real Program Is Built

Nobody writes a whole game in one go. You build one small piece, run it, check it works, and only then add the next. Every game in this lesson grows the same way: data first, then one function, then the loop that ties them together.

# 1. the data. Dictionaries and lists
# 2. the functions. One job each
# 3. the loop. While the game is still going
# 4. the ending. Print the result

rooms = {
    "hall": "A dusty hall with two doors.",
    "library": "Books everywhere. One looks new.",
}

def describe(room):
    return rooms.get(room, "Nothing here.")

print(describe("hall"))
print(describe("cellar"))

What Order Do You Build In?

A Complete Treasure Hunt

Read it before you run it. Every single line uses something from an earlier lesson.

import random

class Player:
    def __init__(self, name):
        self.name = name
        self.coins = 0
        self.energy = 5

    def is_playing(self):
        return self.energy > 0

chests = ["gold", "empty", "gems", "empty", "trap"]
prizes = {"gold": 50, "gems": 30, "empty": 0, "trap": -20}

player = Player("Nova")

while player.is_playing():
    chest = random.choice(chests)
    reward = prizes.get(chest, 0)
    player.coins = player.coins + reward
    player.energy = player.energy - 1
    print("Opened a", chest, "chest ->", reward, "coins")

    if player.coins >= 100:
        print("Rich enough! Going home early.")
        break

print("---")
print(player.name, "finished with", player.coins, "coins")

Name the Pieces

prizes = {"gold": 50, "gems": 30, "empty": 0, "trap": -20}
reward = prizes.get(chest, 0)

Make It Yours

Change the chests, change the prizes, change how much energy the player starts with. Break it and fix it. That is the lesson.

import random

chests = ["gold", "empty", "gems", "trap"]
prizes = {"gold": 50, "gems": 30, "empty": 0, "trap": -20}

coins = 0
energy = 4

while energy > 0:
    chest = random.choice(chests)
    coins = coins + prizes.get(chest, 0)
    energy = energy - 1
    print("Opened", chest, "- coins now", coins)

print("Final score:", coins)

Add a Quiz Round

A quiz built from two lists walked together with zip, marked with a function, wrapped in try. Six lessons in fifteen lines.

questions = ["2 + 2", "10 / 2", "3 * 3"]
answers = [4, 5, 9]
given = ["4", "five", "9"]

def mark(given_answer, correct_answer):
    try:
        return int(given_answer) == correct_answer
    except ValueError:
        return False

score = 0
for number, (question, correct, mine) in enumerate(zip(questions, answers, given), start=1):
    if mark(mine, correct):
        score = score + 1
        print(number, question, "- correct")
    else:
        print(number, question, "- wrong, the answer was", correct)

print("Scored", score, "out of", len(questions))

What you can do on CodeIt

Run the example, change one part, and use the result to check your understanding before moving to the next lesson.