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 46 47 48 49 50 51
| from tkinter import*
# Rebond et deplacement
def move():
global flag,dx,dy
x=can1.coords(balle)[0]
y=can1.coords(balle)[1]
if flag==1:
if x<=0:
dx=1
if x>=240:
dx=-1
if y<=0:
dy=1
if y>=190:
dy=-1
x,y = x+dx,y+dy
can1.coords(balle,x,y,x+10,y+10)
fen1.after(5,move)
def start():
global flag
if flag==0:
flag=1
move()
def stop():
global flag
flag=0
# Programme principal
x,y,dx,dy = 0,50,1,1
flag = 0
fen1 = Tk()
fen1.title('balle qui rebondit')
can1 = Canvas(fen1, bg='grey', width=250, height=200)
can1.pack(side=LEFT)
balle=can1.create_oval(x,y,x+10,y+10, fill='red', outline='red')
bou1=Button(fen1, text='Demarrer', width=8, command=start)
bou1.pack(side=TOP)
bou2=Button(fen1, text='Arreter', width=8, command=stop)
bou2.pack(side=TOP)
bou3=Button(fen1, text='Quitter', width=8, command=fen1.destroy)
bou3.pack(side=BOTTOM)
fen1.mainloop() |
Partager