PygameBg - Pygame Toolbox for Beginners by Petlja
PygameBg is a small Python package aimed to reduce boilerplate code in simple Pygame programs, primarily initialization code and main loop.
PygameBg 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.
How to install PygameBg
Use pip to install PygameBg:
pip3 install pygamebg
If you use Windows and previous command does not work, try:
py -3 -m pip install pygamebg
Metadata
Release files for PygameBg 0.9.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| PygameBg-0.9.3.tar.gz | 4.0 kB | Without provenance | Details for PygameBg-0.9.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| PygameBg-0.9.3-py3-none-any.whl | Python 3 | none | any | Details for PygameBg-0.9.3-py3-none-any.whl |
Total release size: 8.5 kB
Release files / PygameBg-0.9.3.tar.gz
| Download URL | PygameBg-0.9.3.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
840d377c9c4f22b67050af4a0b0e822a12a1cee7ba15997c2be8f4964486f86f
|
|
BLAKE2b-256 checksum How to use checksums |
7bf4b69b351154716decc4b131a0bc5bca339fd5fed70e3f944d9fc9a4758deb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / PygameBg-0.9.3-py3-none-any.whl
| Download URL | PygameBg-0.9.3-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce9005f38c6c62f2557a4450ed1a438ffb06c2b5df878bc00f642d7495f751f2
|
|
BLAKE2b-256 checksum How to use checksums |
79b5e8f65113858c097f197ae7c3c58b62fe89e1afd948aa28831680e6366646
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for PygameBg 0.9.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.