Book Image

Instant Pygame for Python Game Development How-to

By : Ivan Idris
Book Image

Instant Pygame for Python Game Development How-to

By: Ivan Idris

Overview of this book

<p>Pygame is a library created to make multimedia software documenting easy to design. It adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the Python language. Pygame comes with functions and tools that will help you create a great user experience."Instant Pygame for Python Game Development How-to" is written in a concise and result-oriented format. Leading you through practical recipes, you'll find that this essential reference guide helps you to create visually appealing and thrilling games with a few clicks.This book starts with the basic instructions to install Pygame on different servers. It then goes into creating a sample game and explaining the features of drawing, animating, using fonts and Matplotlib with Pygame. The book then takes you through recipes to get access to some great sound and graphic effects. Giving you the steps to allow you to configure these games on Android and other networks, it ends with a walkthrough of the features of Sprites, OpenGL, and Simulation.</p>
Table of Contents (7 chapters)

Running a simple game (Simple)


We will create a simple game that we will improve on further in the book. As is traditional in books about programming, we will start with a Hello World! example. It's not a game per se. 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's Surface object, that is used for drawing and we will handle a quit event.

How to do it...

  1. Imports: First we will import the required Pygame modules. If Pygame is installed properly, we should get no errors, otherwise please return to the Preparing your development environment (Simple) recipe:

    import pygame, sys
    from pygame.locals import *
  2. Initialization: We will initialize Pygame by creating a display of 400 by 300 pixels and setting the window title to Hello world:

    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    
    pygame.display.set_caption('Hello World!')
  3. The main game loop: 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 a font size of 19, red color, and falls back to the default font:

    while True: 
       sys_font = pygame.font.SysFont("None", 19)
       rendered = sys_font.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 the end result:

    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()

How it works...

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

Function

Description

pygame.init()

This function performs the initialization and needs to be called before any other Pygame functions are called.

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

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

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

This function sets the window title to a specified string value.

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

This function 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 function draws text on a surface. The second parameter indicates whether anti-aliasing is used. The last parameter is a tuple representing the RGB values of a color.

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

This function draws on a surface.

pygame.event.get()

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

pygame.quit()

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

pygame.display.update()

This function refreshes the surface.