learn Python. Create Games

User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:python:goblins:step002 [2011/12/11 16:53]
Horst JENS [Step 002 - thousand goblin dice duels]
en:python:goblins:step002 [2017/09/03 16:21] (current)
Horst JENS
Line 1: Line 1:
-[[:en:python:step001| ← previous]] [[en:python:start| ↑ Overview]] [[:en:python:step003| → next ]] +===== 002 values ===== 
-====== Step 002 - thousand goblin dice duels ======+[[step001|←]] ​ | step 002 [[start|↑]] ​ ​|  ​[[step003|→]] ​ |
-If you tried **balancing** the values for the goblins Stinky and Grunty in [[en:​python:​step001|step 001]] you will feel the law of [[wp>Law_of_large_numbers]]: If Stinky wins 5 times in a row over Grunty, that does not proof that Stinky'​s values are superior over Grunty. Maybe Stinky has a streak of luck or Grunty'​s random numbers are specially unlucky. Only if you test a game (several) thousands times you can start to make qualified statements.+[<>]
-Well, pushing thousand times the "run program"​ button on my text editor is not my idea of fun. (Maybe later if the program becomes better). Good thing that there is a python function to repeat a given task a many times: the **for loop**. ​+=== changing variables ===
-The **for loop** **iterates** over given **list**. If you give the **for loop** a list with thousand items to iterate over, it will do the same task thousand times. But where to get a list with thousand items ? Lucky for you, python provides also a function to generate lists: the **range** function (and the **xrange** function).+During ​game of goblin dice duel, each hit on the head will decrease ​the victim'​s remaining hitpoints.
-===== playing with python'​s ​direct mode =====+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).
-This is a good time to play around with python's direct mode. If you start the editor **Idle** (shippedas you know, together with python), you are by default in the direct mode. If you -like me- dislike idle (inner values aside, the thing is simply ugly) you can open a terminal and enter <​code>​python</​code>​. The result in both cases is that you are in python'​s **direct mode**, symbolized by the 3 leading "​greater than" signs: <​code>​>>>>​</​code>​+In python, ​this is done by writing either: 
 +<​code ​python> 
 +hitpointsGrunty = hitpointsGrunty - 1 
 +or more common because less to type: 
 +<​code ​python> 
 +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.
-====== ​flow chart ======+=== source code === 
 +//click reload on your browser if you see no source code here or visit [[https://​​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​|]] // 
 +<​html><​script src="​http://​​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​​footer=minimal"></​script></​html>​
-====== Code Discussion ====== 
-===== comments =====+=== test === 
 +Tricky question: How many hitpoints has Grunty left now ?
-====== ​ideas ======+Find out by either coding in your pyhton shell or online (just click on the graphic): \\ 
 +[[http://​​visualize.html#​code=hitpointsStinky+%3D+22%0AhitpointsGrunty+%3D+43%0AhitpointsGrunty+-%3D+1&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=3|{{:​en:​python:​goblins:​slowgoblins002.png|click on the graphic}}]]
-====== Source Code on Github ======+The answer is 42((If you don't laugh now, you should read [[wp>​The_Hitchhiker%27s_Guide_to_the_Galaxy_(novel)|this book]])) 
 +=== inspecting variables ​===
-To start this example you need:+If you click the grey "​forward"​ button on the online ​example ​above you can see how the values of the variables change each time. IF you work with the python shell, ​you must tell python to show the value of a variable by typing the variable name:
-^  file  ^  ​in folder ​ ^  download ​ ^  comment ​ ^ +Do this in the python shell: 
-|  [[https://​​horstjens/​ThePythonGameBook/​raw/​master/​python/|]] ​ |  python ​ |  Download the whole Archive with all files from Github: ​ \\  https://​​horstjens/​ThePythonGameBook/​archives/​master ​ |  version for python2.x |+<​code ​python
 +Later, you will learn other methods to inspect variables, like the print() function and how to work with debuggers.
-\\ +Generally, ​if you want to print the value of a variable, you code: 
-===== python 2.x ===== +<code python> 
- +print(hitpointsGrunty) 
- +</code>
- +
-View/​Edit/​Download the file directly in Github: https://​​horstjens/​ThePythonGameBook/​blob/​master/​python/​  +
- +
-//click reload in your browser ​if you see no code here:// +
-<html> +
- <​script src="​http://​​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​"​+
- </script></​html>​ +
- +
-^ [[:​en:​python:​step001| ← previous]] ^ [[en:​python:​start| ↑ Overview]] ^ [[:​en:​python:​step003| → next ]] ^ +
-====== Comment this Page ====== +
-~~DISQUS~~ +
 +| [[step001|←]] ​ | step 002 [[start|↑]] ​ |  [[step003|→]] ​ |
/var/www/horst/ · Last modified: 2014/01/09 11:07 (external edit)