ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step002

Differences

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
 +</​code>​
 +or more common because less to type:
 +<code python>
 +hitpointsGrunty -= 1
 +</​code>​
  
-====== 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://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins002.py|Github.com]] // 
 +<​html><​script src="​http://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins002.py?​footer=minimal"></​script></​html>​
  
-^  file  ^  in folder ​ ^  download ​ ^  comment ​ ^ 
-|  [[https://​github.com/​horstjens/​ThePythonGameBook/​raw/​master/​python/​001_goblindice.py|002_goblindice.py]] ​ |  python ​ |  Download the whole Archive with all files from Github: ​ \\  https://​github.com/​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://​www.pythontutor.com/​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://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​python/​002_goblindice.py ​+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://​gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​002_goblindice.py"​+>>>​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>
 +print(hitpointsGrunty)
 +</​code>​
 +
 +| [[step001|←]] ​ | step 002 [[start|↑]] ​ |  [[step003|→]] ​ |
/var/www/horst/thepythongamebook.com/data/attic/en/python/goblins/step002.1323618046.txt.gz · Last modified: 2014/01/09 11:07 (external edit)