Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pygame.snake

Snake supports scenes through the @game.scene decorator. This README doesn't cover them though. Despite that, snake's scenes are awesome, look out for the documentation for them when I make it!


Snake has been designed with the absolute beginner in mind. None of that fussing around with pygame.event.get(), pygame.display.set_mode(), pygame.image.load(), etc.

In fact, you can make a game in snake without import pygame anywhere! A lot of the following code snippets are actually entire snake projects. It's that simple to use.

import snake

game = snake.Game()

while True:
    game.next_frame()
    game.assets.player.stamp((0, 0))

game.assets? That's the assets/ folder automatically loaded with file types detected and filtered into the right things for you.

game.assets.player could have been assets/player.png. It might also have been assets/player.jpg or any other image format. Snake doesn't care.

That's boring. Let's get some user input going on:

import snake

game = snake.Game()

while True:
    events = game.next_frame()

    if events.keys.space:
        print("Space is pressed!")
    else:
        print("Space isn't pressed")

That was easy. Notice how the keyboard was dropped into events.keys? Let's have a look at mouse input:

print("Mouse is at " + str(events.mouse.pos))

Nice. I wonder if we can scroll?

if events.scroll.up:
    print("Scroll up")

Oh. Nice. My game's a little more advanced though. I'm going to need some sprites. No worries.

player = game.sprite(game.assets.player)

while True:
    game.next_frame()
    player.x += 1

Too easy. Onto fonts:

label = game.sprite(game.assets.my_font)

while True:
    events = game.next_frame()

    if events.keys.space:
        label.text = "Space is pressed"
    else:
        label.text = "Space is not pressed"

Remember that game.assets.my_font is going to be assets/my_font.ttf, so make sure you have a font file there.

Quitting isn't even something that needs to be covered. It's handled implicitly for you. No problems there.

Now at the moment everything has been working with the origin, but that's boring. Let's add an FPS counter in the top right corner.

counter = game.sprite(game.assets.my_font)
counter.stick = game.NE

while True:
    game.next_frame()

    counter.text = str(round(game.fps, 2))

We've got a few new things here. The important part is counter.stick which tells snake where to place the sprite. North-east is the top right which is what we want. The default is game.CENTRE. The other new thing is game.fps. No surprises there; it's the FPS.

I don't like the fact the text is mushed right against the side of the window though. Let's fix that:

counter = game.sprite(game.assets.my_font, x=32, y=32)

Now we're offsetting it by 32 pixels in both directions. Easy as anything.

Release files for pygame.snake 0.0.1a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygame.snake 0.0.1a0
File Size Uploaded
pygame.snake-0.0.1a0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygame.snake 0.0.1a0
File Interpreter ABI Platform
pygame.snake-0.0.1a0-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / pygame.snake-0.0.1a0.tar.gz

Download URL pygame.snake-0.0.1a0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d588b33a5ada783c8483f57f30ab16fc1486c71e3e0ad4f6cf88e8c7e848d0ea
BLAKE2b-256 checksum
How to use checksums
27c34b48fa89f614b1a3f736a3835d1c5ecfc0c20e79285b394bdbf6e2281660
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Release files / pygame.snake-0.0.1a0-py3-none-any.whl

Download URL pygame.snake-0.0.1a0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85c22e36d20a30497e4ee5e75aca1402fa854469614dfaed4095a914701b0633
BLAKE2b-256 checksum
How to use checksums
9d66193465970f30130c80749e7f8f9f30156f9062a638bbd095f0931df9017e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.0.1a0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page