www.main.lv
Don't think just code it

2009-09-15 Python Pygame Tutorial Box Move

added constants that helps controlling screen size
SCREEN_X = 500
SCREEN_Y = 500
BOX_SIZE = 20
BOX_SPEED = 1
box have speed by axis
self.dx = BOX_SPEED
self.dy = BOX_SPEE
detecting if given rect is inside screen borders or not if not then change it direction
def move( self ):
        if self.rect.left+BOX_SIZE > SCREEN_X:
            self.dx = -BOX_SPEED
        if self.rect.left < 0:
            self.dx = BOX_SPEED
        if self.rect.top+BOX_SIZE > SCREEN_Y:
            self.dy = -BOX_SPEED
        if self.rect.top < 0:
            self.dy = BOX_SPEED
        self.rect.left += self.dx
        self.rect.top += self.dy
after few line of code where added box move inside given screen and coalide with screen borders
Tutorial Source