Game Development Reference
In-Depth Information
11 - Bagels
Figure 11-1: Flow chart for the Bagels game.
And here is the source code for our game. Start a new file and type the code in, and then
save the file as
bagels.py
. We will design our game so that it is very easy to change the size
of the secret number. It can be 3 digits or 5 digits or 30 digits. We will do this by using a
constant variable named
NUMDIGITS
instead of hard-coding the integer 3 into our source
code.
Hard-coding
means writing a program in a way that it changing the behavior of the
program requires changing a lot of the source code. For example, we could hard-code a
name into a
print()
function call like:
print('Hello, Albert')
. Or we could
use this line:
print('Hello, ' + name)
which would let us change the name that is
printed by changing the name variable
while
the program is running.
How the Code Works: Lines 1 to 9
At the start of the program we import the
random
module and also create a function for
generating a random secret number for the player to guess. The process of creating this
number isn't hard, and also guarantees that it only has unique digits in it.