import pygame
from Richter.Sprite import *
from Richter.Surface import *
import Richter.Engine as Engine
from StringIO import StringIO
resolution = (640,480)
Engine.init(resolution)
pygame.display.set_caption("surfacetest")
surfaces = SurfaceSet(StringIO("""
tilesize 100 100
surface 2 4 3 2
surface 3 4 2 3
solid 2 2 5 3
surface 4 3 5 4
"""))
player = Sprite('img/star.png')
player.setGravity(CENTER, CENTER)
player.position = [resolution[0]/2,resolution[1]/2]
class gamestate:
direction = [0,0]
colliding = False
def input(e):
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_LEFT:
gamestate.direction[0] = -1
elif e.key == pygame.K_RIGHT:
gamestate.direction[0] = 1
elif e.key == pygame.K_DOWN:
gamestate.direction[1] = -1
elif e.key == pygame.K_UP:
gamestate.direction[1] = 1
elif e.type == pygame.KEYUP:
if e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT:
gamestate.direction[0] = 0
elif e.key == pygame.K_UP or e.key == pygame.K_DOWN:
gamestate.direction[1] = 0
Engine.eventhandlers.append(input)
def update():
player.position[0] += gamestate.direction[0]
player.position[1] += gamestate.direction[1]
if surfaces.collidePoint(player.position):
if not gamestate.colliding:
player.setColor(1.0,0.2,0.2)
gamestate.colliding = True
else:
if gamestate.colliding:
player.setColor(1.0,1.0,1.0)
gamestate.colliding = False
Engine.updaters.append(update)
def draw():
surfaces.draw()
player.draw()
Engine.drawers.append(draw)
Engine.run()