Sunday, March 7, 2010

GSoC project idea: Insta-PyGame

I'm still trying to choose new GSoC organizations to join, but I know one of the organizations to which I'll apply: PyGame, the SDL-based Python multimedia library. I plan to write a micro-framework on top of PyGame to dramatically simplify the creation of conventional games.

The Problem

PyGame is a wrapper for the C SDL library. Some of the library hides the complexity of the underlying SDL library, but in most areas, PyGame is simply a Python binding for SDL. For that reason, you often need to write several lines of code to accomplish a simple task, such as checking whether the user has pressed a button. In addition, many pieces of the API are based on C coding idioms that are unfamiliar to most Python programmers.

The Plan

I propose the development of a new framework on to of PyGame that operates at a much higher level of abstraction. Through this new framework, people would create games by declaring the sprites the characters use, the arrangement and graphics of the levels, and the interactions between the player and the contents of the levels.

The framework would be divided into modules with each module representing a genre. The "platformer" module would contain everything necessary to create a platformer game, including a physics engine, a tile map engine, and enemies that die when you jump on them. In contrast, the "scrolling shooter" module would contain tools for controlling the behavior of projectiles and enemy ships.

Here's an example of the type of code games using the framework could look like. I haven't put much thought into the API, but I would like game code to be written at this level of abstraction.


from insta.menu import *

from insta.platformer import *



def startMenu():

    titleScreen = Screen(600, 400)
    titleScreen.setTheme(themes.MEDIEVAL)
    titleScreen.setTitle("Porcupine's Tiny Adventure")
    titleScreen.setOptions(["Play", "Controls", "Credits"])
    titleScreen.getOption("Play").setAction(startGame)
    # More code for other menu options

def startGame():

    game = Game()

    hero = Player()
    hero.setSprite("standing.gif")
    hero.setRunningSprites(["running1.gif", "running2.gif", "running3.gif"])
    hero.setJumpSprite("jumping.gif")
    hero.setDeathSprite("gravestone.gif")

    hero.setMovementTriggers(constants.ARROW_KEYS)
    hero.setJumpTrigger(constants.SPACE_BAR)

    goal = Item()
    goal.setSprite("bigring.gif")
    goal.setBehavior(constants.FLOATING)
    goal.setAction(game.nextLevel)

    itemGenerator = ItemGenerator([None, goal, hero])

    '''
    Tile generator translates level maps (text files full of numbers) into tile
    maps in a context-sensitive manner
    '''
    tileGenerator = TileGenerator()
    tileGenerator.setFloorSprite("levelground.gif")
    tileGenerator.setUndergroundSprite("underground.gif")
    tileGenerator.setPlatformSprite("platform.gif")
    # Edge and corner sprites could also be set

    mushroom = Enemy()
    mushroom.setRunningSprites(["step1.gif", "step2.gif"])
    mushroom.setDeathSprite("explosion.gif")
    # Some simple behaviors would be pre-defined
    mushroom.setBehavior(constants.WALKING)

    bird = Enemy()
    bird.setFlightSprites(["flap1.gif", "flap2.gif"])
    bird.setDeathSprite("feathers.gif")
    bird.setBehavior(constants.FLYING)

    # List associates enemy types with numbers in the text file
    enemyGenerator = EnemyGenerator([None, mushroom, bird])

    level = Level()
    level.setTileMap(tileGenerator.generateTileMap("levelmap1.txt"))
    level.setEnemyMap(enemyGenerator.generateEnemyMap("enemymap1.txt"))
    level.setItemMap(itemGenerator.generateItemMap("itemmap1.txt"))

    level.setBackground("background.gif")
    level.setBackgroundOptions([constants.TILED, constants.PARALLAX])

    game.setLevels([level])
    game.start()

if (__name__ == "__main__"):

    startMenu()

The Goal

I want to free PyGame developers from thinking about the hows of game development, allowing them to focus on the whats of their ideas. By freeing game creators from thinking about the implementation of their games, I hope to allow them to explore new ideas in video game design, such as dynamically-generated levels, media mashups, user-created content, and AI.

This is just a rough outline of the framework I'd like to build and the effects I hope to see. I plan to start work on a prototype soon if the PyGame community appears receptive to my idea.