How to get ball to stay on screen

I want to be able to get the red ball to stay within the range of the graph window is 600 by 700. Here’s the code I have so far:

‘‘‘

import time

import random
… from graphics import *
… def main():
… dx= int(1)
… dy= int(1)
… win= GraphWin(“shapes”, 600,700)
… center= Point(dx,dy)
… circ= Circle(center, 30)
… circ.setFill(‘red’)
… circ.draw(win)
… for i in range(100):
… dx1=random.randint(1,40)
… dy2=random.randint(1,40)
… circ.move(dx1,dy2)

… time.sleep(3)

‘‘‘

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

If the ball’s position would be outside a range, change the position to be inside the range. All you need to do that are if statements.

2 Likes

your current code moves ball randomly without checking its position, you need to fix it to the ball bouncing inside the window.

Help us help you. Read this link first and learn how to format code so we can help you better. Formatting code Do not use a screen shot of your code, instead paste your code in as preformatted code, as some of us will copy and paste the code to get it to work for you, so you can learn from this. You will get more help this way.