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 randint
from math import sqrt
import os
def bouger():
global x, y, sensX, sensY, vitesse, dx, dy, flag
x , y = x+dx*sensX, y+dy*sensY
if x < 10:
x = 10
sensX = 1
dx = randint(1,5)
dy = randint(1,5)
if y < 10:
y = 10
sensY = 1
dx = randint(1,5)
dy = randint(1,5)
if x > 290:
x = 290
sensX = -1
dx = randint(1,5)
dy = randint(1,5)
if y > 290:
y = 290
sensY = -1
dx = randint(1,5)
dy = randint(1,5)
vitesse = int((sqrt(dx*dx + dy*dy)*50)/sqrt(5*5+5*5))
can.coords(oval, x-5, y-5, x+5, y+5)
os.system('clear')
print("dx = ", dx, "dy = ", dy, "vitesse = ", vitesse, "distance =", sqrt(dx*dx + dy*dy))
if flag > 0:
flag = 1
fen.after(vitesse, bouger)
def go():
global flag
if flag == 0:
flag = 1
bouger()
def arret():
global flag
flag = 0
x, y = 10, 10
dx, dy = 5, 5
flag = 0
vitesse = 50
sensX, sensY = 1, 1
fen = Tk()
can = Canvas(fen, height = 300, width = 300, bg= "grey")
can.pack(side = LEFT, padx = 5, pady= 5)
oval = can.create_oval(x-5, y-5, x+5, y+5, fill = "red")
bou1 = Button(fen, text="Démarrer", command=go)
bou1.pack()
bou2 = Button(fen, text="Arrêt", command=arret)
bou2.pack()
fen.mainloop() |
Partager