ThePythonGameBook

learn Python. Create Games

User Tools

Site Tools


en:python:goblins:step007

Differences

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

Link to this comparison view

Next revision
Previous revision
en:python:goblins:step007 [2013/07/07 19:35]
Horst JENS created
en:python:goblins:step007 [2014/01/09 11:07] (current)
Line 1: Line 1:
-===== 007 ===== +===== 007 function ​===== 
  
-previous: ​[[step006]]+|  ​[[step006|←]]  ​| ​ step007 [[start|↑]] ​ |  [[step008|→]] ​ |
  
 ===== functions ===== ===== functions =====
Line 82: Line 82:
   * functions can have default parameters, that is a parameter followed by an <​key>​=</​key>​ and a default value. If the function is called without such an parameter, the default value is set. Note that if you mix non-default parameters and default parameters, the non-default parameters need to come first.   * functions can have default parameters, that is a parameter followed by an <​key>​=</​key>​ and a default value. If the function is called without such an parameter, the default value is set. Note that if you mix non-default parameters and default parameters, the non-default parameters need to come first.
  
-=== function demo ===+==== function demo ====
  
 Try out this function demo inside your python editor or inside your python shell. Change the  Try out this function demo inside your python editor or inside your python shell. Change the 
 values of a,b,c and d and play around with it: values of a,b,c and d and play around with it:
-<code python slowgoblins007demo.py>​ 
-def output(a,​b=20,​c=30):​ 
-    """​a docstring in triple quotes explaining that this function print out variables"""​ 
-    print("​inside the function. The parameters a,​b,​c:"​) 
-    print("​a:​ {} b: {} c: {}"​.format(a,​b,​c)) 
-    print("​i set now d locally to -100") 
-    d = -100 # a local d 
-    print("​the local variable d: {}"​.format(d)) 
-    print("​i manipulate locally a and double it") 
-    a *= 2 # the same as a = a * 2 
-    print("​local a is now: {}"​.format(a)) 
-    print("​returning a,b and c") 
-    return a,b,c 
  
-# calling the function +=== 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/​slowgoblins007demo.py|Github.com]] // 
-22 +<​html><​script src="http://gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins007demo.py?​footer=minimal">​</script></​html
-100 +=== output === 
-5000 +The output of this function ​demo:
- +
-print("​a,​b,​c,​d:",​a,​b,​c,​d) +
-a,b,c output(a) +
-print("a,b,c,d:",​a,​b,​c,​d) +
-print(output.__doc__) +
-</code+
- +
-The output of this function:+
 <​code>​ <​code>​
 a,b,c,d: 5 22 100 5000 a,b,c,d: 5 22 100 5000
Line 125: Line 104:
 a docstring in triple quotes explaining that this function print out variables a docstring in triple quotes explaining that this function print out variables
 </​code>​ </​code>​
- 
 === online example === === online example ===
  
Line 134: Line 112:
  
 It is time to improve the Gobling Dice Duel using a function for output: It is time to improve the Gobling Dice Duel using a function for output:
- +=== source code === 
-<code python slowgoblins007.py+//click reload on your browser if you see no source ​code here or visit [[https://​github.com/​horstjens/​ThePythonGameBook/​blob/​master/​python/goblins/slowgoblins007.py|Github.com]] // 
-def output(combatround,​hitpointsStinky,​hitpointsGrunty):​ +<​html><​script src="http://gist-it.appspot.com/​github/​horstjens/​ThePythonGameBook/​blob/​master/​python/​goblins/​slowgoblins007.py?​footer=minimal"></script></html>
-    """​printing out three values"""​ +
-    print("​{0:​2d} Stinky: {1:2d} Grunty: {2:​2d}"​.format(combatround,​ hitpointsStinky,​ hitpointsGrunty)) +
-    # no return necessary because this functions returns nothing (yet) +
-     +
-import random +
-hitpointsStinky ​22 +
-hitpointsGrunty = 43 +
-combatround = 0 +
-print(" ​--- Goblin Dice Duel ---"​) +
-print("​round ​   hitpoints"​) +
-while hitpointsStinky >0: +
-    output(combatround,​ hitpointsStinky,​ hitpointsGrunty) +
-    combatround += 1 +
-    hitpointsGrunty ​-= random.randint(0,​6) +
-    if hitpointsGrunty <0: +
-        break +
-    hitpointsStinky -= random.randint(0,​6) +
-output(combatround,​ hitpointsStinky,​ hitpointsGrunty) # output of final strike +
-print("Game Over"​) +
-if hitpointsStinky ​hitpointsGrunty:​ +
-    print("​Stinky wins"​) +
-elif hitpointsGrunty ​hitpointsStinky:​ +
-    print("​Grunty wins"​) +
-else: +
-    print("​Nobody wins ?") +
-print("​thank you for playing Goblin Dice Duel. bye-bye!"​) +
-</code>+
  
 === online === === online ===
  
 Of course you can try out this example online. Just click on the graphic below: \\ Of course you can try out this example online. Just click on the graphic below: \\
-[[http://​www.pythontutor.com/​visualize.html#​code=def+output(combatround,​hitpointsStinky,​hitpointsGrunty)%3A%0A++++%22%22%22printing+out+three+values%22%22%22%0A++++print(%22%7B0%3A2d%7D+Stinky%3A+%7B1%3A2d%7D+Grunty%3A+%7B2%3A2d%7D%22.format(combatround,​+hitpointsStinky,​+hitpointsGrunty))%0A++++%23+no+return+necessary+because+this+functions+returns+nothing+(yet)%0A++++%0Aimport+random%0AhitpointsStinky+%3D+22%0AhitpointsGrunty+%3D+43%0Acombatround+%3D+0%0Aprint(%22+---+Goblin+Dice+Duel+---%22)%0Aprint(%22round++++hitpoints%22)%0Awhile+hitpointsStinky+%3E0%3A%0A++++output(combatround,​+hitpointsStinky,​+hitpointsGrunty)%0A++++combatround+%2B%3D+1%0A++++hitpointsGrunty+-%3D+random.randint(0,​6)%0A++++if+hitpointsGrunty+%3C%3D+0%3A%0A++++++++break%0A++++hitpointsStinky+-%3D+random.randint(0,​6)%0Aoutput(combatround,​+hitpointsStinky,​+hitpointsGrunty)+%23+output+of+final+strike%0Aprint(%22Game+Over%22)%0Aif+hitpointsStinky+%3E+hitpointsGrunty%3A%0A++++print(%22Stinky+wins%22)%0Aelif+hitpointsGrunty+%3E+hitpointsStinky%3A%0A++++print(%22Grunty+wins%22)%0Aelse%3A%0A++++print(%22Nobody+wins+%3F%22)%0Aprint(%22thank+you+for+playing+Gobling+Dice+Duel.+bye-bye!%22)&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=9|{{:​en:​python:​goblins:​slowgoblins007.png|click me}}]]+[[http://​www.pythontutor.com/​visualize.html#​code=def+output(combatround,​hitpointsStinky,​hitpointsGrunty)%3A%0A++++%22%22%22printing+out+three+values%22%22%22%0A++++print(%22%7B0%3A2d%7D+Stinky%3A+%7B1%3A2d%7D+Grunty%3A+%7B2%3A2d%7D%22.format(combatround,​+hitpointsStinky,​+hitpointsGrunty))%0A++++%23+no+return+necessary+because+this+functions+returns+nothing+(yet)%0A++++%0Aimport+random%0AhitpointsStinky+%3D+22%0AhitpointsGrunty+%3D+43%0Acombatround+%3D+0%0Aprint(%22+---+Goblin+Dice+Duel+---%22)%0Aprint(%22round++++hitpoints%22)%0Awhile+hitpointsStinky+%3E0%3A%0A++++output(combatround,​+hitpointsStinky,​+hitpointsGrunty)%0A++++combatround+%2B%3D+1%0A++++hitpointsGrunty+-%3D+random.randint(0,​6)%0A++++if+hitpointsGrunty+%3C%3D+0%3A%0A++++++++break%0A++++hitpointsStinky+-%3D+random.randint(0,​6)%0Aoutput(combatround,​+hitpointsStinky,​+hitpointsGrunty)+%23+output+of+final+strike%0Aprint(%22Game+Over%22)%0Aif+hitpointsStinky+%3E+hitpointsGrunty%3A%0A++++print(%22Stinky+wins%22)%0Aelif+hitpointsGrunty+%3E+hitpointsStinky%3A%0A++++print(%22Grunty+wins%22)%0Aelse%3A%0A++++print(%22Nobody+wins+%3F%22)%0Aprint(%22thank+you+for+playing+Gobling+Dice+Duel.+bye-bye!%22)&​mode=display&​cumulative=true&​heapPrimitives=true&​drawParentPointers=false&​textReferences=false&​showOnlyOutputs=false&​py=3&​curInstr=9|{{:​en:​python:​goblins:​slowgoblins007.png?300|click me}}]]
  
 === output === === output ===
Line 190: Line 141:
 As you can see here, in his last action, Stinky managed to inflict one damage on the still mighty Grunty, but received 5 damage when only having 4 hitpoints left. As you can see here, in his last action, Stinky managed to inflict one damage on the still mighty Grunty, but received 5 damage when only having 4 hitpoints left.
  
-next: [[step008]]+|  [[step006|←]] ​ |  step007 [[start|↑]] ​ |  ​[[step008|→]]  |
/var/www/horst/thepythongamebook.com/data/attic/en/python/goblins/step007.1373218532.txt.gz · Last modified: 2014/01/09 11:07 (external edit)