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 71 72 73
|
from Tkinter import *
from time import *
def initialize(title = 'MonApplication'):
global app
app = Tk()
app.title(title)
app.geometry('640x500')
#Canvas
global can
can = Canvas(app, width = 640, height = 480, bg = 'green')
can.pack()
#Label
new = Label(app,text = 'Appuyer sur N pour demarrer')
new.pack()
#Evenement
app.bind_all('<n>',Nouveau)
app.bind_all('<q>',Quit)
#Coordonnée balle
global x,y
x , y = 50, 50
def Nouveau(event): #Quand on appui sur N
Animate()
def affiche(x,y):
drawcircle(x,y,30)
can.update()
def drawcircle(x,y,rad):
global boule
boule = can.create_oval(x-rad, y-rad, x+rad, y+rad,fill = 'red')
def Animate():
global dx,dy #Les directions
global x,y
dx,dy = 1,1
while 1:
#On change les coordonnée
x = x+ (dx* 10)
y = y+ (dy*10)
if x > 610:
dx = -dx
if x < 30:
dx = -dx
if y >450:
dy = -dy
if y<30:
dy = -dy
can.after(30,affiche(x,y))
can.delete(boule)
def Quit(event):
app.destroy()
#Le main
initialize('Une balle qui rebondit')
app.mainloop()
app.destroy() |
Partager