Python - Program
Category: Computers > Algorithms
Asked by: macca111-ga
List Price: $25.00
06 Sep 2006 02:40 PDT
Expires: 20 Sep 2006 04:04 PDT
Question ID: 762626
How do you write an algorithm that animates a circle bouncing around a window. Using the change in x and y to control the movement. Using a loop and each time you go through the loop the x and y and move + or - 1. If the circle hits the edge then the sign changes to - from + or vice-versa.
|There is no answer at this time.|
Re: Python - Program
From: gryphon5-ga on 07 Sep 2006 07:58 PDT
Using the pygame library, this is fairly easy, here is an example. ------------------------ import sys, pygame pygame.init() size = width, height = 600, 400 speed = [20, 20] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("ball.gif") ballrect = ball.get_rect() while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed = -speed if ballrect.top < 0 or ballrect.bottom > height: speed = -speed screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() ------------------------ Make sure you have an image (named ball.gif in this example) in the same directory as the script. Hope this helps, Tom
If you feel that you have found inappropriate content, please let us know by emailing us at email@example.com with the question ID listed above. Thank you.
|Search Google Answers for|