# ThePythonGameBook

learn Python. Create Games

### Sidebar

en:secret:start

This is an old revision of the document!

# secret

stuff that i try out and that should not be seen (yet)

## Goblins

Let's image two (not too bright) goblins doing there favorite sport playing gobling dice, aka hitting each other on the head with big clubs.

The last gobling standing is usually the winner.

Let us call the goblins “Stinky” and “Grunty” and simulate their game using python, to save them the many headaches from hitting each other, while allowing us to learn a thing or two about game programming.

#### hitpoints

The first game concept necessary is the concept of hitpoints. Hitpoints is an integer value describing how many hits (on his head) a gobling can suffer before going down. Computer game players should be familiar with concept of hitpoints. In many computer games, but also pen-and-paper role-playing games, hitpoints are represented by a green bar or some other visual indicator as well as displayed as numbers.

#### Variables

To simulate Grunty and Stinky in all their robustness in python, it is necessary to create 2 variables. A variable is something like a money box or piggy bank… a box (container) holding some money (value). Also a variable has a name, like “pension fund” for a piggy bank. The point of a variable is that it's value ( the amount of money in the piggy bank) can change over time. (You add money or take some money out of the box). Thus the term variable in contrast to constants, like pi.

In this example, i create the two variables and assign integer values to them:

```hitpointsStinky = 22
hitpointsGrunty = 43```

The python interpreter creates a new variables with the name hitpointsStinky and assings the value of 44 to it. He also creates the new variable hitpointsGrunty and assigns the value of 63 to it.

Note that the variable names in python should always begin with a lowercase and can not include a space sign. For better readabilty instead of “hitpointsstinky” i wrote “hitpointsStinky” (Stinky has a big S) wich is called CamelCase. There a more rules for naming variables in python, like that you can not use a reserved python keyword, the first char can not be a number and other than letter (a-z) and numbers (0-9) only the underscore (“_”) is allowed. See naming variables for more information.

it is a good idea to write that in a python shell. If you have no python shell installed you can however do this example online:

In game mechanics, Grunty is more robust than Stinky and can tolerate more hits. Possibly because his skull is thicker.

#### changing variables

During a game of goblin dice duel, each hit on the head will decrease the victim's remaining hitpoints.

Let's assume that Stinky swings his club and hit Grunty over the head so that Grunty looses one hitpoint ( or hitting him for 1 point of damage).

In python, this is done by writing either:

`hitpointsGrunty = hitpointsGrunty - 1`

or more common because less to type:

`hitpointsGrunty -= 1`

In both cases, the python interpreter will first calculate the right side of the equation ( hitpointsGrunty - 1) and assign the result to the variable on the left side of the equation….hitpointsGrunty. The old value of the variable is overwritten.

Tricky question: How many hitpoints has Grunty left now ? Find out by either coding in your pyhton shell or online: