Google Answers Logo
View Question
 
Q: Python - Program ( No Answer,   1 Comment )
Question  
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
vice-versa.
Answer  
There is no answer at this time.

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

    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

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 answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


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