Google Answers Logo
View Question
Q: Python - Program ( No Answer,   1 Comment )
Subject: Python - Program
Category: Computers > Algorithms
Asked by: macca111-ga
List Price: $25.00
Posted: 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
There is no answer at this time.

Subject: 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

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[0] = -speed[0]
    if < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.blit(ball, ballrect)

Make sure you have an image (named ball.gif in this example) in the
same directory as the script.

Hope this helps,

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy