Pygame Framework

The pygame library contains several modules and classes that you will use when creating video retro arcade games.

You will need to familiarise yourself with the most useful modules and classes as listed below: (Click on a module/class to access a description of the module/class)
pygane-framework
Zoom in / Open pygame framework in new window pygame top level module pygame.display module pygame.time module pygame.event module pygame.mouse module pygame.key module pygame.mixer module pygame.music module pygame.sprite module pygame.draw module pygame.transform module Sprite Class Surface Class Clock Class Event Class Group Class Rect Class Font Class Sound Class

You can access the Pygame online documentation to gain a better understanding of the main functions/methods and attributes from this framework and access relevant code examples.

Please note that the following list of modules classes and their matching functions/methods is only a selection of some of the most useful features of the pygame library. It is not an exhaustive list.

Modules/Classes Description Pygame Documentation
pygame-pygame pygame.init()
Initialise all imported pygame modules.

pygame.quit()
Uninitialise all pygame modules.

Access online documentation
pygame-sprite Sprite()
The base class for all the sprites of your game.

Group()
A container class to hold multiple Sprite objects.

spritecollide()
List all the sprites in a group that collide with a specified sprite.

groupcollide()
Find all sprites that collide between two groups.

collide_mask()
Check if two sprites are colliding (using mask/transparency).

Access online documentation
pygame-sprite-class alive
Does this sprite belong to any group? (Boolean value)

groups
A list of all the groups that contain this sprite.

update()
A method that can be overridden (in a derived class) to implement the behaviour of a sprite.

add()
Add this sprite to the specified group(s).

remove()
Remove this sprite from the specified group(s).

kill()
Remove the sprite from all groups.

Access online documentation
pygame-group-class sprites
A list of all the sprites contained by this group

add()
Add the specified sprite(s) to this group.

remove()
Remove the specified sprite(s) from this group.

has()
Check if specified sprite(s) are in this group.

update()
Call the update() method on all the sprites from this group.

draw()
Blit all the sprites images,

clear()
Draw the specified background over the sprites from this group.

empty()
Remove all sprites from this group.

Access online documentation
pygame-display init()
Initialise the display module.

quit()
uninitialise the display module.

set_mode()
Initialise a window or a screen for display using the specified size. This function returns a Surface object.

get_surface()
Get a reference to the currently set display surface.

flip()
Update the full display Surface to the screen.

update()
Update portions of the screen for software displays.

set_icon()
Change the system image for the display window.

set_caption()
Set the current window caption.

get_caption()
Get the current window caption.

get_window_size()
Return the size of the window or screen as a tuple: (width, height).

Access online documentation
pygame-draw line()
Draw a line on the specified surface.

rect()
Draw a rectangle on the specified surface.

polygon()
Draw a polygon on the specified surface.

circle()
Draw a circle on the specified surface.

ellipse()
Draw an ellipse on the specified surface.

Access online documentation
pygame-transform flip()
Flip the specified horizontally and/or vertically.

scale()
Resize the specified surface to a new resolution (width,height).

rotate()
Rotate a specified surface using a specified angle (in degrees).

Access online documentation
pygame-surface-class blit()
Draw a specified image/surface on this surface.

convert()
Change the pixel format of this surface/image.

convert_alpha()
Change the pixel format of this surface/image including per pixel alphas to support opacity/transparency.

fill()
Fill this surface with a solid color

set_colorkey()
Set the transparent colorkey.

set_alpha()
Set the alpha value for the full Surface image. (To allow transparency)

get_width()
Get the width of this surface.

get_height()
Get the height of this surface.

get_rect()
Returns the rectangular area (Rect object) of this Surface.

get_at()
Set the colour value for a single pixel identified using its (x,y) coordinates.

Access online documentation
pygame-rect-class x,y
Used to set or retrieve the x and y coordinates of the rectangle object. Other properties can also be used such as top, left, bottom, right, topleft, bottomleft, topright, bottomright, midtop, midleft, midbottom, midright, center, centerx, centery.

width
Used to set or retrieve the width coordinates of the rectangle object.

height
Used to set or retrieve the height coordinates of the rectangle object.

copy()
Create a copy the rectangle object.

move()
Move a copy the rectangle object to the specified (x,y) coordinates.

inflate()
Grow or shrink the rectangle size.

collidepoint()
Check if a point (identified using (x,y) coordinates) is inside a rectangle.

colliderect()
Check if this rectangle object overlaps with another specified rectangle object.

collidelist()
Check if this rectangle object overlaps with any other specified rectangle objects.

Access online documentation
pygame-font-class render()
Add text on a new surface.

set_underline()
To underline the text!

set_bold()
To render the text in bold!

set_italic()
To render the text in italic!

Access online documentation
pygame-time wait()
Pause the program for a specified amount of milliseconds.

delay()
Pause the program for a specified amount of milliseconds.

get_ticks()
Return the number of milliseconds since pygame.init() was called.

set_timer()
Set an event type to appear on the event queue every given number of milliseconds.

Clock()
Create a clock object to help track time and set the desired frame rate.

Access online documentation
pygame-clock-class tick()
Update the clock. Should be used once per frame.

get_time()
Return the number of milliseconds that passed between the previous two calls to Clock.tick().

get_fps()
Compute your game’s framerate (in frames per second) by averaging the last ten calls to Clock.tick().

Access online documentation
pygame-event Event()
Instantiate a new event object.

get()
Get (and remove) all the events from the queue. Type(s) of events to be retrieved can be specified.

wait()
Wait for a single event from the queue. The event will be returned and removed from the queue. While the program is waiting it will sleep in an idle state.

clear()
Remove all events from the queue. Type(s) of events to be removed can be specified.

Access online documentation
pygame-event-class type
The type of event such as QUIT, KEYDOWN, KEYUP, MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP, etc.

key
To find out which key has been pressed.

pos
To find out the position of the mouse cursor using (x,y) coordinates.

Access online documentation
pygame-mouse get_pressed()
Returns a list of Boolean values representing the state of all the mouse buttons.

get_pos()
Returns the position of the mouse cursor, using (x,y) coordinates.

set_pos()
Sets the position of the mouse cursor, using (x,y) coordinates.

set_visible()
Hide or show the mouse cursor.

set_cursor()
Change the image for the mouse cursor.

Access online documentation
pygame-key get_pressed()
Returns a list of Boolean values representing the state of all the keyboard keys.

get_mods()
Used to determine which modifier key(s) are being held.

name()
Returns the name (string value) of a key identifier.

Access online documentation
pygame-music load()
Load a music file for playback.

unload()
Unload the currently loaded music to free up resources.

play()
Start the playback of the music stream.

rewind()
Reset the playback of the current music to the beginning.

pause()
Temporarily stop music playback.

unpause()
Resume the previously paused playback.

fadeout()
Stop music playback with a fading out effect over a specified number of milliseconds.

stop()
Stop the music playback.

get_volume()
Set the music volume to a value between 0.0 and 1.0.

set_volume()
Return the current volume for the music. The value will be between 0.0 and 1.0.

queue()
Queue a sound file to follow the current. Only one sound file can be queued at a time.

Access online documentation
pygame-mixer pre_init()
Preset the mixer init arguments

init()
Initialise the mixer module.

pause()
Temporarily stop playback of all sound channels.

unpause()
Resume paused playback of sound channels.

fadeout()
Fade out the volume on all sounds before stopping.

stop()
Stop playback of all sound channels.

quit()
Uninitialise the mixer.

Sound()
Create a new Sound object from a file or buffer object.

Access online documentation
pygame-sound-class play()
Begin sound playback.

stop()
Stop sound playback.

fadeout()
Stop sound playback after fading out.

set_volume()
Set the playback volume for this Sound using a value between 0.0 and 1.0.

get_volume()
Get the playback volume for this Sound using a value between 0.0 and 1.0.

get_length()
Get the length in seconds of this Sound.

Access online documentation

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 10

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: