Pygame Theory of Operation

Pygame Simplified Theory of Operation


The Pygame module contains many modules and functions. The previous section explained how to write a Pygane app.
The “Display an Image” program showed how to use the pygame module and its functions to display an image.

This section discusses the various modules and functions contained in the pygame module in detail

Import Pygame Statement

import pygame

This statement imports the pygame module including all functions related to graphics, sound, drawing, etc. that are contained in the pygame module.

Pygame Modules

Some of the pygame modules are:

  • Pygame.display: Pygame module to control the display window and screen
  • Pygame.image: Pygame module for image load
  • Pygame.draw: Pygame module for drawing shapes
  • Pygame.event: Pygame module for managing events
  • Pygame.font: Pygame module for loading and rendering fonts
  • pygame.key:  Pygame module to work with the keyboard
  • pygame.mouse: Pygame module to work with the mouse
  • Pygame.time: Pygame module for monitoring time in apps
  • Pygame.mixer: Pygame module for loading and playing sounds

There are many more modules in Pygame that are not listed above.  All these modules are automatically imported by the import pygame statement.

The programs in the following sections will use these modules and their functions and methods.

pygame.init()

pygame.init is a function call that initializes all pygame modules that are imported by the import pygame statement.

The function pygame.init() must be called after importing the pygame module. It must be executed before you call any other pygame functions.

pygame.quit()

pygame.quit() is the opposite of pygame.init(). It un-initializes all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called.

pygame.display.set_caption()

Pygame module has a module called Pygame.display module.

The  Pygame.display module has a method pygame.display.set_caption(). This method sets the current window’s caption. The caption appears on the window’s border. The display.set_caption() method’s argument is a string that is specified within the parentheses. This string represents the title of our game’s window.

pygame.display.set_mode((x, y)) function

Pygame.display module has a method set_mode()associated with it. When we call the set_mode() method, it returns a pygame.surface object window with dimensions (x, y) with its width = x and height = y.

The display.set_mode() function sets the app window’s width and height.  The Surface object returned by pygame.display.set_mode() is called the display Surface.  

Calling the display.set_mode() method is done by the statement below:

screen = pygame.display.set_mode((width, height), flags, depth)

width” = width of display screen in pixels.

height” = height of display screen in pixels.

Note (width, pixel) argument must be tuple data type.

depth” is the color depth and is specified in bits per pixel.  It is normally 8.  You can ignore the “depth” argument.  You can also ignore the “flags” argument.

As an example, the tuple (640, 480) will make the surface resolution 640 pixels wide and 480 pixels high. The code to create a 640×480 Surface is below.

>>> screen = pygame.display.set_mode((640, 480))

The Surface object returned by the set_mode() function is stored in the variable we named “screen”.

Note:  Anything that you draw on the display Surface object is not displayed on the screen until pygame.display.update() function is called. The pygame.display.update() function is discussed below. 

Pygame.display.flip() method

Pygame.display module has a method named flip().

Pygame.display.flip()method updates the full display Surface to the screen. This method will update the contents of the entire display.  When we are finished drawing, we call the pygame.display.flip() method. This makes everything we have drawn on the screen Surface become visible.

Pygame.display.update() Function

Pygame Display module has another function called update(). While pygame.display.flip() updates the entire display surface, the pygame.display.update() function allows only a portion of the screen to be updated, instead of the entire area. If no argument is passed, display.update() function updates the entire Surface area just like pygame.display.flip.

Note: Nothing is drawn to the screen until the pygame.display.update() function is called. Do not call this function after each drawing is done. Call this function to update the screen once all the drawing functions have been called.

blit() Method for Surface Objects

The blit() method copies data of one Surface object onto another Surface object.  The “blit” method allows us to copy graphics from one image to another. In other words, it copies the memory content from one memory location to another memory location.

There are two arguments within the parenthesis of the blit() method:

a) source Surface to copy from

b) the destination location where to paste the source memory contents.

To position an image at the destination location on the screen, we need to tell the blit() function where to put the image. This is done by passing the position’s (x, y) coordinates as a tuple.

To summarize, in the blit() method, we need to pass two arguments to the blit() function.

  1. The source Surface object variable name.
  2. The destination (x, y) where to put the source image object on the destination screen Surface object.  The destination coordinates (x,y) is tuple data type.

The above is explained in the line of code below.

>>> screen.blit(image object, (x, y))

Where “screen” is the apps display screen,

“image object” is the source Surface,

(x, y) are the coordinates of the destination location’s top-left corner of the image object.

Pygame.image.load Function

Pygame module has within it a module called the image module. Pygame.image module contains functions to transfer images in and out of Surfaces.

The pygame.image module has a function called load().  To load an image file (for example image named ball.png), we make a function call to the function pygame.image.load(). The name of the image to be loaded is specified as a parameter within the parenthesis of the function.

This function call returns a Surface object that has the image specified as the parameter. The image.load() function uses the file name specified in the parenthesis and returns a new Surface object with the loaded image.

Example of image.load Function

The following is an example of the code to load the images.  In this example, “ball.bmp” is the ball image and “bg.bmp” is the background image.

>>> screenSurface = pygame.display.set_mode((640, 480)) #1)

The above statement returns a Surface object screen of width = 640 and height = 480 pixels. The statement names this screen “screenSurface”.

>>> ball = pygame.image.load('ball.bmp') #2)

The above statement calls the image.load function that returns a new Surface object with the ball image (“ball.bmp”)

>>> background = pygame.image.load('bg.bmp') #3)

The above statement calls the image.load function that returns a Surface object with the background image (“bg.bmp”).

Note: Pygame supports these image formats: .png, .jpg, .gif, .bmp

Note: The two new image Surface objects returned by the image.load functions in 2) and 3) are separate Surface objects from the “screenSurface” object returned in 1).  To display the “ball.png” image and the “bg.png” image on the “screenSurface” object, you will need to blit the images surface objects on the “screenSurface” object, as described in the above section and the below example code.

>>> screenSurface.blit(ball, (x, y))

>>> screenSurface.blit(background, (0, 0)

(x, y) is a tuple that specifies the location on the “screenSurface” where the ball image should be copied.

The background image is placed at (0, 0), which is the left-top corner of the “screenSurface”.

Pygame.Surface.fill() Function:

Pygame.Surface.fill() function fills a Surface with a solid color.

Syntax: fill(color, rect=None)

If no rect argument is given, the entire surface will be filled with color. The rect argument limits the fill to a specific area. The color argument can be either an RGB sequence (a tuple) or a mapped color index (such as WHITE or YELLOW)

Pygame Time Module

Pygame provides a module called the time module, which is used for monitoring time. The pygame.time module has several methods some of which are discussed below.  We will use these methods in program development and on the Sprite Game Programming page.

pygame.time.Clock() Function

The pygame time module has a method called pygame.time.clock(). This method is used to create a new “clock” object. The clock object is the Main Loop’s clock.  The pygame.time.clock()is used in association with the clock.tick() method explained below.

Note: The Main Loop is also called the Event Loop.

The pygame.time.Clock function is used to set the main loop’s clock. This is done by putting pauses in the program after each loop through the while loop.

clock.tick() Method

pygame.time.clock() function has a method associated with it called tick(). A call to the tick() method introduces a pause in the program after each iteration of the loop. The pause ensures that the app runs at the specified frames-per-second (FPS) and does not run too fast.

The introduction of pauses is necessary. This is because the computers are so fast that without the pause, the game will run too fast for us to see.

The value in the parenthesis of the tick() method is the frame per second (FPS) parameter. 

The FPS value is normally specified as 60, which means that the program’s main loop redraws the display screen 60 times per second.

FPS is also referred to as refresh rate or frame rate.

For fast action-packed games, the FPS needs to be set to a value higher than 60. If FPS is less than 16, the motion of the objects during an animation will be jerky and not smooth.

The clock.tick() method is called once per iteration at the end of the Main Loop after the call to pygame.display.update() method.

Learn more about frame rate (FPS) in the Enrichment: More About Colors, Frame-rate, and Anti-aliasing in Pygame Drawing and Animation chapter.

Pygame time.set_timer() Method

Pygame.time module has a method called set_timer().  The pygame.time.set_timer() method can be used to repeatedly create an event on the event queue after a specified number of milliseconds.

See how time.set_timer() method is used in custom events USEREVENT description in the Sprite Game Programming chapter.

Syntax: set_timer(eventid, milliseconds)

The time.set_timer() method sets an event type to appear on the event queue every given number of milliseconds.

The events are discussed in detail on the next page “Pygame Events“.

The pygame.time.set_timer() method usage is explained in a few program examples in the Sprite Game Programming chapter.

In the next section we will discuss about Pygame Events.

Copyright © 2019 – 2024 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights