Hardware Reference
In-Depth Information
his line of code asks the player to answer the question, “Do you want to go left or
right?” he program will wait for the player to type in an acceptable response—one
that the program can understand.
Using Conditionals
Once the player has responded, you want something to happen based on her answer.
You therefore need to use conditionals . You used conditionals in your Scratch adven-
ture game in Adventure 3 to control the movement of the adventurer sprite, using the
forever if control block.
Remember that creating a conditional is like asking a question where there are two or
more outcomes. For example, you could ask the question “Is it raining?” If the answer
is yes, you should put on a raincoat; if the answer is no, you should go out without a
jacket. he key word used here is “ if ”.
You will use if in Python 3 to create your game conditions. Open a new Python IDLE
3 text editor window and save the ile as AdventureGame.py .
1. he irst step is to import the modules that you will need for the program. As in
the inventory program, you will need the sleep() function from the time
module, so import that module with the following code:
import time
2. Later in this game, you may wish to give your player health points that could go
up or down depending on which directions she takes in the adventure. he num-
ber of remaining health points is stored in a variable. To include that feature,
type the following line:
hp = 30
3. Now use the print() function to tell your player where she is located in the
game, and then use the sleep() function to wait one second before moving on.
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)
4. As in the inventory program, you will want to get input from the player of your
game. In this case, you want to know if she wishes to turn left or right, and the
player's answer is then labelled as direction1 .
direction1 = input(“Do you want to go left or right? “)
Search WWH ::




Custom Search