by Brendon Thiede
If you were designing a video game...
What is the first room the player starts in?
Describe it in one sentence.
Time to get our starter code!

git clone https://github.com/YOUR-USERNAME/workshops.git
cd workshops/advanced-python/starter-code
Already cloned? Run git pull instead
code .
Open the integrated terminal: View → Terminal
cd advanced-python/starter-code
python game.py
You should see the game title and a name prompt!
Type a name, then type quit to exit
Ordered collections — perfect for inventories
inventory = [] # empty list
inventory.append("sword") # add an item
inventory.append("torch") # add another
print(inventory) # ['sword', 'torch']
print(inventory[0]) # 'sword' (starts at 0!)
print(len(inventory)) # 2
Square brackets [ ] — items separated by commas
inventory = ["sword", "torch", "map"]
for item in inventory:
print(f" - {item}")
Output:
- sword
- torch
- map
Labeled data — perfect for room descriptions
room = {
"name": "Dark Cave",
"description": "Cold and damp.",
"exits": ["entrance", "tunnel"],
"item": "torch"
}
print(room["name"]) # Dark Cave
print(room["exits"]) # ['entrance', 'tunnel']
print(room["exits"][0]) # entrance
Curly braces { } — key: value pairs
This is how game.py stores all the rooms!
rooms = {
"cave": {
"name": "Dark Cave",
"exits": ["entrance"],
"item": "torch"
},
"entrance": {
"name": "Cave Entrance",
"exits": ["cave", "forest"],
"item": None
},
}
print(rooms["cave"]["item"]) # torch
Find the rooms dictionary at the top.
What does rooms["forest"]["item"] return?
Reusable pieces of code — the secret to bigger programs
Without functions:
# Room 1
print("=== Dark Cave ===")
print(rooms["cave"]["description"])
print(f"Exits: {', '.join(rooms['cave']['exits'])}")
# Room 2
print("=== Forest ===")
print(rooms["forest"]["description"])
print(f"Exits: {', '.join(rooms['forest']['exits'])}")
# ... repeat for every room
Copy-paste is a warning sign — time for a function!
def describe_room(room):
print(f"\n=== {room['name']} ===")
print(room['description'])
print(f"Exits: {', '.join(room['exits'])}")
if room['item']:
print(f"You see: {room['item']}")
# Now call it for any room:
describe_room(rooms["cave"])
describe_room(rooms["forest"])
Change the function once → fixes everything
def greet(name): # def, function name, parameter
message = f"Hello, {name}!"
return message # send a value back
result = greet("Alex") # call the function
print(result) # Hello, Alex!
Find take_item() in game.py. It currently does nothing (pass). Make
it:
room['item'] is not Noneplayer.inventory, clear room['item'], print a message
def take_item(player, room):
if room['item']:
item = room['item']
player.inventory.append(item)
room['item'] = None
print(f"You picked up: {item}")
else:
print("There is nothing to take here.")
Why do programmers prefer dark mode?
Bugs are attracted to light!
Blueprints for creating objects
A class describes what data an object has and what it can do.
class Player:
def __init__(self, name): # runs when created
self.name = name # attribute
self.health = 100 # attribute
self.inventory = [] # attribute
player = Player("Alex") # create one Player
print(player.name) # Alex
print(player.health) # 100
class Player:
def __init__(self, name):
self.name = name
self.health = 100
def is_alive(self):
return self.health > 0
player = Player("Alex")
print(player.is_alive()) # True
player.health = 0
print(player.is_alive()) # False
self refers to this specific Player object
player.py holds the Player class.
game.py imports it:
from player import Player
player = Player("Alex")
One file, one responsibility — easier to read and fix
Open player.py and add two methods:
take_damage(self, amount) — reduce health, print statushas_item(self, item_name) — return True/False
def take_damage(self, amount):
self.health = self.health - amount
print(f"You took {amount} damage! Health: {self.health}")
def has_item(self, item_name):
return item_name in self.inventory
Don't forget to indent your methods inside the class!
Making your game remember between sessions
The save_game() and load_game() functions are already in game.py.
import json
# Writing to a file
data = {"name": "Alex", "inventory": ["torch"]}
with open("savegame.json", "w") as f:
json.dump(data, f)
# Reading from a file
with open("savegame.json", "r") as f:
data = json.load(f)
print(data["inventory"]) # ['torch']
python game.pytake)save, then quitOpen savegame.json in VS Code to see what Python wrote
take_item() in game.pytake_damage() and has_item() in player.pyrooms dict and connect it"help" command to the dispatcherplayer.take_damage()max_inventory limit to the Player classKeyError — check for typos in room namesAttributeError — make sure your method has selfIndentationError — check method is inside the classsavegame.json if the save file is corruptedLet's commit to Git!
git add .
git commit -m "Complete text RPG"
git push
Now your RPG is saved on GitHub!
Who wants to demo their game?
Enter a room, pick up an item, save, and reload!
Which concept clicked the most for you today?
What would you add to your RPG with more time?