Game Design in PY Games

Updated: February 17, 2022
No Credit Card Required


Hi, are you ready for a very interesting lesson. In this unit, we're going to make a basic game where the goal is to dodge incoming obstacles. Sounds cool, right?

Before we start, let's do a quick review of what we covered in the last lesson.

1. We covered how to use a Python Library by importing it with import.

2. Then we created a game Loop using a While Loop inside the main game Loop.

3. We created a For Loop to iterate through all the PY Game Events.

4. Next, we check to see if any of those Events involves clicking the X in the corner of our game window. If that Event became true, we quit our game.

5. Then, we used the FILL Method to fill our game background with a color

6. As well as the CIRCLE Method to create a circle.

7. Finally, to refresh our circle, we use the FLIP Method.

8. And once the Loop ended, we quit PY Game using the QUIT Method.

So now that we know about how to get PY Game running, we want to focus on the game that we're actually going to make. The game we're going to make is a basic game, where the goal is to dodge incoming obstacles. In this lesson, we'll be setting up our game so we can start adding sprites in later lessons.

For today's lesson, please go to and create a new Repl and name it PGD_10. Import PY Game and then Initialize it like we did last lesson.

You can test that your set up is correct by adding the example code to your main.pi. Now you need something to draw on. Let's create a screen to be the overall canvas for our game. We create the screen to use by calling PY Game.display.set_mode and passing the desired width and height. Here's the syntax. The window is 800 x 600 as defined by the constants, screen width, and screen height. Add the code below PYGame.init and you should see a new window pop up.

Next, let's add a Loop to control gameplay. We did this last lesson. Add this code to your editor. Look familiar? You probably remember it from the last lesson. Key presses, mouse movements, and even joystick movements are some of the ways in which a user can provide input. All user input results in an Event being generated. Dealing with Events is referred to as handling them. And the code to do so is called an EVENT HANDLER.

You access the list of all active Events in the queue by calling PYGame.event.get. Go ahead and call this Event in a new editor window. Loop through this list. Inspect each Event type and familiarize yourself with what's there. If the event.type is equal to a key down, then we can check which key it is using. Here we use event.key to exit the While Loop using a conditional statement. Add this code here to your editor.

Huh? What if the user closes the window? Well, we'd also want to stop the game. Remember the ELIF Function it comes in handy here.

When you add these lines to the previous code and run it, you'll see a window with a blank or black screen. Press the Escape key to close the window.

Today we introduced Events which allow us to add key presses, mouse movements and even joystick movements to your game.

Alright, nice work today. I'll see you again soon.