learn Python. Create Games

User Tools

Site Tools


This is an old revision of the document!


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


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.


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.


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:
if you see no graphic here press reload on your webbrowser, use a different browser or follow this link

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


/var/www/horst/ · Last modified: 2014/01/09 11:07 (external edit)