Pygame Zero Helper

Pygame Zero lacks a number of functions that are present in Scratch and commonly used in games. The Pygame Zero Helper library aims to remedy this, by providing the missing functions.

Download it here.

To use it, add from pgzhelper import * to the top of your Python program, just after import pgzrun. Example…

import pgzrun
from pgzhelper import *

alien = Actor('alien')
alien.flip_x = True

def draw():
  alien.draw()

pgzrun.go()

Functions provided by pgzhelper includes…

Actor.flip_x, Actor.flip_y

Flips the image in the x or y direction. Example…

alien = Actor('alien')
alien.flip_x = True

Actor.scale

Scale the image. Takes a float. Example…

alien = Actor('alien')
alien.scale = 0.5  # Half size
alien.scale = 2    # Double size

Actor.move_forward(), Actor.move_back(), Actor.move_right(), Actor.move_left()

Move the actor relative to the direction it is facing. By default, the Actor is facing right. Example…

alien = Actor('alien')
alien.angle = 45
alien.move_forward(50)  # Moves towards the North-East

Actor.get_rect()

Get the rectangle for the Actor image. Example

alien = Actor('alien')
screen.draw.rect(alien.get_rect(), (255,0,0))

Actor.images, Actor.next_image()

Set a list of images. Switch the image to the next one in the list.

alien = Actor('alien_run1')
alien.images = ['alien_run1','alien_run2','alien_run3']

def update():
  alien.next_image()

def draw():
  alien.draw()

Actor.fps, Actor.animate()

Works like Actor.next_image(), but only switch image based on the specified frames per second (default is 5). Must set Actor.images first.

alien = Actor('alien_run1')
alien.images = ['alien_run1','alien_run2','alien_run3']
alien.fps = 10

def update():
  alien.animate()

def draw():
  alien.draw()

Actor.collidepoint_pixel(), Actor.collide_pixel(), Actor.collidelist_pixel(), Actor collidelistall_pixel()

Pixel perfect version of the Rect collide. Like Scratch, this ignores transparent areas when detecting collision. WARNING: This can cause poor performance and “stuck” actors. It’s usually preferable to use the normal Rect collide instead.

alien = Actor('alien')

def on_mouse_down(pos):
  if alien.collidepoint_pixel(pos):
    print("Eek!")

set_fullscreen(), set_windowed(), toggle_fullscreen(), hide_mouse(), show_mouse()

Does what their names suggests…

hide_mouse()  # Hides the mouse cursor

def update():
  if keyboard.f:
    toggle_fullscreen()  # Switch between fullscreen and windowed