Structuring Your Game
00:00 In the previous lesson, I showed you how to make very quick blue speckles in a dazzling display of randomness. In this lesson, I’ll show you how to structure your code files to get ready for larger programs.
The typical way of running a Python script is to use the
python command and pass it the script file, or if you’re on a Unix-based system, use the hash-bang (
#!) control at the top of the file, which is really just a shortcut for doing the same thing.
00:26 There is another way of doing things, though. You can run a directory instead of running a single file. This can be useful when you want to split your code up into multiple files and you don’t want your user to have to figure out which one is the one to run.
To use this directory running mechanism, you need a file named
__main__.py, also known as dunder main. The oh-so-clever name for our Asteroids clone is Space Rocks.
As there’s going to be several files for the code, you can create a directory named
space_rocks and put a
__main__.py file inside.
There will be other files in there as well, such as
game.py, where a lot of the game logic will live. With this structure in place, calling the Python interpreter with the
space_rocks directory as a parameter will result in your game running.
Very shortly, I’ll be showing you the first code for Space Rocks. First, though, make sure to create a directory where your files will live. You can do that with the
As I mentioned before, the first file you’ll need is
__main__.py. The contents of
__main__.py will be pretty simple. It imports and creates a
SpaceRocks object from the
game file and then runs its
All of this is done inside of a
__name__ check on line 4. If you haven’t seen this before, this
if statement is checking if the script is being run as code, as opposed to being loaded as a module. Inside of a
__main__.py file, this is likely redundant.
01:59 You’ll probably never load this file as a module, but it is best practice to do this and it signals to other developers that this is the program’s entry point.
The next file you’ll create is
game.py. This is where you’ll keep the main loop of the game and the core logic. The contents here aren’t that different from
blue.py in the previous lesson.
It’s just organized in a different way. The initializer for the
SpaceRocks object contains the Pygame
set_caption(), and screen size calls that get you going.
The surface from the
set_mode() call is being stored for later use inside of the
SpaceRocks object in
Line 12 defines the core game loop. Inside of here, you’ll find the
while loop. It checks for input, does any game logic, and draws a frame. No different than
blue.py, just reorganized to be a bit more clear.
In each iteration of the loop, you’re going to check for events that need handling. That’s the
._handle_input() method. Just like in
blue.py, I’m starting out here just checking for whether or not
QUIT was pushed.
03:14 The second part of the game loop, which is doing the game logic, is defined on line 23. This is just a placeholder for now. There isn’t any logic yet, but this will be where objects can be moved around.
03:26 Expect great spaceship and rock position logic here in the future. Let me just scroll down. The final part of the game loop is the drawing. This first version of Space Rocks does nothing but draw a blue background.
Not all that exciting, but imagine the frame rate you’re going to get with so little happening in each loop. Let me call
python with the
space_rocks directory, and…
03:54 Isn’t this exciting? Not exactly the easiest game to win, but you’re ready to go! With this, a multi-million dollar budget, and several hundred developers, you could be writing Call of Duty in no time. Okay, enough with the Call of Duty references.
04:08 I promise the next one will be all about Grand Theft Auto.
04:13 Don’t tell Bill Gates I said so, but blue screens are so passé. Next up, I’ll show you how to import graphics. Ultra-violence and drive-by shootings, here you come. Told you there’d be a GTA reference.
Become a Member to join the conversation.