The Game Loop

What is a loop? The loop's meaning of this is repeating itself. This operation could depend on a specific value or it can take forever. We could want it works 40 times or 600 times or forever. Well, the game loop will be run forever until we close the game. The game loop is the most important structure that will keep our game active. Everything about the game will be updated, drawn in the game loop. If the game loop starts, the game is on, if it ends, the game is done. The game loop is the thing that keeps alive our game. 

Let's get to work. I'm going to create a new folder for the game and it will contain the file named main.py. This file contains us game code. We are going to use pygame module. If you don't have any idea about it, you can look at its documentation.

The first thing we should import to pygame:
import pygame
After that, we are going to initialize it:
pygame.init()
Create a surface represent our game window. 640x480 is the game resolution and they are optional as values. We have to pass resolution argument as tuple:
screen = pygame.display.set_mode((640, 480))
I'm going to create main function which contains the game loop.
def main():
    running = True
    # the game loop
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        screen.fill((255,255,255))
        pygame.display.flip()
The variable called running is our key of the game loop. If it is true, the loop run and all the statements in the loop run againg and againg until the loop ends. The for loop is the event handling cycle. I typed this loop because if I dont, pygame doesn't let the program works as well. So, we get the event about quit option and if user wants to close program, the running variable is will be false and the game loop will be done. screen.fill((255,255,255)) statement fills the screen with white color. (255,255,255) is the rbg value and represent of white color. pygame.display.flip() is used for update all the display for every frame. In addition, there is a pygame.display.update() but it's not the same exactly.
if __name__ == "__main__":
    main()

pygame.quit()
What is this statement if __name__ == "__main__":. It provides modules we created can be executed as main file. It gives test oppurtinity fast. Well, it's not really important thing for now. We couldn't use it. pygame.quit() off pygame module.

Run this program and we will get a white window on the screen. It's okay but we can't see clearly how to works the game loop. So, let's make an example about it. I'm going to draw a circle on the screen. We can use draw functions of pygame. That function is pygame.draw.circle(screen, color, position, radius). I want to draw the red circle on the center of the screen:
def main():
    running = True
    # the game loop
    while running:
        for event in pygame.event.get():
            ...
            
        # draw
        screen.fill((255,255,255))
        pygame.draw.circle(screen, (255,0,0), screen.get_rect().center, 20)
        
        # update
        pygame.display.flip()
And we run this code. We are getting the view of the game:
Pygame Game Programming

That's good. Let's give a movement to this red circle. I'm going to use random module. For each frame, the circle's position will be changed by randint function and gives movement motion to the circle:
def main():
    running = True
    # the game loop
    while running:
        for event in pygame.event.get():
            ...
            
        # draw
        screen.fill((255,255,255))
        pygame.draw.circle(screen, (255,0,0), (random.randint(0, 640), 240), 20)
        
        # update
        pygame.display.flip()
Don't remember import random statement. The result what we got it:
Pygame Animation Circle

That's enough for this post. The next post will be about adding a player to the game and we are going to control it in the game. 

The full of source code:
import pygame
import random

# we initialize pygame module
pygame.init()

# create a surface represent our window
screen = pygame.display.set_mode((640, 480))

def main():
    running = True
    # the game loop
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        # draw
        screen.fill((255,255,255))
        pygame.draw.circle(screen, (255,0,0), (random.randint(0, 640), 240), 20)
        
        # update
        pygame.display.flip()

if __name__ == "__main__":
    main()

pygame.quit()

No comments:

Post a Comment