Skip to main content

Pygame Toolbox for Beginners by Petlja

Project description

PygameBg is a small Python package aimed to reduce boilerplate code in simple Pygame programs, primarily initialization code and main loop.

PygamePg should make Pygame learning curve more gradual for beginner programmers, but without losing focus from the pure Pygame API.

When we compare Python with C-like programming languages, one of the positive features we usually mention is a single line “Hellow World!” example:

print('Hellow World!')

Pygame is not pythonic enough here. A proper “Draw circle” program looks like:

import pygame as pg

pg.init()
surface = pg.display.set_mode((400,400))
pg.display.set_caption("Blue circle")

pg.draw.circle(surface, pg.Color("blue"), (200,200), 100)

pg.display.update()
while pg.event.wait().type != pg.QUIT:
    pass
pg.quit()

The central line of code in this example is:

pg.draw.circle(surface, pg.Color("blue"), (200,200),100)

We could say that the first three lines (excluding import) opens a window, and the last four lines waits for user to quit, but we would not like to burden beginners with details of those boilerplate statements.

Here is an equivalent example that use PatljaBg:

import pygame as pg
import pygamebg

surface = pygamebg.open_window(400, 400, "Blue circle")

pg.draw.circle(surface, pg.Color("blue"), (200,200), 100)

pygamebg.wait_loop()

This is much more readable first example for beginners and easier to explain: We open window, then draw blue circle and then wait for user to quit.

Besides wait_loop, PygameBg supports frame_loop and event_loop.

Here is example that use frame_loop:

import pygame as pg
import pygamebg

surface = pygamebg.open_window(300, 300, "Read keyboard state")

x, y = 150, 150

def update():
    global x, y
    surface.fill(pg.Color("white"))
    pressed = pg.key.get_pressed()
    if pressed[pg.K_RIGHT]:
        x += 1
    if pressed[pg.K_LEFT]:
        x -= 1
    if pressed[pg.K_DOWN]:
        y += 1
    if pressed[pg.K_UP]:
        y -= 1
    pg.draw.circle(surface , pg.Color("red"), (x, y), 30)

pygamebg.frame_loop(30, update)

So, frame loop calls update function once per frame and may optionally call an event handler:

import pygame as pg
import pygamebg

width, height = 500, 300
surface = pygamebg.open_window(width, width, "Increasing and decreasing speed")
pg.key.set_repeat(10,10)

fps = 30
x, y = 150, 150
vx, vy = 0, 0

def update():
    global x,y
    x = (x + vx/fps) % width
    y = (y + vy/fps) % height

    surface.fill(pg.Color("white"))
    color = pg.Color("red")
    pg.draw.circle(surface, color, (int(x), int(y)), 30)

def handle_event(d):
    global vx, vy
    if d.type == pg.KEYDOWN:
        if d.key == pg.K_RIGHT:
            vx += 1
        elif d.key == pg.K_LEFT:
            vx -= 1
        elif d.key == pg.K_DOWN:
            vy += 1
        elif d.key == pg.K_UP:
            vy -= 1

pygamebg.frame_loop(fps, update, handle_event)

We can also use a dictionary argument to specify event handlers for specific event types:

def keydown(e):
    global vx, vy
    if e.key == pg.K_RIGHT:
        vx += 1
    elif e.key == pg.K_LEFT:
        vx -= 1
    elif e.key == pg.K_DOWN:
        vy += 1
    elif e.key == pg.K_UP:
        vy -= 1

pygamebg.frame_loop(fps, update, {pg.KEYDOWN: keydown})

Frame loop can handle events, but it is always frame driven: it updates on each frame and handles pending events before each update.

A pure event loop handles events immediately when they occurred and triggers repaint when needed (when an event handler returns True):

import pygame as pg
import pygamebg

surface = pygamebg.open_window(500, 500, "Keyboard and mouse events")
pg.key.set_repeat(10,10)

x, y = 150, 150

def handle_event(e):
    global x, y
    if e.type == pg.MOUSEBUTTONDOWN:
        x,y = e.pos
        return True
    if e.type == pg.KEYDOWN:
        if e.key == pg.K_RIGHT:
            x += 1
        elif e.key == pg.K_LEFT:
            x -= 1
        elif e.key == pg.K_DOWN:
            y += 1
        elif e.key == pg.K_UP:
            y -= 1
        else:
            return False
        return True
    return False

def paint():
    surface.fill(pg.Color("white"))
    pg.draw.circle(surface, pg.Color("blue"), (x, y), 50)

pygamebg.event_loop(paint, handle_event)

A dictionary argument can also be used to specify event handlers for specific event types:

import pygame as pg
import pygamebg

surface = pygamebg.open_window(500, 500, "Keyboard and mouse events")
pg.key.set_repeat(10,10)

x, y = 150, 150

def clicked(e):
    global x, y
    x,y = e.pos
    return True

def keypressed(e):
    global x,y
    if e.key == pg.K_RIGHT:
        x += 1
    elif e.key == pg.K_LEFT:
        x -= 1
    elif e.key == pg.K_DOWN:
        y += 1
    elif e.key == pg.K_UP:
        y -= 1
    else:
        return False
    return True

def paint():
    surface.fill(pg.Color("white"))
    pg.draw.circle(surface, pg.Color("blue"), (x, y), 50)

pygamebg.event_loop(paint, {pg.MOUSEBUTTONDOWN:clicked, pg.KEYDOWN:keypressed})

Source files of all examples are available here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PygameBg-0.9.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

PygameBg-0.9.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file PygameBg-0.9.1.tar.gz.

File metadata

  • Download URL: PygameBg-0.9.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for PygameBg-0.9.1.tar.gz
Algorithm Hash digest
SHA256 2536126e0dda2f2863f3cee3bec2a637a26417ffd36b14c1b90c724189a0f1a5
MD5 bda127f51c659caa374360d20a99298f
BLAKE2b-256 b84ae122551eee96247f22f03c38e1b403f46bf03171111134a2daa675d35231

See more details on using hashes here.

File details

Details for the file PygameBg-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: PygameBg-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

File hashes

Hashes for PygameBg-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd3e6357abadc1b3f690a30f9bee821af2ca3f0bbf58d21c92fcacb4a177b13c
MD5 0fc4d93f890b06fd76dcc08ec39afdf9
BLAKE2b-256 d319f5ff84561088e998baa325d59362739ffdef1d8a247317a1166d02f635d6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page