import pygame, os, random
from pygame.locals import *
def load_image(name, colorkey=None):
    fullname = os.path.join('images', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()
random.seed()    
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('rota')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0, 0))
pygame.display.flip()
g = pygame.sprite.RenderUpdates()
s = pygame.sprite.Sprite(g)
s.image, s.rect = load_image('caballito.gif', -1)
z = s.image
g.add(s)
pygame.display.update(g.draw(screen))
clock = pygame.time.Clock()
rotation = 0
xx = 1
yy = 1
while 1:
    clock.tick(100)
    center = s.rect.center
    rotation += 12
    print "Rotation:",rotation,
    if rotation >= 360:
        rotation = 0
        s.image = z
    else:
        s.image = pygame.transform.rotate(z , rotation)
    print "Dimesions:",s.rect.width,",",s.rect.height
    s.rect = s.image.get_rect()
    s.rect.center = center
    stepx = random.randint(0,3)
    stepy = random.randint(0,3)
    if s.rect.x >  475:
        xx = -1 - stepx
    elif s.rect.x < 0:
        xx = 1 + stepx
    if s.rect.y > 475:
        yy = -1 - stepy
    elif s.rect.y < 0:
        yy = 1 + stepy
    s.rect.x += xx
    s.rect.y += yy
    print xx
    print yy
    print s.rect.x

    g.update()
    pygame.display.update(g.draw(screen))
    g.clear(screen, background)
    




