Want I want to be able to do is to animate a red ball bouncing around on a screen via a for loop. I have variables dx and dy I want to initialize to 1 at the outset but then values will be updated. However when I run the code nothing happens. Also I attached a screenshot of my code.
Copy and paste any code or traceback, and in order to preserve formatting, select the code or traceback that you posted and then click the </> button.
All the for loop does is increment dx and dy. The lines that undraw and move the circle are outside the loop. Itâs just drawing the circle, changing dx and dy repeatedly, and then undrawing and moving the circle.
Hello than you for your response. I have updated the code since then but its not doing what I want. Every time I run it the ball continues to move out of frame. I have adjusted the graphic window size as well as the random range numbers to be more in sync. Heres what I have so far:
import time
import random
from graphics import *
def main():
window_width= 150
window_length=150
dx= int(1)
dy= int(1)
win= GraphWin(âshapesâ, window_width,window_length)
center= Point(dx,dy)
circ= Circle(center, 30)
circ.setFill(âredâ)
circ.draw(win)
for i in range(100):
dx1=random.randint(1,155)
dy2=random.randint(1,155)
dx+=dx1
dy+=dy2
if dx > window_width & dy > window_length:
dx = int(1)
dy= int(1)
circ.move(dx1,dy2)