
# beachBounce.py
# Bounce a beach ball around a window

import pygame
from pygame.locals import *

BLACK = (   0,   0,   0)
WHITE = ( 255, 255, 255)


# ---------- main -------------

pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill(WHITE)
pygame.display.set_caption("Bouncing Beachball")


ballIm = pygame.image.load('ball.png').convert_alpha()

# store dimensions for later
scrWidth, scrHeight = screen.get_size()
imWidth, imHeight = ballIm.get_size()


# start position of the ball
x = 50; y = 50

# step size and direction along each axis
xStep = 10; yStep = 10

clock = pygame.time.Clock()

running = True    
while running:
    clock.tick(30)

    # handle events
    for event in pygame.event.get():
        if event.type == QUIT: 
            running = False
    

    # update game state
    # change x-step direction at left and right sides
    if (x <= 0) or (x >= scrWidth - 1 - imWidth):      
        xStep = -xStep

    # change y-step direction at top and bottom sides
    if (y <= 0) or (y >= scrHeight -1 - imHeight):        
        yStep = -yStep     
                                   
    x += xStep   # move the ball horizontally
    y += yStep   # and vertically


    # redraw
    screen.fill(WHITE)                       
    screen.blit(ballIm, [x, y])
    pygame.display.update()

pygame.quit()
