02b | Game Loop

The game loop part is responsible for updating the positions of the game objects and checking for collisions.

The game loop is like the heartbeat of the game, running repeatedly until the game is closed.

In each iteration of the game loop, we'll draw the game objects in their new positions and check for collisions, all happening so fast that it appears as a continuous movement.

Game loop: Three essential steps (06:43)

The game loop has three essential steps:

  1. Event Handling code: checks for any events that occur in the game, such as quitting the game, a key pressed on the keyboard, etc. This is done using the Pygame event handling system.

  2. Updating Positions code: updates the positions of all game objects, such as the snake and food, based on the events we detected with event handling

  3. Drawing Objects code: draws all the game objects in their new positions on the screen. This step uses the Pygame graphics functions to render the objects in the display.

By following these three steps, we can create a dynamic and interactive game that responds to user inputs and updates its state accordingly.

We start the game loop with a while loop like this:

while True: 

The while loop runs continuously until we close the game. At each iteration of the loop, it performs the three key steps.

It's important to note that before running the game, we need to ensure that the code inside the while loop has been fully written. If we run the code at this point, the while loop will run indefinitely (infinite loop) since we have not defined a way to stop its execution. So, in the next step, we'll add the necessary code to stop the while loop.

for event in pygame.event.get(): 

This line of code gets all the events that pygame recognizes and happened since the last time the while loop ran and puts them in a list. Then we loop through the list of events and check if any of the events is the QUIT event.

The QUIT event is when we click on the close button of the window. If the event is the QUIT event we break out of the while loop.

if event.type == pygame.QUIT: pygame.quit() 

and then we have to close the program completely with the command

sys.exit()

This command is part of the sys module, so we have to import it like this.

In the first line of the code after importing pygame:

import pygame

we type comma (,) sys like this:

import pygame, sys

That’s it. Now we have defined a way to exit the game, but we need a couple more lines of code before running our game for the first time. We have to update the screen with this line of code outside of the for loop:

pygame.display.update() 

This line of code takes all the changes we made in the game objects and draws a picture from them. Since we haven’t created any game objects yet this line just draws a black screen.

Remember the clock object we created in the beginning? We need to use it now.

We have to tell the clock object how fast the game should run. We do that by using the tick() method.

The tick() method takes an integer as an argument and that integer is the number of frames per

second that we want. I am going to use 60 frames per second.

clock.tick(60) 

This means that the while loop, and all the code inside it, will run 60 times every second.

If we didn't set a frame rate, the game would run as fast as the computer can handle, which could lead to inconsistencies in the game's speed. By setting the frame rate, we make sure that the game runs smoothly and at the same speed for all players. Now that the game loop is complete we can run the game for the first time.

If we run the game at this point we won’t see anything because we haven’t drawn anything yet. But, the important thing is that we can reliably close the game by clicking on the X icon.

Make game Window Green

The game window is black; let’s make it green.

Pygame provides several built-in colors, but we will be defining our own colors for this game. In Pygame, colors are represented as a tuple of three values, each value representing the amount of red, green, and blue in the color. Our game will have a retro look, so we will be using only two colors - GREEN and DARK_GREEN.

We will create two tuples to store the values of these colors: So at the beginning of our code we define the colors like this:

GREEN = (173, 204, 96) DARK_GREEN = (43, 51, 24) 

Now, let’s paint the game screen GREEN. Inside the game loop, before updating the screen we call the fill method of the screen object like this.

screen.fill(GREEN) 

The fill method just fills the display surface we have created, our canvas with the color we define, in this case, GREEN. If we run the game now, we can see, the game window is now GREEN.

We just completed a very basic game loop, so we can remove step #2 from our list.

Now, let’s work on adding food.