Project #3 — Treasure Island Game with Python
Ciao, tutti!
In the realm of programming and game development, there’s a wonderful avenue for creativity: text-based adventure games. These games allow players to embark on thrilling journeys through a series of decisions and choices. In this blog post, I’ll take you through the creation of a simple yet engaging text-based adventure game called “Treasure Island” using the Python programming language.
Setting the Scene
Our adventure begins on a mysterious island known as Treasure Island. The player’s mission is clear: find the hidden treasure! The fate of the game is determined by the choices the player makes along the way.
The Python Code
Let’s start by looking at the Python code that powers our Treasure Island game:
print('''
*******************************************************************************
| | | |
_________|________________.=""_;=.______________|_____________________|_______
| | ,-"_,="" `"=.| |
|___________________|__"=._o`"-._ `"=.______________|___________________
| `"=._o`"=._ _`"=._ |
_________|_____________________:=._o "=._."_.-="'"=.__________________|_______
| | __.--" , ; `"=._o." ,-"""-._ ". |
|___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________
| |o`"=._` , "` `; .". , "-._"-._; ; |
_________|___________| ;`-.o`"=._; ." ` '`."\\` . "-._ /_______________|_______
| | |o; `"-.o`"=._`` '` " ,__.--o; |
|___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____
/______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_
____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/[TomekK]
*******************************************************************************
''')
print("Welcome to Treasure Island.")
print("Your mission is to find the treasure")
# First choice: Left or Right?
choice1 = input('You\'re on a crossroad, where do you want to go? Type "Left" or "Right".\n').lower()
if choice1 == "left":
# Continue the game
choice2 = input('You\'ve come to a lake. There is an island inside the lake. Type "Wait" to wait for a boat. Type "Swim" to swim across.\n').lower()
if choice2 == "wait":
# Continue the game
choice3 = input("You've arrived at the island unharmed. There is a house with 3 doors. One red, one yellow, and one blue. Which one do you choose?\n").lower()
if choice3 == "red":
print("You get burned by fire. Game over!")
elif choice3 == "yellow":
print("Cool anjing. You win, puh sepuh!")
elif choice3 == "blue":
print("Oops! You got eaten by a monster babi. Game Over!")
else:
print("You chose a door that doesn't exist. Game over!")
else:
print("Something terrible happens. Game over!")
else:
print("Oh no! You've fallen into a hole. Game over!")
Explaining the Game:
- Welcome Message: The game begins with a warm welcome and a clear mission statement: find the treasure.
- Choice 1: The player is presented with their first choice — go “Left” or “Right.” This decision will set the course for the entire adventure.
- Choice 2: If the player chooses “Left,” they face a lake with an island. Here, they must decide whether to “Wait” for a boat or “Swim” across. The consequences of this choice can be dramatic.
- Choice 3: If the player chooses to “Wait” and reaches the island, they encounter a house with three doors: red, yellow, and blue. Each door leads to a different outcome, ranging from victory to peril
Creating text-based adventure games like “Treasure Island” in Python can be both fun and educational. This example showcases the power of conditional statements and user input to craft engaging stories and interactive experiences. Feel free to modify and expand upon this code to create your own unique adventures. Happy coding and may you always find the treasure in your quests!
Takeaways:
- if/elif/else statement
- Multiple if statement
- Logical Operations in Python
- Nested if statement and elif statement
- Control flow with if/else and Conditional Operators
Try it out by hitting this link!