Example

The following code is a standalone script demonstrating SSR's current features. The code should be documented out well enough to be self-explanatory. The script can also be found in the source distribution, from which it can be run without modification.

# this is a simple test script for SSR
# do note that in a real game, you should probably handle game logic differently
#
# rather than using rendering objects, your game logic should modify
# your own type of objects and keep the rendering objects up to date from there

from renderer import MapPlane,Obj, Tile

import pygame, pygame.image

from pygame.locals import *

import random, time
from random import randint

# we sub-class the Obj class so we can create identical objects more quickly
class Grass(Obj):
    sprite = pygame.image.load("grass.png")
    layer = 0

class Dirt(Obj):
    sprite = pygame.image.load("dirt.png")
    layer = 0

class Pillar(Obj):
    sprite = pygame.image.load("pillar.png")
    layer = 1

class Player(Obj):
    sprite = pygame.image.load("player.png")
    layer = 2

# specify the size of our viewport
viewport_size_x = 640
viewport_size_y = 480

# create a new plane to render the map to
plane = MapPlane(640, 480, 100*32, 100*32)

# fill the map with some simple ground
for x in xrange(0,100+1):
    for y in xrange(0,100+1):
        if ((x + y) % 2) == 0: obj = Grass(plane,x*32,y*32)
        else: obj = Dirt(plane,x*32,y*32)

# position the player's view
plane.eye_x = 20 * 32
plane.eye_y = 20 * 32

# create an object representing the player
player = Player(plane, 20*32, 20*32)
# center the object (by default, the sprite will extend to the top right from the object's center)
player.center()

# create some objects for testing
pillars = []
for i in xrange(10):
    pillars.append(Pillar(plane,20*32+(i-5)*5,20*32))

# initialize pygame for rendering
pygame.init()
pygame.display.set_mode((plane.view_x,plane.view_y))

surf = pygame.display.get_surface()

# initialize the rendering loop
for i in xrange(10000):
    # avoid rendering too often(this could be done better)
    time.sleep(0.02)

    # move all the pillars randomly
    for pillar in pillars:
        pillar.move(randint(-1,1),randint(-1,1))

    # move the player's view and his avatar
    keys = pygame.key.get_pressed()
    move_x,move_y = 0,0

    # move him two pixels per rendering tick
    if(keys[K_UP]): move_y = 2
    if(keys[K_DOWN]): move_y = -2
    if(keys[K_LEFT]): move_x = -2
    if(keys[K_RIGHT]): move_x = 2

    # move his view
    plane.move_eye(move_x,move_y)
    player.move(move_x,move_y)

    # draw the frame and update pygame
    plane.render(surf, (0,0))
    pygame.event.pump()
    pygame.display.update()