| 12
 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
 
 | #---------------------------------------------------------
# Robot POO
# 19/10/2013
#---------------------------------------------------------
from tkinter import *
from math import *
from random import *
from time import *
#---------------------------------------------------------
#Lancement :
def depart():
	robot=Bloc(cnv1,randint(1,100),randint(1,100),10,'blue')
	cible=Bloc(cnv1,30,40,10,'red')
	
	for i in range(10):
		pause(0.5)
		robot.mvt(5,5)
def pause(t):
	#pause de t secondes
	fnt1.update()
	sleep(t)
	
 
#---------------------------------------------------------
#Objets :
class Bloc(object):
	"""Petit bloc carré """
	def __init__(self,cnv,x,y,taille,couleur):
		self.cnv=cnv		#réf au canevas
		self.x=x
		self.y=y
		self.taille=taille
		self.couleur=couleur
		self.carre = cnv.create_rectangle(x,y,x+taille,y+taille,fill=couleur)
			
	def mvt(self,dx,dy):
		self.x=randint(1,100)
		self.y=randint(1,100)
#		print(cnv1.coords(self)) # Que réprésente self ici?
		cnv1.coords(self.carre,self.x,self.y,self.x+10,self.y+10)
		
#---------------------------------------------------------
#Zone graphique :
#Fenêtre principale :
fnt1 = Tk()
#Canevas :
cnv1 = Canvas(fnt1, width=300, height=200, bg='grey')
cnv1.pack(side=LEFT)
#Boutons :
bt_depart=Button(fnt1, text="départ",command=depart)
bt_depart.pack(side=TOP)
bt_fin=Button(fnt1, text="stop",command=fnt1.destroy)
bt_fin.pack(side=TOP)
bt_rob_x=Label(fnt1,text="x_rob")
bt_rob_x.pack(side=TOP)
#Boucle principale :
fnt1.mainloop() | 
Partager