rendered paste body#/usr/bin/env python
import pygame, random
from pygame.locals import *
pygame.init( )
class Objects( pygame.sprite.Sprite ):
def __init__( self, color, position ):
pygame.sprite.Sprite.__init__( self )
self.image = pygame.Surface( [ 15, 5 ] ).convert( )
self.image.fill( color )
self.rect = self.image.get_rect( )
self.rect.topleft = position
class DrawBall( pygame.sprite.Sprite ):
# changed
def __init__ ( self, color, radius, velocity, screen_rects ):
pygame.sprite.Sprite.__init__( self )
self.image = pygame.Surface(( 2 * radius, 2 * radius )).convert( )
self.rect = self.image.get_rect( )
self.image.set_colorkey(( 0, 0, 0 ))
self.rect.center = ( 50, 50 )
# changed
self.velocity_x, self.velocity_y = velocity
pygame.draw.circle( self.image, color, ( radius, radius ), radius )
# changed
self.screen_rect = screen_rects
def UpdateBall( self ):
# changed whole method
if self.rect.right >= self.screen_rect.right or self.rect.left <= 0:
self.velocity_x = -self.velocity_x
if self.rect.bottom >= self.screen_rect.bottom:
self.velocity_y = -self.velocity_y
#if self.rect.right >= 200 -1 or self.rect.left <= 0: velocity_x = - velocity_x
self.rect.move_ip( self.velocity_x, self.velocity_y )
_screen_ = pygame.display.set_mode( [ 200, 200 ] )
_scrRect = _screen_.get_rect( )
velocityX, velocityY = random.randint( 1, 5 ), random.randint( 1, 5 )
objgroup =pygame.sprite.Group( )
#changed
_ball = DrawBall( [ 255, 0, 0 ], 5, (velocityX, velocityY), _scrRect)
for colors, pos in [ ( [ 255, 0, 0 ], [ 0, 5 ] ), ( [ 0, 255, 0 ], [ 15, 5 ] ), ( [ 0, 0, 255 ], [ 30, 5 ] ) ]:
objgroup.add( Objects( colors, pos ))
_loop_ = True
while pygame.event.poll( ).type != pygame.KEYDOWN:
pygame.time.delay( 30 )
_screen_.fill(( 0, 0, 0 ))
for b in objgroup.sprites( ): _screen_.blit( b.image, b.rect )
_ball.UpdateBall() # changed
_screen_.blit( _ball.image, _ball.rect )
pygame.display.update( )
pygame.quit()