Hardware Reference
In-Depth Information
Using a Variable for Health Points
In the text adventure game so far, you have created a variable for health points ( hp =
30 ), like the variable you created in the Scratch adventure game in Adventure 3. Here
you have given an initial value that will change as the player plays the game. he value
you have given is 30, but this could be any value of your choosing.
Now code can be added that will change the hp value based on the decisions made by
the player. You name a variable using the form name = value , as in the following
example:
hp = 30
Here are two ways to change the value of the hp variable in Python 3:
To subtract 10, use hp = hp - 10 or hp -= 10
To add 10, use hp = hp + 10 or hp += 10
You can use the following symbols to program calculations:
- Subtract
+ Add
* Multiply
/ Divide
< Less than
> Greater than
The following symbols produce a value that is True or False, so they are useful in
conditionals:
== Equals
!= Not equals
<=
Less than or equal to
> Greater than
>=
Greater than or equal to
To make the game more interesting for the player, you can add some code to the end
of what you have already written to tell her how many health points she has after each
move:
Search WWH ::




Custom Search