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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| from tkinter import *
from random import randrange
# procédure générale de déplacement :
def move():
"déplacement de la balle"
global x1, y1, dx, dy, flag
x1, y1 = x1 +dx, y1 + dy
if x1 >210:
x1, dx, dy = 210,0,15
if y1 >210:
y1, dx, dy = 210,-15,0
if x1 <10:
x1, dx, dy = 10,0,-15
if y1 <10:
y1, dx, dy = 10,15,0
can1.coords(oval1, x1,y1, x1+30,y1+30)
if flag >0:
fen1.after(100,move) # boucler, après 50 millisecondes
def start_it():
"démarrage de l'animation"
global flag
if flag ==0:
flag =1
move()
def avance(gb, hb):
global x1, y1
x1, y1 = x1 +gb, y1 +hb
can1.coords(oval1, x1,y1, x1+30,y1+30)
# gestionnaires d'évènements:
def dep1_gauche():
avance(-50, 0)
def dep1_droite():
avance(50, 0)
def dep1_haut():
avance(0, -50)
def dep1_bas():
avance(0, 50)
# Programme principal
# les variables suivantes seront utilisées de manière globale:
x1, y1 = 10, 10 #coordonnées initiales
dx, dy = 15, 0
flag =0 #commutateur
# Création du widget principal ("maître"):
fen1 = Tk()
fen1.title("Super Ball")
# création des widgets "esclaves":
can1 = Canvas (fen1, bg='blue', height=300, width=300)
oval1= can1.create_oval(x1,y1,x1+30,y1+30,width=2,fill='yellow')
can1.pack(side=LEFT)
Button(fen1, text='Démarrer', command=start_it).pack(side=BOTTOM)
Button(fen1, text='Quitter', command=fen1.destroy).pack(side=BOTTOM)
Button(fen1, text='Gauche',command=dep1_gauche).pack()
Button(fen1, text='Droite', command=dep1_droite).pack()
Button(fen1, text='Haut', command=dep1_haut).pack()
Button(fen1, text='Bas', command=dep1_bas).pack()
# démarrage du réceptionnaire d'évènements (boucle principale):
fen1.mainloop() |
Partager