Mini game: a campus adventure
Part of the free LogicWiz Python course, Pack B: College Semester Pack. The examples and practice run in the app.
The lab mini-project is due: "any game in Python". Pygame needs a laptop and a weekend. A text adventure needs only while, input() and a dictionary, and it is the best way to prove you understand loops, state and dicts.
You'll learn to
- Structure a game as a loop: read a command, update the state, print the result
- Store a world in a dictionary of rooms and a list of items
- Make games testable: fixed input, a seeded `random`, exact output
Learn
Every game is the same loop
set up the state (where you are, what you carry, score)
while the game is not over:
show the player what they see
read a command
update the state
The state is just variables. The world is data: a dict of rooms, each with a description, exits and items. Adding a room is adding a dict entry, not writing new ifs. That's the design idea the viva will ask about.
A three-room campus
rooms = {
"gate": {"desc": "You are at the college gate.", "exits": {"north": "canteen", "east": "library"}},
"canteen": {"desc": "Shankar bhaiya's canteen smells of samosas.", "exits": {"south": "gate"}},
"library": {"desc": "The library is silent. Exams are near.", "exits": {"west": "gate"}},
}
here = "gate"
while True:
print(rooms[here]["desc"])
cmd = input().strip().lower()
if cmd == "quit":
print("Bye!")
break
if cmd in rooms[here]["exits"]:
here = rooms[here]["exits"][cmd]
else:
print("You can't go that way.")
The input box holds the player's moves, one per line. Change them and run again. .strip().lower() means "East " still works.
Guess the number, testably
import random
random.seed(2024) # same seed -> same "random" number every run
secret = random.randint(1, 50)
for attempt in range(1, 6):
guess = int(input())
if guess < secret:
print(guess, "is too low")
elif guess > secret:
print(guess, "is too high")
else:
print(f"Correct! Got it in {attempt} tries.")
break
else:
print("Out of tries. It was", secret)
The for ... else runs the else only if the loop ended without break, which is exactly "out of tries". random.seed makes the game repeatable, so you (and a test) can check it. Guessing the middle each time is binary search (lesson B2): 50 numbers never need more than 6 guesses.
Items and a locked door
ROOMS = {
"gate": {"desc": "College gate. Paths go north and east.",
"exits": {"north": "canteen", "east": "library"}, "items": []},
"canteen": {"desc": "Shankar bhaiya's canteen. It smells of samosas.",
"exits": {"south": "gate"}, "items": ["id card"]},
"library": {"desc": "The library. Silent, and full of seniors.",
"exits": {"west": "gate"}, "items": []},
}
here, bag = "gate", []
for cmd in ["go east", "go north", "take id card", "go south", "go east"]:
print(">", cmd)
verb, _, rest = cmd.partition(" ")
if verb == "take" and rest in ROOMS[here]["items"]:
ROOMS[here]["items"].remove(rest)
bag.append(rest)
print("Taken:", rest)
elif verb == "go" and rest in ROOMS[here]["exits"]:
nxt = ROOMS[here]["exits"][rest]
if nxt == "library" and "id card" not in bag:
print("The guard stops you: no ID card.")
else:
here = nxt
print(ROOMS[here]["desc"])
cmd.partition(" ") splits "take id card" into "take" and "id card". The locked library is a single if on the state. Change the command list and run it again.
Make it testable. Read input with plain input() (no prompt text), always .strip().lower() commands, print exact messages, and seed random. Then a whole play-through is just an input list and an expected output, which is how the tests below (and a lab examiner) check your game. On a laptop, the same logic can drive a Tkinter or Pygame window later.
Lab mapping. Mini-projects and "simple game" programs (number guessing, rock-paper-scissors, quiz, text adventure) are part of the lab or project component in VTU, AKTU, JNTU, Anna, DU and Mumbai first-year courses.
Viva: "How does your game end?" (a flag or break) "Why a dict for rooms?" (data, not code: easy to add rooms) "What does random.seed do?" "What happens if the user types something unexpected?" (show your else branch).
In one line
A game is a while loop over (show, read, update); keep the world in dicts, clean input with .strip().lower(), seed random, and print exact messages so it is testable.