Hardware Reference
In-Depth Information
# Check health points after the player has made a move
print(“You now have “, hp, “health points”)
if hp <= 0:
print(“You are dead. I am sorry.”)
he last two lines add a conditional so that if the value of the hp variable is less than
or equal to 0, the statement “You are dead. I am sorry.” is displayed and the game ends.
Putting It All Together
Now put all the elements together in your text adventure game by typing the following
program into a new Python 3 IDLE text editor window:
A
You can download the completed AdventureGame1.py code ile from the
companion website at www.wiley.com/go/adventuresinrp but, as I
mentioned earlier, you will learn more by typing in the code as you read through
the steps.
Python Text Adventure Game
# Python Text Adventure Game
import time
# Create health point variable
hp = 30
# Tell player their location and wait 1 second
print(“You are standing on a path at the edge of a jungle. ;
There is a cave to your left and a beach to your right.”)
time.sleep(1)
# Loop until we get a recognised response
while True:
direction1 = input(“Do you want to go left or right? “)
# Convert to lower case to accept LEFT and RiGhT etc.
direction1 = direction1.lower()
if direction1 == “left”:
print(“You walk to the cave and notice there is an opening.”)
print(“A small snake bites you, and you lose 20 health ;
points.”)
hp = hp - 20
continued
Search WWH ::




Custom Search