Skip to content

A collection of smaller projects I made using pygame, numpy, and/or OpenGL

Notifications You must be signed in to change notification settings

kadir014/pygame-utils

 
 

Repository files navigation

A collection of pygame experiments and utilities.

All of these are free to use & modify, with or without attribution. Every top-level python file is a standalone project. Most of these require additional libraires. Use this to install every dependency, or just install the ones you need based on the imports at the top of the file you want to use.

pip install -r requirements.txt

life (life.py)

An efficient game of life simulation using pygame and numpy.

life_demo.gif

After installing dependencies, run with python life.py

colorswirl (colorswirl.py)

A colorful celluar automata effect using pygame and numpy.

colorswirl.gif

After installing dependencies, run with python colorswirl.py

fractal (fractal.py)

An implementation of the mandlebrot set, which lets you zoom in and out.

fractal_zoom.gif

After installing dependencies, run with python fractal.py

rainbowize (rainbowize.py)

A function that applies a "rainbow effect" to a single surface.

rainbowize.gif

After installing dependencies, run with python rainbowize.py to see a demo.

Or import it into your own project and call rainbowize.apply_rainbow(my_surface).

This program also demonstrates how to set up a pygame.SCALED display with a custom initial scaling factor and outer fill color (see function make_fancy_scaled_display).

warp (warp.py)

A function that stretches a surface into an arbitrary quad using cv2's perspective warp.

warp.gif

After installing dependencies (numpy, cv2), run with python warp.py to see a demo.

lut (lut.py)

A function that transforms the colors of a surface using a lookup table (aka a "LUT").

lut.gif

After installing dependencies, run with python lut.py to see a demo.

Or import it into your own project and call lut.apply_lut(source_surface, lut_surface, idx).

If numpy isn't available, the function will fall back to a pure pygame routine (which is slower but produces the same result). The function also has an optional built-in caching system, and handles per-pixel alpha in a reasonable way.

video (video.py)

A video playback utility for pygame using numpy and cv2.

video_demo.gif

See documentation in class for usage instructions.

steganography (steganography.py)

A module that writes text data into the pixel values of images.

stega_demo.png

After installing dependencies, import the module into your project and call its methods. See docstrings for detailed usage instructions and information about advanced settings (e.g. bit depth, image resizing).

Example of writing a message into a surface.

output_surface = steganography.write_text_to_surface("secret message", my_surface)
message = steganography.read_text_from_surface(output_surface)
print(message)  # prints "secret message"

Example of saving a message to a PNG.

steganography.save_text_as_image_file("secret message", my_surface, "path/to/file.png")
message = steganography.load_text_from_image_file("path/to/file.png")
print(message)  # prints "secret message"

gifreader (gifreader.py)

A utility function that loads a gif file's images and metadata.

Metadata includes things like frame durations and image dimensions. See documentation on the function for usage instructions.

Requires imageio (pip install imageio) and numpy

Example of loading and displaying a GIF

gifreader_demo.gif

benchmark (benchmark.py)

A program that benchmarks pygame's rendering and displays the results in a graph.

Requires matplotlib (pip install matplotlib).

Instructions

Use python benchmark.py to run the tests with default settings, or python benchmark.py --help to see all settings.

If you close the window while the test is running, the results from cases that have already completed will still be shown.

Sample Results

benchmark_results.png

A plot that shows the relationship between FPS and the number of entities being rendered. Each line is a separate test case.

ALL           = Entities of all types rendered together
SURF_RGB      = Surfaces with no translucency
SURF_RGBA     = Surfaces with per-pixel translucency
SURF_RGB_WITH_ALPHA = Surfaces with full-surface translucency
RECT_FILLED   = pygame.draw.rect with width = 0 (i.e. filled)
CIRCLE_FILLED = pygame.draw.circle with width = 0 (i.e. filled)
LINE          = pygame.draw.line
RECT_HOLLOW   = pygame.draw.rect with width > 0
CIRCLE_HOLLOW = pygame.draw.circle with width > 0

Note that non-rectangular entities are scaled up and/or assigned widths so that drawing them will roughly change the same number of pixels as blitting a surface. This seemed like the most sensible way to compare rendering speeds.

double-pendulum (pendulum.py)

An efficient double pendulum simulation using pygame, numpy, and OpenGL.

Demo (N=1000)

n=1000animated.gif

Instructions

After installing dependencies, use this to run the default program:

python pendulum.py

To see the program's optional arguments, use:

python pendulum.py -h

A command like this can be used to make a "realistic-looking" 3-pendulum simulation:

python pendulum.py -n 3 --opacity 1.0 --size 400 400 --length 5 --zoom 20 --spread 1.3

While the simulation is running, the following actions can be used via keybindings:

  • [r] to restart the simlution with a new starting angle
  • [Esc] to quit the program
  • [p] to enable / disable profiling

N=1000

n=1000.PNG

N=10,000

n=10000.PNG

N=100,000

n=100000.PNG

About

A collection of smaller projects I made using pygame, numpy, and/or OpenGL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%