There is still something to do before adding more complex combat mechanics: making sure that the Goblin Dice Duel works when merged with the code from the previous site. This means managing all the new functions as well as the old existing functions.
In many python programs, you will find a lot of functions defined and nearly nothing else. The only code not hiding inside a function (or later, inside a class) will look like this:
if __name__ == "__main__": main()
This code is not indented and does not belong to any function or class. such code is executed as soon as python opens the
.py file that include such code. It's code that only belong to this python file (or python module).
By testing the (always existing, internal) variable
__name__ for equality with
"__main__" you can find out if a python module was started directly (
True) or imported by another python module (
False). If the module was started directly you usually want to execute the module's most important function; In the example above a function with the named
main() is called. In the Goblin Dice Duel example below, i named the core function
As you can see in this updated Goblin Dice Duel code below, only 2 (!) code lines are on module level:
if __name__ == "__main__":test
Everything else is stuffed inside functions.
click reload on your browser if you see no source code here or visit Github.com
What does the
pass command in the last line do ? Well…exactly nothing. Because of python's syntax, you can not simply end a line with a colon and then stop coding. You need at least a
pass command to indicate that “here happens nothing”. It is usually typed for function or branches of functions that you have no time to code at the moment but will eventually code later. If the python interpreter runs into a
pass command he does nothing at all.
Also see the online documentation for pass.
As you can see in the output below the actual version of Goblin Dice Duel lists the new stats attack and defense for both players but does nothing with them (beside comparing in a table). It is time for some advanced combat mechanics…
--- Goblin Dice Duel --- Stiny | vs. | Grunty ---------------+-----+----------- hitpoints: 22 | < | 43 attack: 6 | > | 5 defense: 9 | > | 3 round hitpoints 0 Stinky: 22 Grunty: 43 1 Stinky: 22 Grunty: 42 2 Stinky: 20 Grunty: 38 3 Stinky: 17 Grunty: 36 4 Stinky: 13 Grunty: 31 5 Stinky: 11 Grunty: 29 6 Stinky: 5 Grunty: 25 7 Stinky: 2 Grunty: 24 8 Stinky: 1 Grunty: 24 9 Stinky: -1 Grunty: 18 Game Over Grunty wins thank you for playing Goblin Dice Duel. bye-bye!