#!/usr/bin/env python  
# -*- coding: utf-8 -*-

#==============================================
# gas.py - moviendo múltiples objetos
#----------------------------------------------
# FJA - neocipres@telefonica.net, Abril de 2007
#==============================================
 
import sys, random
import pygame
from pygame.locals import *

XMAX  = 800 
YMAX = 600
NEGRO = (0, 0, 0)

class Bola:
    def __init__(self, imagen, r0, vx0, vy0):
        self.imagen = imagen
        self.vx = vx0
        self.vy = vy0
        self.pos = imagen.get_rect().move( r0 )
    def move(self):
        self.pos = self.pos.move( self.vx, self.vy )
     
    def cambiaV(self, vx, vy):
        self.vx = vx
        self.vy = vy
       
        
def main():
    pygame.init()
    screen = pygame.display.set_mode( (XMAX, YMAX) )
    pygame.display.set_caption('un gas')
    imagen = pygame.image.load( "bola.gif" )
    bolas = []
    for n in range( 500 ): 	
        p = random.randrange(XMAX - 25), random.randrange(YMAX - 25)
        v = random.choice([-1, 1]), random.choice( [-1, 0, 1])
        bola = Bola(imagen, p, v[0], v[1] )
        bolas.append( bola )
   
   
    while 1:
        for event in pygame.event.get():
            if event.type in (QUIT, KEYDOWN):
                sys.exit()
         
        for bola in bolas:
            bola.move()
            if bola.pos.left < 0 or bola.pos.right > XMAX:
                bola.cambiaV( -bola.vx, bola.vy)
            if bola.pos.top < 0 or bola.pos.bottom > YMAX:
                bola.cambiaV( bola.vx, -bola.vy )
              
            screen.blit(bola.imagen, bola.pos)
            
        pygame.display.update()        # o bien, pygame.display.flip()
        screen.fill( NEGRO)
        pygame.time.delay(3)
        
if __name__ == '__main__':
    main()
