The Python Game Book

code games. learn Python.

User Tools

Site Tools




001 variables

step 001

Slow Goblin Dice Duel Tutorial

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

The last goblin 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 python game programming.


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 the 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.


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:

source code

click reload on your browser if you see no source code here or visit The first two lines starting with an hash (<key>#</key>) are comments. For now, just ignore them.

code discussion

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. Also of any chars other than letters (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. Just click on the graphic below:
click on the graphic!

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

step 001
en/secret/goblins/step001.txt · Last modified: 2020/05/03 21:35 by horst