Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dessaya/python-gamelib
Pure-Python single-file library for writing simple games
https://github.com/dessaya/python-gamelib
education games python
Last synced: 2 months ago
JSON representation
Pure-Python single-file library for writing simple games
- Host: GitHub
- URL: https://github.com/dessaya/python-gamelib
- Owner: dessaya
- License: mit
- Created: 2020-01-29T22:09:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-19T02:36:43.000Z (over 3 years ago)
- Last Synced: 2024-11-02T14:42:42.884Z (2 months ago)
- Topics: education, games, python
- Language: Python
- Homepage:
- Size: 121 KB
- Stars: 17
- Watchers: 1
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gamelib
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:
```python
import gamelibdef main():
gamelib.resize(300, 300)gamelib.draw_begin()
gamelib.draw_text('Hello world!', 150, 150)
gamelib.draw_end()# wait until the user presses any key
gamelib.wait(gamelib.EventType.KeyPress)gamelib.init(main)
```And this example shows a rectangle moving around the screen:
```python
import gamelibdef main():
gamelib.resize(300, 300)x, y = 150, 80
dx, dy = 5, 5while gamelib.loop(fps=30):
for event in gamelib.get_events():
if event.type == gamelib.EventType.KeyPress and event.key == 'q':
returngamelib.draw_begin()
gamelib.draw_rectangle(x-10, y-10, x+10, y+10, fill='red')
gamelib.draw_end()x += dx
y += dy
if x > 300 or x < 0:
dx *= -1
if y > 300 or y < 0:
dy *= -1gamelib.init(main)
```## Goals
* **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](https://xkcd.com/1987/). `gamelib.py` should
not depend on anything that's not available in a fresh Python installation.
That rules out `pip`.## Installation
Just [download](https://raw.githubusercontent.com/dessaya/python-gamelib/master/gamelib.py)
`gamelib.py` and place it alongside your project :)## Documentation
First, look into the provided examples!
* [Hello world](https://github.com/dessaya/python-gamelib/blob/master/example-01-hello-world.py)
* [Bouncing square](https://github.com/dessaya/python-gamelib/blob/master/example-02-bounce.py)
* [Game of Life](https://github.com/dessaya/python-gamelib/blob/master/example-03-life.py)
* [Pong](https://github.com/dessaya/python-gamelib/blob/master/example-04-pong.py)Gamelib library reference: https://dessaya.github.io/python-gamelib/
To generate the HTML documentation:
```
$ pip3 install pdoc3
$ bash docs/generate.sh
```## Run the examples
```
$ python3 example-01-hello-world.py
```## Limitations
* Very limited drawing API (based on [Tkinter Canvas](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/canvas.html)).
* 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](https://github.com/TaylorSMarks/playsound)).
* The only sound format supported accross all platforms is probably WAV.
* Very limited GUI API (just two functions: `say()` and `input()`).
* Supports only a single window.
* No joystick support.