#(c) Rory Clune 02/11/2011 import sys, pygame pygame.init() velocity = [1,1] position = [100,100] theta = 0 #rate of spin radius = 50 cr, fr = 0.9, 0.9 g = 0.002 deltaT = 10 #ms white = (253,253,253) # screen width and height size = width, height = 600, 400 # create a screen screen = pygame.display.set_mode(size) # give the screen a title pygame.display.set_caption('A Bouncing Ball using PyGame') image = pygame.image.load('ball.jpg').convert() image = pygame.transform.scale(image, (2*radius,2*radius)) rect = image.get_rect() clock = pygame.time.Clock() while 1: pygame.time.wait(deltaT)#pause for this long (each loop will, of course, take longer) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() velocity[1] += g*deltaT position[0] += velocity[0]*deltaT position[1] += velocity[1]*deltaT + 0.5*g*deltaT*deltaT rect.center = (round(position[0]), round(position[1])) if position[0]-radius < 0: velocity[0] = -velocity[0]*cr velocity[1] *= fr position[0] = radius elif position[0]+radius > width: velocity[0] = -velocity[0]*cr velocity[1] *= fr position[0] = width - radius if position[1]-radius < 0: velocity[1] = -velocity[1]*cr velocity[0] *= fr position[1] = radius elif position[1]+radius > height: velocity[1] = -velocity[1]*cr velocity[0] *= fr position[1] = height - radius screen.fill(white) screen.blit(image, rect) pygame.display.update()