Hello everyone, I need some help with instances and loops (I am a very new programmer). Basically, I have a problem where I need to make a program that has a circle move around a window every time the user clicks. This circle is suppose to have a text box inside it that states the number of times the circle has moved. When the circle gets to the end of the window, I am suppose to change the movement coordinates so it comes back. I am using graphics that an author from the book I am using wrote.
Anyways, I have written code that moves the circle when the user clicks:
from zellegraphics import * ##Write a program to animate a circle bouncing around a window. The basic idea ##is to start the circle somewhere in the interior of the window. Use variables ##dx and dy (both initalized to 10) to control the movement of the circle. Use a ##large counted loop (say 10000 iterations), and each time through the loop move ##the circle using dx and dy. When the x-value of the center of the circle gets ##too high (it hits the edge), change dx to -10. When it gets too low, change dx ##back to 10. Use a similar approach for dy. def main(): #Set up a window 400 by 400 win = GraphWin("Circle Moving", 400, 400) #Set up circle that will travel around the window c = Circle(Point (200,200), 30) c.draw(win) x = 1 l = Text(Point (200,200), x) l.draw(win) for i in range(100): win.getMouse() dx = 10 dy = -10 c.move(dx,dy) center = c.getCenter() l.move(dx,dy) main()
I set the window to be 400 by 400. Variable c is a circle with the center point of (200,200) and a radius of 30. I made a text box which is drawn at the same place (200,200) and has a message of variable x. Variable x = 1. In the for loop I have made it so the circle will move (10,-10) every time the user clicks. Now, when I use the c.getCenter() I get back the center in the format of "Point(x,y)". The type of this is an instance.
How can I isolate point x so I can make an if statement that changes dx to -10 if x gets too high. I do not know how to work with instances. Also, I need variable x to change everytime I click. However, putting x = x+1 in the for loop won't change that. I assume this is because x in variable is outside the for loop. Can anyone help me?