V
V
Vitalianskiy Lavrov2022-02-21 13:45:21
Animation
Vitalianskiy Lavrov, 2022-02-21 13:45:21

How to stop 2d animation?

I need to stop these two circles at the moment of touch
. Tell me or point me in the right direction, maybe there are some books.
Here is the actual code:

import math
from tkinter import Canvas, Tk
 
SIZE = 600
WIDTH = SIZE
HEIGHT = SIZE
RADIUS4 = SIZE / 5
RADIUS1 = SIZE / 2.85
#radius orbiti
RADIUS2 = SIZE / 12
#radius pervogo kruga
RADIUS3 = SIZE / 15
#radius vtorogo kruga
 
def coords(angle):
    x = math.cos(angle) * RADIUS1
    y = math.sin(angle) * RADIUS1
    return x - RADIUS2 + SIZE / 2, y - RADIUS2 + SIZE / 2, x + RADIUS2 + SIZE / 2, y + RADIUS2 + SIZE / 2
 
def coords_1(angle):
    x = math.cos(angle) * RADIUS4
    y = math.sin(angle) * RADIUS4
    return x - RADIUS3 + SIZE / 2, y - RADIUS3 + SIZE / 2, x + RADIUS3 + SIZE / 2, y + RADIUS3 + SIZE / 2
 
def motion(angle):
    angle = angle + 0.1
    c.coords(f, coords(angle))
    root.after(50, lambda: motion(angle))
 
def motion_1(angle):
    angle = angle - 0.1
    c.coords(f1, coords_1(angle))
    root.after(50, lambda: motion_1(angle))
 
angle = 0
root = Tk()
c = Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
c.pack()
#c.create_oval(SIZE / 2 - RADIUS1, SIZE / 2 - RADIUS1, SIZE / 2 + RADIUS1, SIZE / 2 + RADIUS1)
# #орбита окружности
#c.create_oval(SIZE / 2 - RADIUS4, SIZE / 2 - RADIUS4, SIZE / 2 + RADIUS4, SIZE / 2 + RADIUS4)
# #орбита окружности 2
c.create_oval(SIZE / 2 - 160, SIZE / 2 - 160, SIZE / 2 + 160, SIZE / 2 + 160)
#орбита окружности 3
f = c.create_oval(coords(angle), fill='black')
f1 = c.create_oval(coords(angle), fill='green')
root.after(100, motion(angle))
root.after(100, motion_1(angle))
root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
QPDEH, 2022-02-22
@Vitalianskiy

Here you are. I had to farm a little, but it works

def motion(angle):
    x1 = (c.coords(f)[0] + c.coords(f)[2]) / 2#координата x центра первого круга
    y1 = (c.coords(f)[1] + c.coords(f)[3]) / 2#координата y центра первого круга
    x2 = (c.coords(f1)[0] + c.coords(f1)[2]) / 2#координата x центра второго круга
    y2 = (c.coords(f1)[1] + c.coords(f1)[3]) / 2#координата y центра второго круга
    if not math.floor(((x2-x1)**2+(y2-y1)**2)**0.5) == RADIUS2 + RADIUS3:#Вычисляем расстояние между центрами окружностей и если оно равно сумме радиусов окружностей то выходим. Пришлось сделать math.floor() т.к. иногда не сходятся
        angle = angle + 0.1
        c.coords(f, coords(angle))
        root.after(50, lambda: motion(angle))
 
def motion_1(angle):
    x1 = (c.coords(f)[0] + c.coords(f)[2]) / 2#тоже самое
    y1 = (c.coords(f)[1] + c.coords(f)[3]) / 2
    x2 = (c.coords(f1)[0] + c.coords(f1)[2]) / 2
    y2 = (c.coords(f1)[1] + c.coords(f1)[3]) / 2
    if not math.floor(((x2-x1)**2+(y2-y1)**2)**0.5) == RADIUS2 + RADIUS3:
        angle = angle - 0.1
        c.coords(f1, coords_1(angle))
        root.after(50, lambda: motion_1(angle))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question