02 | Setup

Blank Screen (02:47)

The next step in building our Snake game is to create a blank screen. Before we start, it's important to understand the structure of a game.

The game consists of two main parts: definitions and the Game Loop. We will cover the game loop in 02b: Game Loop

Definitions

The definitions part is where we define our variables and game objects, such as the snake, the food, and the arena (screen)

First of all we need to initialize pygame so we type:

pygame.init() 

Then we create a display surface. A display surface is like a blank canvas where we can draw our game objects. We need to specify the width and the height of the display surface. We will use a size of 750 x 750 pixels for now. We will change that later. Let’s create the display surface:

display surface.screen = pygame.display.set_mode((750,750)) 

This line of code creates a display surface object named screen. The set_mode() method takes a tuple as an argument. The first value is the width and the second value is the height of the display surface, our canvas. In simpler terms, we just created the game window and to draw on it, we use a coordinate system similar to the one taught in high school. Each point on this window has its unique x and y values.

Coordinate System:

Computer Graphics vs. Cartesian System (04:52)

The coordinate system used in computer graphics differs from the standard Cartesian coordinate system taught in high school. In computer graphics, the origin of the display is located at the top-left corner, with (0,0) being at the top-left. In contrast, the standard Cartesian coordinate system starts from the bottom-left corner, with (0,0) being the origin. In this system, the x-coordinate increases as we move to the right and the y-coordinate increases as we move up. However, in the top-left corner coordinate system used in computer graphics, the x-coordinate increases as we move to the right and the y-coordinate increases as we move down.

This means that a point at (10, 10) would be located 10 units to the right and 10 units down from the origin. We'll be using the top-left corner coordinate system in our Snake game. That’s why it's important to understand the differences between these two coordinate systems, Now that we have created the screen we need to give it a title. We do that by using the set_caption() function:

pygame.display.set_caption(“Retro Snake”) 

Next we create a clock object like this:

clock = pygame.time.Clock()

Be careful, we need a capital C here.

We need this clock object to control the frame rate of the game, how fast the game will run.

Don’t worry about this line of code now, we will get back to it in a minute. The basic setup of the game is now complete. Let’s now work on the Game Loop.