Game Development Reference
In-Depth Information
Topics Covered In This Chapter:
The
time
module.
The
time.sleep()
function.
The
return
keyword.
Creating our own functions with the
def
keyword.
The
and
and
or
and
not
boolean operators.
Truth tables
Variable scope (Global and Local)
Parameters and Arguments
Flow charts
Introducing Functions
We've already used two functions in our previous programs:
input()
and
print()
.
In our previous programs, we have called these functions to execute the code that is inside
these functions. In this chapter, we will write our own functions for our programs to call. A
function is like a mini-program that is inside of our program. Many times in a program we
want to run the exact same code multiple times. Instead of typing out this code several
times, we can put that code inside a function and call the function several times. This has
the added benefit that if we make a mistake, we only have one place in the code to change
it.
The game we will create to introduce functions is called "Dragon Realm", and lets the
player make a guess between two caves which randomly hold treasure or certain doom.