Skip to main content

The super-simple pygame framework.

Project description

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.

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

pygame.snake-0.0.1a0.tar.gz (9.0 kB view hashes)

Uploaded Source

Built Distribution

pygame.snake-0.0.1a0-py3-none-any.whl (10.2 kB view hashes)

Uploaded Python 3

Supported by

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