Vitalianskiy
@Vitalianskiy
Student

Как остановить 2d анимацию?

Мне нужно остановить эти два круга в момент прикосновения
Подскажите или наведите в правильное русло, может какие-то книги есть
Вот собственно код:
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()
  • Вопрос задан
  • 171 просмотр
Решения вопроса 1
@QPDEH
Даже не джун, любитель
Вот держи. Пришлось малость колхозить, но работает
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))
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы