tkinter 모듈을 이용하여 사각영역에서 공이 이동하면서 테두리에서 반사하는 기본적인 애니메이션 만들기
from tkinter import *
import time
win = Tk()
win.geometry("800x800")
win.title("이동 애니메이션 기본")
c = Canvas(win ,width=800 ,height=800)
c.pack()
oval = c.create_oval(5,5,60,60,fill='red')
a = 1
b = 1
for x in range(0 ,100):
c.move(oval,a,b)
win.update()
time.sleep(.03)
win.mainloop()
경계에 충돌하고 반사하는 공 애니메이션
from tkinter import *
import time
win = Tk()
win.title("First title")
win.geometry("600x400")
c = Canvas(win ,width=600 ,height=400, bg="#559955")
c.pack()
oval = c.create_oval(5,5,60,60,fill='white')
xd = 2
yd = 2
while True:
c.move(oval,xd,yd)
# 현재 볼의 위치([x1,y1, x2,y2])를 구함
p = c.coords(oval) # Return a list of coordinates for the item given in ARGS.
if p[3] >= 400 or p[1] <=0: # 상하 영역을 벗어났는지 확인
yd = -yd
if p[2] >=600 or p[0] <=0: # 좌우 영역을 벗어났는지 확인
xd = -xd
win.update()
time.sleep(0.01)
win.mainloop()