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
| from tkinter import *
def move():
global x, y, flag, dx, dy
if x > 280 or x < 20:
dx = -dx
x += dx
if y > 280 or y < 20:
dy = - dy
y += dy
can.coords(ball, x-10, y-10, x+10, y+10)
if flag > 0:
fen.after(50, move)
def start():
global flag
flag += 1
if flag == 1:
move()
def stop():
global flag
flag = 0
# Création des variables:
x, y = 150, 280
flag = 0
dx, dy = 5, 5
# Création de la fenêtre:
fen = Tk()
fen.title("Petit jeu avec Tkinter")
# Création du canevas:
can = Canvas(fen, bg= "light grey", width= 300, height= 300)
can.grid(row= 2, column= 0)
# Création de la balle:
ball = can.create_oval(x-10, y-10, x+10, y+10, outline= "black", fill= "light blue")
# Création du lien entre le souris et le canevas:
can.bind("<Button-1>")
# Création du label "Score":
labScore = Label(fen, text= "Nombre de Points: 0")
labScore.grid(row= 3, column= 0)
# Création des boutons:
# Création du bouton "Débuter la Partie":
Button(fen, text= "Débuter la Partie", command= start).grid(row= 0, column= 0)
# Création du bouton "Arrêter la Partie":
Button(fen, text= "Arrêter la Partie", command= stop).grid(row= 1, column= 0)
# Création du bouton "Quitter":
Button(fen, text= "Quitter", command= fen.quit).grid(row= 4, column= 0)
fen.mainloop()
fen.destroy() |
Partager