Valus Sulav
Valus Sulav

Reputation: 145

Zelle Graphics animation

I am making a simple program of roulette in python using zelle graphics. I have the code below and I'm attempting to move the ball in between those two circles(track). Is there any way to do that. My thought is to undraw(ball.undraw()) and draw it again in in a .10 second time interval(time.sleep(.10)) but I don't know how to do it mathematically so it moves through the track.

from graphics import * def main(): win = GraphWin()

Circle1 = Circle(Point(100,100),95) #Makes the first Circle
Circle1.draw(win)

Circle2 = Circle(Point(100,100),80) #Makes the second Circle
Circle2.draw(win) 

Ball = Circle(Point(100,12),5) #Makes the ball Circle
Ball.draw(win) 

win.getMouse()
win.close()

main()

Upvotes: 0

Views: 1064

Answers (1)

Alecg_O
Alecg_O

Reputation: 1039

The method you are looking for is move(dx,dy).

To calculate the distance in each direction the ball needs to move, you must find the new position of the ball. The x and y coordinates of these positions will correlate with the sine and cosine (called from the math library) of the angle of the line connecting the ball to the center of the wheel.

x = radius*sin(angle)
y = radius*cos(angle)

Note: It will make it easier to calculate if you set your coordinates to (-100,-100,100,100) so that the centers of the circles are at 0,0.

Now that you have the new position of the ball, the distance it moves in each direction will be the final position - the initial position.

Ball.move(x - Ball.getCenter().getX(), y - Ball.getCenter().getY())

Throw these in a loop and steadily accumulate your angle, and your ball will move steadily along the radius.

angle = 0
radius = 87
while angle < 360:
    x = radius*sin(angle)
    y = radius*cos(angle)
    Ball.move(x - Ball.getCenter().getX(), y - Ball.getCenter().getY())
    angle += 0.01

Note: angle = 360 does not mean one revolution since the default angular unit in the math library is radians. If you need to know, 1 revolution = 2*pi, or about 6.28.

Slow it down with a time.sleep() in the loop to make it go at any speed you like.

Upvotes: 2

Related Questions