The Monkey Project

Python Game Development News

Screenshot Code

with 2 comments

If you want people to look at your game, screenshots are what most pique people’s attention.

It’s sometimes possible to take screenshots using a generic screen grab tool, but that’s not always the case, and it’s not always quick and easy. It’s best to set up a specific key you can press to take screenshots quickly and easily (I invariably use F12).

Fortunately doing this from Python code is pretty formulaic.

Pygame

Pygame can directly save a surface as an image. The code to do this just needs to be dropped into your event handling.


import datetime
import pygame

def screenshot_path():
    return datetime.datetime.now().strftime('screenshot_%Y-%m-%d_%H:%M:%S.%f.png')

...

# in your event loop
if event.type == KEYDOWN:
    if event.key == K_F12:
        pygame.image.save(screen_surface, screenshot_path())


Pyglet

In OpenGL, you have to read back the colour buffer to an image and save that. As you generally don’t want the colour buffer’s alpha channel to be saved if it has one, there are a couple of OpenGL calls to force every pixel to be read as opaque. Pyglet can handle reading and saving the colour buffer though.


import datetime
from pyglet import gl
from pyglet.window import key

def screenshot_path():
    return datetime.datetime.now().strftime('screenshot_%Y-%m-%d_%H:%M:%S.%f.png')

...

def on_key_press(symbol, modifiers):
    """This is your registered on_key_press handler.

    window is the pyglet.window.Window object to grab.
    """
    if symbol == key.F12:
        gl.glPixelTransferf(gl.GL_ALPHA_BIAS, 1.0)  # don't transfer alpha channel
        image = pyglet.image.ColorBufferImage(0, 0, window.width, window.height)
        image.save(screenshot_path())
        gl.glPixelTransferf(gl.GL_ALPHA_BIAS, 0.0)  # restore alpha channel transfer
Advertisements

Written by mauve

May 4, 2011 at 4:00 pm

Posted in Techniques

One-Hour Games

leave a comment »

After Pyweek 12, a few contestants fuelled by the drug of Python game coding set about a new challenge, writing a game in one hour, solo.

superjoe and John opened the competition with hour-game and space_bombers:


hour-game, by superjoe

space_bombers, by John

Chard followed up with an addictive little avoid-the-laser game, futility

futility, by Chard

My attempt was a simple destroy-the-planet game.

hourgame, by mauve

camel, made by Cosmologicon in just 30 minutes, was created according to a randomly selected theme:

camel, by Cosmologicon

And that’s all I’ve seen, so far. If anyone would like to participate, simply set your timer for one hour, knock up a game from scratch, in Python, and pop a note with a link to a download in the comments.

Written by mauve

May 3, 2011 at 11:46 pm

Posted in Competitions

Pyggy Awards, July 2011

leave a comment »

Off the back of Pyweek 12, Greg Ewing has announced the 6th Pyggy awards, to be judged in July 2011.

This time the 2 month long contest is open to any open-source Python game, as Greg explains:

If you’ve had a Python game project on the back burner, you no longer have any excuse for not finishing it off! Get going!

Previously the Pyggy awards were only open to games building upon Pyweek entries or themes.

Written by mauve

May 3, 2011 at 10:26 pm

Posted in Competitions