Python Adventure Game Sample Code
Python Adventure Game Sample Code
This guide will help you make a basic adventure game in Python. Follow each section to build your game step-by-
step!
Example Code:
Explanation:
- The input() function is used to ask the player their name. This gets stored in `player_name` so we can use it later.
Example Code:
Explanation:
- The IF checks if the player typed 'left'. If they did, it shows a message about going down a spooky path.
- The ELIF checks if the player typed 'right'. If they did, it shows a message about a peaceful clearing.
- The ELSE is used if the player typed something other than 'left' or 'right'. This message appears if their input
doesn’t match either choice.
while game_active:
choice = input("Type 'left', 'right', or 'exit' to make your choice: ")
if choice == "left":
print("You venture down a dark path.")
elif choice == "right":
print("You find a quiet lake.")
elif choice == "exit":
print("You have chosen to end your adventure.")
game_active = False
else:
print("Invalid choice. Please try again.")
Explanation:
- `while game_active:` means the code inside the loop will keep going as long as `game_active` is `True`.
- `if choice == 'exit':` lets the player end the game by setting `game_active` to `False`, which stops the loop.
Tips:
- Type your code exactly as shown.
- Make sure each line is in the right place (especially lines inside loops and `if` statements).