Gamelib is a pure-Python single-file library/framework for writing simple games. It is intended for educational purposes (e.g. to be used in basic programming courses).
Here is a "hello world" example:
importgamelibdefmain():
gamelib.resize(300, 300)
gamelib.draw_begin()
gamelib.draw_text('Hello world!', 150, 150)
gamelib.draw_end()
# wait until the user presses any keygamelib.wait(gamelib.EventType.KeyPress)
gamelib.init(main)And this example shows a rectangle moving around the screen:
importgamelibdefmain():
gamelib.resize(300, 300)
x, y=150, 80dx, dy=5, 5whilegamelib.loop(fps=30):
foreventingamelib.get_events():
ifevent.type==gamelib.EventType.KeyPressandevent.key=='q':
returngamelib.draw_begin()
gamelib.draw_rectangle(x-10, y-10, x+10, y+10, fill='red')
gamelib.draw_end()
x+=dxy+=dyifx>300orx<0:
dx*=-1ify>300ory<0:
dy*=-1gamelib.init(main)- Easy to learn: Writing a simple game should be almost as easy as writing console programs. It should not require knowledge about inheritance, components, double-buffering, color channels, blitting or actors.
- Simple, basic API: Support drawing stuff and maybe playing sounds, nothing more.
- Portable Support Windows / Mac OS / Linux desktop.
- Easy to install: See relevant XKCD.
gamelib.pyshould not depend on anything that's not available in a fresh Python installation. That rules outpip.
Just downloadgamelib.py and place it alongside your project :)
First, look into the provided examples!
Gamelib library reference: https://dessaya.github.io/python-gamelib/
To generate the HTML documentation:
$ pip3 install pdoc3
$ bash docs/generate.sh
$ python3 example-01-hello-world.py
- Very limited drawing API (based on Tkinter Canvas).
- Don't expect to be able to draw thousands of elements at 60 FPS.
- The only image formats that are supported accross all platforms are GIF and PPM/PGM/PBM.
- Very limited sound API (just a single function:
play_sound(), based on playsound).- The only sound format supported accross all platforms is probably WAV.
- Very limited GUI API (just two functions:
say()andinput()). - Supports only a single window.
- No joystick support.