1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
from tkinter import *
SIDE=400
PAD=SIDE//12
dx,dy=2,2
def move():
global dx
global dy
#global timer
ulx, uly, lrx, lry = list(map(int, cvs.coords(ball)))
# bords verticaux
if ulx <=0 or lrx >=SIDE:
dx=-dx
# bords horizontaux
elif uly<=0 or lry>=SIDE:
dy=-dy
cvs.move(ball, dx, dy)
timer=cvs.after(20, move)
# Création dun canevas
w=Tk()
cvs=Canvas(w, width=SIDE, height=SIDE, highlightthickness=0,
bg="ivory")
cvs.pack(padx=PAD, pady=PAD)
# Création dune balle
R=20
x0=200
y0=40
ball=cvs.create_oval(x0, y0, 2*R+x0, 2*R+y0, outline='#ff0000',
fill='#ff0000')
# Lancement de lanimation
timer=cvs.after(20, move)
def quitter():
cvs.after_cancel(timer)
w.destroy()
w.protocol('WM_DELETE_WINDOW', quitter)
w.mainloop() |
Partager