By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,428 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,428 IT Pros & Developers. It's quick & easy.

How to bounce a ball within a square?

P: 1
I am doing a project of creating a playing pool and 2 balls bouncing within the pool. I have tried to use "while loop" and "if statement" to set restriction limiting the ball not to bounce out of the pool. However, the ball doesn't move or bounce.

It moves but stops after it hits one of the walls when I write this function below:
Expand|Select|Wrap|Line Numbers
  1. def move_ball(heading):
  2.     t1=turtle.Pen()
  3.     t1.color("yellow")
  4.     t1.shape("circle")
  5.     x,y=turtle.position()
  6.     while 200 > (t1.xcor()) >-200 and -90< (t1.ycor())<90:
  7.         t1.forward(1)
  8.         t1.setheading(heading)  
But, it doesn't move at all when I write this function below:
Expand|Select|Wrap|Line Numbers
  1. # here I try to do all restrictions of 4 walls  
  2.  
  3. # just 2 walls here
  4.  
  5. def wall_bottom(heading):
  6.     t1=turtle.Pen()
  7.     t1.color("yellow")
  8.     t1.shape("circle")
  9.     current=0
  10.     if 100<t1.ycor()<-100:
  11.         if 270 < t1.setheading() < 360:
  12.             t1.forward(1)
  13.             t1.setheading(0+(360-(t1.heading())))
  14.         else:
  15.             t1.forward(1)
  16.             t1.setheading(180-(t1.heading()-180))
  17.  
  18. def wall_top(heading):
  19.     t1=turtle.Pen()
  20.     t1.color("yellow")
  21.     t1.shape("circle")
  22.     if 200> t1.xcor() >-200:
  23.         if 90> t1.setheading()<0:
  24.             t1.forward(1)
  25.             t1.setheading(0-(180-(t1.heading())))
  26.         else:
  27.             t1.forward(1)
  28.             t1.setheading(180-(t1.heading()-180))
I don't know if I am doing the right things. This is so confusing. Please Help me for this. I want the ball bounce. Thanks!
Feb 16 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.