learn Python. Create Games

User Tools

Site Tools



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

Link to this comparison view

Next revision
Previous revision
en:python:goblins:step002 [2011/12/11 16:40]
Horst JENS created
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|→]] ​ |
-====== flow chart ======+[<>]
-====== Code Discussion ======+=== changing variables ​===
-===== comments =====+During a game of goblin dice duel, each hit on the head will decrease the victim'​s remaining hitpoints.
-====== ideas ======+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:
 +<code python>
 +hitpointsGrunty = hitpointsGrunty - 1
 +or more common because less to type:
 +<code python>
 +hitpointsGrunty -= 1
-====== Source Code on Github ======+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.
-To start this example ​you need:+=== 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>​
-^  file  ^  in folder ​ ^  download ​ ^  comment ​ ^ 
-|  [[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 | 
 +=== test ===
 +Tricky question: How many hitpoints has Grunty left now ?
-\\ +Find out by either coding in your pyhton shell or online (just click on the graphic): ​\\ 
-===== python 2.x =====+[[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}}]]
 +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 ===
-View/​Edit/​Download ​the file directly in Github: https://​​horstjens/​ThePythonGameBook/​blob/​master/​python/​ ​+If you click the grey "​forward"​ button on the online example above you can see how the values of the variables change each timeIF you work with the python ​shell, you must tell python to show the value of a variable by typing the variable name:
-//click reload ​in your browser if you see no code here:// +Do this in the python shell
-<html+<code python
- <script src="​http://​​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​"​+>>>​hitpointsGrunty 
- </script></​html>+</code>
-^ [[:​en:​python:​step001| ← previous]] ^ [[en:​python:​start| ↑ Overview]] ^ [[:​en:​python:​step003| → next ]] ^ +Later, you will learn other methods to inspect variables, like the print() function and how to work with debuggers.
-====== Comment this Page ====== +
-~~DISQUS~~ +
 +Generally, if you want to print the value of a variable, you code:
 +<code python>
 +| [[step001|←]] ​ | step 002 [[start|↑]] ​ |  [[step003|→]] ​ |
/var/www/horst/ · Last modified: 2014/01/09 11:07 (external edit)