Hardware Reference
In-Depth Information
return “start”
elif location == “beach”:
print(“You walk to the beach but remember you do ;
not have any swimwear.”)
print(“The cool water revitalizes you. You have never ;
felt more alive, gain 70 health points.”)
hp += 70
return “end”
else:
print(“Programmer error, room “, location, “ is ;
unknown”)
return “end”
4. Now add a loop to the game that loops until the player reaches the special end
location that ends the game.
location = “start”
# Loop until we reach the special “end” location
while location != “end”:
location = handle_room(location) # update location
5. As the game also relies on the player having health points, at each turn the pro-
gram needs to check how many health points the player has and determine that
the player is not dead, because this would end the game.
# Check we are not dead each turn
print(“You now have “, hp, “health points.”)
if hp <= 0:
print(“You are dead. I am sorry.”)
break
print(“Your adventure has ended, goodbye.”)
Each time around, the loop checks that the health points are greater than or equal to
zero (checking that the player is not dead!) so that the game can continue. he loop will
also end if the location returned by handle_room is end , a special room name indi-
cating the end of the game. Figure 5-13 shows the refactored code using functions in
the Python adventure game.
Search WWH ::




Custom Search