Skip to main content

No project description provided

Project description

Pygskin

A collection of useful functions and classes for Pygame

animate function

Animation generator function which takes a list of frames or mapping of quotients (0.0 - 1.0) to keyframes and a function that returns a quotient, and returns an generator that returns frames.

anim = animate([image1, image2], timer.quotient)
screen.blit(next(anim))

Assets class

Provides attribute access to asset files and batch loading

assets = Assets()
screen.blit(assets.player)
assets.player_spawn_sfx.play()

Camera class

A camera class for scrolling and zooming a surface.

camera = Camera(screen.get_rect(), clamp=world_map.get_rect())
camera.view.fill("black")
sprite_group.draw(camera)
camera.zoom = 2.4
camera.draw(screen)

Timer class

A countdown timer dataclass. Can be used with the animate function.

timer = Timer(3000)  # 3 seconds
timer.tick()
if timer.finished:
    timer.elapsed = 0  # loop

iter_dialogue function

Generator function for stepping through a dialogue script parsed from a JSON or YAML file.

context = {}
dialogue = iter_dialogue(
    assets.act1_scene1,
    context,
    speak=speak,
)

def main_loop(screen, events, quit):
    if action := next(dialogue, None):
        action()

Direction enum

Enum for up/down/left/right directions

direction = Direction.UP
if direction in Direction.VERTICAL:
    rect.move_ip(direction.vector)

easing module

A selection of easing functions for use with interpolation. Can be used with the animate and make_color_gradient functions.

get_ecs_update_fn function

An extremely simple ECS implementation.

@filter_entities(has_velocity)
def apply_velocity(entity):
    entity.pos += entity.velocity

ecs_update = get_ecs_update_fn([apply_velocity])

@dataclass
class Entity:
    pos: Vector2
    velocity: Vector2

ecs_update([Entity(pos=Vector2(0, 0), velocity=Vector2(1, 1)])

bind function

Partial function application with one or more argument placeholders at arbitrary positions.

foos = filter(bind(isinstance, ..., Foo), items)

run_game function

Pygbag compatible game loop.

def main_loop(screen, events, exit):
    screen.fill(random.choice(pygame.color.THECOLORS.values()))

    for event in events:
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            exit()


if __name__ == '__main__':
    run_game(Window("My Game", (WIDTH, HEIGHT)), main_loop)

make_color_gradient function

Generate a color gradient between two colors.

sky_image = make_color_gradient(screen.size, "white", "blue")
screen.blit(sky_image)

imgui module

Immediate mode GUI.

gui = imgui.IMGUI()

def main_loop(screen, events, exit):
    with imgui.render(gui, screen) as render:
        if render(imgui.button("Quit"), center=(100, 200)):
            exit()

map_inputs_to_actions function

Map input events to actions. Enables user-defined key bindings.

keyboard_controls = {
    "jump": Event(pg.KEYDOWN, key=pg.K_UP),
    "duck": Event(pg.KEYDOWN, key=pg.K_DOWN),
    "quit": Event(pg.KEYDOWN, key=pg.K_ESCAPE),
}
for action in map_inputs_to_actions(
    keyboard_controls,
    pygame.event.get(),
):
    if action == "jump":
        player.jump()
    if action == "duck":
        player.duck()
    if action == "quit":
        exit()

lazy class

Lazy loading object proxy. Works like a partial function application for objects.

image = lazy(pygame.image.load, "foo.png"))
screen.blit(image)

scroll_parallax_layers function

Scroll parallax layers at different rates.

background = LayeredUpdates()
background.add(assets.sky, layer=0)
background.add(assets.mountains, layer=1)
background.add(assets.trees, layer=2)

scroll_parallax_layers(
    (vx, vy),
    background.layers,
    background.get_sprites_from_layer,
    {0: 0, 1: 1.5, 2: 2.0},
)

channel function

Simple pubsub implementation.

foo = channel()
foo.subscribe(lambda x: print(f"subscriber 1: {x}"))
foo("bar")

get_rect_attrs function

Filter rect attributes (eg top, center, size) from a dictionary. Useful for passing kwargs to pygame.Rect.move_to or pygame.Surface.get_rect.

def foo(image: Surface, **kwargs):
    image_rect = image.get_rect(**get_rect_attrs(kwargs))

add_padding function

Add padding of varying amounts to a Rect.

top, right, bottom, left = [100, 50, 10, 5]
padded, rect = add_padding(Rect(0, 0, 10, 10), [top, right, bottom, left])
assert padded.size == (120, 65)
assert rect.topleft == (5, 100)

grid function

Divide a Rect into a specified number of rows and columns, and return a function to access the cells by row/column index, or string aliases.

get_cell = grid(
    Rect(0, 0, 100, 100),
    rows=2,
    cols=2,
    names={"nw": (0, 0), "ne": (1, 0), "sw": (0, 1), "se": (1, 1)},
)
assert get_cell("ne") == get_cell(1, 0) == Rect(50, 0, 50, 50)

screen_manager function

Screen manager state machine.

def main():
    return screen_manager(
        show_main_menu,
        play_level,
    )

def show_main_menu(surface, events, exit_screen):
    surface.blit(assets.main_menu)
    for event in events:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                exit_screen()
            if event.key == pygame.K_RETURN:
                exit_screen(to=play_level)

def play_level(surface, events, exit_screen):
    ...

shake function

Shake animation generator.

rect = Rect(100, 100, 100, 100)
timer = Timer(3000)
shake_fn = shake()

def main_loop(screen, events, exit):
    screen.fill("black")
    timer.tick()
    pygame.draw.rect(screen, "red", rect.move(shake_fn(timer.quotient())))

spritesheet function

Provides grid cell access to a spritesheet image.

get_sprite = spritesheet(pygame.image.load("foo.png"), rows=3, cols=4)
screen.blit(get_sprite(2, 1))
walk_frames = [get_sprite(0, i) for i in range(4)]
walk_anim = animate(walk_frames, timer.quotient)

draw_sprite_stack function

Draw a pseudo-3d sprite using sprite-stacking.

draw_sprite_stack(
    screen,
    spritesheet(assets.player, rows=1, cols=8),
    (100, 100),
    spacing=3,
)

statemachine function

State machine as generator.

get_styles function

Simple cascading style sheet engine. Filters styles by object type, class and id attributes. Can be used with imgui module.

# styles.yaml
"*":
  color: black
  background_color: grey

"#error":
  background_color: red
# cascading styles
stylesheet = partial(get_styles, assets.styles)
Foo = namedtuple("Foo", ["id"])
styles = stylesheet(Foo(id="error"))
assert styles["background_color"] == "red"

# use with imgui
with imgui.render(gui, surface, stylesheet) as render:
    render(imgui.button("Click me!"), center=(100, 100))

make_sprite function

Create a sprite from an image.

player = make_sprite(assets.player, center=player_pos)

rotate_surface function

Rotate a surface in place or around a specified point.

rotated_image = rotate_surface(image, angle, center=(0, 0))

to_snakecase function

Convert CapWords to snake_case.

speech_duration function

Calculate rough speech duration in seconds.

tile function

Generate a blit sequence to tile an image across a surface.

screen.blits(tile(screen.get_rect(), assets.grass))

angle_between function

Calculate the angle between two points.

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

pygskin-0.2.5.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygskin-0.2.5-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file pygskin-0.2.5.tar.gz.

File metadata

  • Download URL: pygskin-0.2.5.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pygskin-0.2.5.tar.gz
Algorithm Hash digest
SHA256 542e4c58b022fbbde33c9396cce15e04b2ed93344c4439fc0e11606d233707e4
MD5 9800a119e49afa1d9b11e871570bab1b
BLAKE2b-256 9825a74e108e9976294ccbc88faa9c39df8a705c1b70b6707be0aaea1b497470

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygskin-0.2.5.tar.gz:

Publisher: python-publish.yml on andyhd/pygskin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pygskin-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: pygskin-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pygskin-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f3d69f4e119626fa691c29529e772229157be343b1c075fa6a63a0f29209b169
MD5 f48cda0ae74c9b960818d49c1206a79a
BLAKE2b-256 12338fa1b6a80517dbe58af1fe10bba746208f602d46a3417723a83f38ccdc03

See more details on using hashes here.

Provenance

The following attestation bundles were made for pygskin-0.2.5-py3-none-any.whl:

Publisher: python-publish.yml on andyhd/pygskin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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