Time for action – creating a simple game

It's important to notice the so-called main game loop where all the action happens and the usage of the Font module to render text. In this program we will manipulate a Pygame Surface object that is used for drawing, and we will handle a quit event. Perform the following steps to create a simple game:

  1. First import the required Pygame modules. If Pygame is installed properly, we should get no errors, otherwise please return to the installation recipe.
    import pygame, sys
    from pygame.locals import *
  2. We will initialize Pygame, create a display of 400 x 300 pixels, and set the window title to Hello World!.
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    
    pygame.display.set_caption('Hello World!')
  3. Games usually have a game loop, which runs forever until for instance a quit event occurs. In this example we will only set a label with the text Hello World at coordinates (100, 100). The text has font size of 19 and a red color.
    while True: 
        sysFont = pygame.font.SysFont("None", 19)
        rendered = sysFont.render('Hello World', 0, (255, 100, 100))
        screen.blit(rendered, (100, 100))
    
        for event in pygame.event.get():
          if event.type == QUIT:
            pygame.quit()
            sys.exit()
    
        pygame.display.update()

    We get the following screenshot as an end result:

    Time for action – creating a simple game

    The following is the complete code for the "Hello World" example:

    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    
    pygame.display.set_caption('Hello World!')
    
    while True: 
        sysFont = pygame.font.SysFont("None", 19)
        rendered = sysFont.render('Hello World', 0, (255, 100, 100))
        screen.blit(rendered, (100, 100))
    
        for event in pygame.event.get():
          if event.type == QUIT:
            pygame.quit()
            sys.exit()
    
        pygame.display.update()

What just happened?

It might not seem like much, but we learned a lot in this tutorial. The functions that passed the review are summarized in the following table:

Function

Description

pygame.init()

This function does initialization and needs to be called before other Pygame functions are called.

pygame.display.set_mode((400, 300))

This creates a so-called Surface object to draw on. We give this function a tuple representing the dimensions of the surface.

pygame.display.set_caption('Hello World!')

This sets the window title to a specified string value.

pygame.font.SysFont("None", 19)

This creates a system font from a comma-separated list of fonts (in this case. none) and a font size parameter.

sysFont.render('Hello World', 0, (255, 100, 100))

This draws text on a Surface object. The last parameter is a tuple representing the RGB values of a color.

screen.blit(rendered, (100, 100))

This draws on a Surface object.

pygame.event.get()

This gets a list of Event objects. The Event objects represent some special occurrence in the system, such as a user quitting the game.

pygame.quit()

This cleans up resources used by Pygame. Call this function before exiting the game.

pygame.display.update()

This refreshes the surface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.179