Loading video player…

Importing and Initializing

In this lesson, you’ll start writing the code for your game. To start, create a new file named sky_dodge.py. Your first step will be importing pygame, and then you’ll also need to initialize it. This allows pygame to connect its abstractions to your specific hardware:

 1# Import the pygame module
 2import pygame
 4# Import pygame.locals for easier access to key coordinates
 5# Updated to conform to flake8 and black standards
 6from pygame.locals import (
 7    K_UP,
 8    K_DOWN,
 9    K_LEFT,
10    K_RIGHT,
11    K_ESCAPE,
12    KEYDOWN,
13    QUIT,
16# Initialize pygame

For more information about the local constants, check out the pygame documentation. For more information about the Flake8 and Black coding formatters, check out the following resources:

00:00 It’s time to start typing some code in, and you’ll start by importing and initializing PyGame. To get started, you need a file to be putting all your code into. I’ve come up with a name for it: it’s going to be called Sky Dodge. So give it a name, sky_dodge.py, and this is where you’re going to be putting all your code.

00:27 Right off the bat, you need to import pygame, and after that, you’re going to import a few other things from the pygame module.

00:35 A thing called pygame.locals is going to provide easier access to key coordinates that you’re going to use.

00:50 And this is just a note to say that the original code was updated to conform to Flake8 and Black standards. Both of those are code-formatting tools and I’ll include links if you would like to learn more about those standards in the text below this video.

01:09 In the next set of lines, you’re going to import from pygame.locals a set of what are called local constants. These constants are values for keypresses—like Up and Down, Left and Right, and the Escape.

01:24 What that’s going to do is avoid having to type the longer version of pygame. and then the name of the constant. You’ll be able to just to type out the constant’s name, creating a shortcut to the namespace for these values.

01:36 And these are common values that you’ll use in your own games as you create them. So, this will import all of those, and you’re going to be using these as you go through the tutorial.

01:52 Here at line 17, you’re going to init pygame. And as you can see here, it initializes all those imported pygame modules. Great! Go ahead and save.

02:07 The next lesson is going to dive into setting up the display.

Become a Member to join the conversation.