Game Development Reference
In-Depth Information
arrow from the box to another box. Your finger is kind of like the program execution.
Your finger will trace out a path from box to box, until finally your finger lands on the
"End" box. As you can see, when you get to the "Check for friendly or hungry dragon" box,
the program could either go to the "Player wins" box or the "Player loses" box. Either way,
both paths will end up at the "Ask to play again" box, and from there the program will
either end or show the introduction to the player again.
Summary
In the "Dragon Realm" game, we created our own functions that the main section of the
program called. You can think of functions as mini-programs within our program. The code
inside the function is run when our program calls that function. By breaking up our code
into functions, we can organize our code into smaller and easier to understand sections. We
can also run the same code by placing it inside of a function, instead of typing it out each
time we want to run that code.
The inputs for functions are the arguments we pass when we make a function call. The
function call itself evaluates to a value called the return value. The return value is the
output of the function.
We also learned about variable scopes. Variables that are created inside of a function
exist in the local scope, and variables created outside of all functions exist in the global
scope. Code in the global scope can not make use of local variables. If a local variable has
the same name as a variable in the global scope, Python considers it to be a separate
variable and assigning new values to the local variable will not change the value in the
global variable.
Variable scopes might seem complicated, but they are very useful for organizing
functions as pieces of code that are separate from the rest of the function. Because each
function has it's own local scope, we can be sure that the code in one function will not
cause bugs in other functions.
All nontrivial programs use functions because they are so useful, including the rest of the
games in this topic. By understanding how functions work, we can save ourselves a lot of
typing and make our programs easier to read later on.
Search WWH ::




Custom Search