from Tkinter import * import math, time, threading try : import psyco psyco.full() except : pass class Branche(): def __init__( self, canvas, x1, y1, x2, y2): self.canvas = canvas self.x1 = x1#+2 self.y1 = y1#+2 self.x2 = x2#+2 self.y2 = y2#+2 self.crt1Master = None self.crt2Master = None #Draw branche self.line = self.canvas.create_line( self.x1, self.y1, self.x2, self.y2, width = 5, fill= "maroon") self.crant1 = self.canvas.create_oval( self.x1-3, self.y1-3, self.x1+3, self.y1+3, fill = "white", tag = "B" ) self.crant2 = self.canvas.create_oval( self.x2-3, self.y2-3, self.x2+3, self.y2+3, fill = "white", tag = "B") def run(self): if self.crt1Master != None : c1x1, c1y1, c1x2, c1y2 = self.canvas.coords( self.crt1Master ) self.canvas.coords(self.crant1, c1x1, c1y1, c1x2, c1y2) self.x1 = ( c1x2 + c1x1 ) /2 self.y1 = ( c1y2 + c1y1 ) /2 if self.crt2Master != None : c2x1, c2y1, c2x2, c2y2 = self.canvas.coords( self.crt2Master ) self.canvas.coords(self.crant2, c2x1, c2y1, c2x2, c2y2) self.x2 = ( c2x2 + c2x1 ) /2 self.y2 = ( c2y2 + c2y1 ) /2 self.canvas.coords(self.line, self.x1, self.y1, self.x2, self.y2 ) class RoueR(): TBPOSE = ( (-1,0),(-1,0),(-1,0),(-1,1),(-1,0),(-1,0),(-1,1),(-1,0),(-1,1),(-1,1),(-1,1),(0,1), (-1,1),(0,1),(0,1),(-1,1),(0,1), (0,1), (0,1), (0,1),(1,1), (0,1), (0,1), (1,1), (0,1),(1,1), (1,1), (1,1), (1,0), (1,1), (1,0), (1,0), (1,1), (1,0), (1,0), (1,0),(1,0), (1,-1),(1,0),(1,0),(1,-1),(1,0),(1,-1),(1,-1),(1,-1),(0,-1),(1,-1),(0,-1),(0,-1),(1,-1),(0,-1), (0,-1),(0,-1),(0,-1),(-1,-1),(0,-1),(0,-1),(-1,-1),(0,-1),(-1,-1),(-1,-1),(-1,-1),(-1,0),(-1,-1),(-1,0),(-1,0),(-1,-1),(-1,0) ) def __init__( self, canvas, x, y): x -= 3 y -= 3 self.index1 = 1 self.index2 = 17 self.index3 = 34 self.index4 = 51 self.canvas = canvas self.xh = x -10 self.yh = y -10 self.xb = x +20 self.yb = y +20 self.alpha = 0.01 self.centre = None self.perif = None self.tbCrant = [] self.speedR = 2 self.speedX = 0 self.speedY = 1 #dessiner roue self.perif = self.canvas.create_oval( self.xh , self.yh, self.xb, self.yb, fill = "gold" ) self.centre = self.canvas.create_oval( self.xh+12 , self.yh+12, self.xb-12, self.yb-12, fill = "white", tag = "R" ) self.tbCrant.append( self.canvas.create_oval( self.xh+12, self.yh+0, self.xb-12, self.yb-24, fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh+24, self.yh+12, self.xb, self.yb-12, fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh+12, self.yh+24, self.xb-12, self.yb , fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh, self.yh+12, self.xb-24, self.yb-12, fill = "white", tag = "R" ) ) def run(self): itemCoords = self.canvas.coords( self.perif ) i = 0 #tant qu'il ny a pas d'obstacle regarder plus loing juska speedY while i < self.speedY and self.canvas.find_overlapping( itemCoords[0]+15, itemCoords[3]+2+i, itemCoords[0]+15, itemCoords[3]+2+i, ) == () : i += 0.5 if i != 0 : #Si l'obstacle n'est pas juste dessou : self.canvas.move( self.perif, self.speedX, i ) self.canvas.move( self.centre, self.speedX, i ) for item in self.tbCrant : self.canvas.move(item, self.speedX, i ) if self.speedY < 20 : self.speedY += 0.5 if self.speedX < 0 : self.speedX += 0.5 else : self.speedY = 1 self.canvas.move(self.perif, self.speedX, 0 ) self.canvas.move(self.centre, self.speedX, 0 ) for item in self.tbCrant : self.canvas.move(item, self.speedX, 0 ) if self.speedX < self.speedR : self.speedX -= 0.5 i= x1= y1= x2= y2= x3= y3= x4= y4 = 0 while i < self.speedR : x1 += self.TBPOSE[self.index2][0] y1 += self.TBPOSE[self.index2][1] x2 += self.TBPOSE[self.index1][0] y2 += self.TBPOSE[self.index1][1] x3 += self.TBPOSE[self.index3][0] y3 += self.TBPOSE[self.index3][1] x4 += self.TBPOSE[self.index4][0] y4 += self.TBPOSE[self.index4][1] self.index1 += 1 self.index2 += 1 self.index3 += 1 self.index4 += 1 if self.index1 > 67 : self.index1 = 0 if self.index2 > 67 : self.index2 = 0 if self.index3 > 67 : self.index3 = 0 if self.index4 > 67 : self.index4 = 0 i += 1 self.canvas.move( self.tbCrant[3], x1, y1 ) self.canvas.move( self.tbCrant[0], x2, y2 ) self.canvas.move( self.tbCrant[2], x3, y3 ) self.canvas.move( self.tbCrant[1], x4, y4 ) class RoueL(): TBPOSE = ((1,0), (1,0), (1,0), (1,1), (1,0), (1,0), (1,1), (1,0), (1,1), (1,1), (1,1), (0,1), (1,1), (0,1), (0,1), (1,1),(0,1), (0,1), (0,1), (0,1),(-1,1), (0,1), (0,1),(-1, 1),(0,1),(-1,1),(-1,1),(-1,1),(-1,0),(-1,1),(-1,0),(-1,0),(-1,1),(-1,0), (-1,0), (-1,0), (-1,0), (-1,-1), (-1,0), (-1,0), (-1,-1), (-1,0), (-1,-1), (-1,-1), (-1,-1), (0,-1), (-1,-1), (0,-1), (0,-1), (-1,-1),(0,-1), (0,-1), (0,-1), (0,-1),(1,-1), (0,-1), (0,-1),(1, -1),(0,-1),(1,-1),(1,-1),(1,-1),(1,0),(1,-1),(1,0),(1,0),(1,-1),(1,0),) def __init__( self, canvas, x, y): x -= 3 y -= 3 self.index = 1 self.canvas = canvas self.xh = x -10 self.yh = y -10 self.xb = x +20 self.yb = y +20 self.alpha = 0.01 self.tbCrant = [] self.stateRun = False #dessiner roue self.canvas.create_oval( self.xh , self.yh, self.xb, self.yb, fill = "orange red" ) self.canvas.create_oval( self.xh+12 , self.yh+12, self.xb-12, self.yb-12, fill = "white", tag = "R" ) self.tbCrant.append( self.canvas.create_oval( self.xh+12, self.yh+0, self.xb-12, self.yb-24, fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh+24, self.yh+12, self.xb, self.yb-12, fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh+12, self.yh+24, self.xb-12, self.yb , fill = "white", tag = "R" ) ) self.tbCrant.append( self.canvas.create_oval( self.xh, self.yh+12, self.xb-24, self.yb-12, fill = "white", tag = "R" ) ) def run(self): #for crant in self.tbCrant: #x, y = 50+170*math.sin(self.alpha), 50+170*math.cos(self.alpha) stateRun = True x1, y1 = self.TBPOSE[self.index] x2, y2 = self.TBPOSE[self.index-51] x3, y3 = self.TBPOSE[self.index-34] x4, y4 = self.TBPOSE[self.index-17] self.canvas.move( self.tbCrant[0], x1, y1 ) self.canvas.move( self.tbCrant[1], x2, y2 ) self.canvas.move( self.tbCrant[2], x3, y3 ) self.canvas.move( self.tbCrant[3], x4, y4 ) self.index += 1 if self.index > 67 : self.index = 0 #import Canvas.find_closest() class app(threading.Thread): """Fenetre du jeu""" def __init__(self, fps): threading.Thread.__init__(self) self.sleepTime = 1.0/fps self.graviti = 1 self.currentObjet = None self.lstObjet = [] self.firstCoordBrche = None self.idCrant1 = None self.running = False self.root = Tk() self.monde = Canvas(self.root, bg = "sky blue", width = 1200, height = 800) self.monde.pack() frmBut = Frame(self.root) self.butStartStop = Button(frmBut, text = "Start", width=15, command = self.start ) #start est obtenu par eritage self.butStartStop.pack(side = LEFT) Button(frmBut, text = "RoueL", width=15, command = lambda x = 1 : self.selectObjet( x ) ).pack(side = LEFT) Button(frmBut, text = "RoueR", width=15, command = lambda x = 2 : self.selectObjet( x ) ).pack(side = LEFT) Button(frmBut, text = "Branche", width=15, command = lambda x = 3 : self.selectObjet( x ) ).pack(side = LEFT) frmBut.pack() self.root.bind("Destroy", self.stop) self.monde.bind("", lambda event : self.placeObject( event ) ) #========================================# # DECORT # self.monde.create_polygon(0,600, 200,600, 200,500, 400,500, 400,1000, fill = "chartreuse") #========================================# def add_object(self, objet): self.lstObjet.append(objet) def selectObjet(self, objet): self.currentObjet = objet def placeObject(self, event): if not self.running : if self.currentObjet == 1 :# Placer une RoueL self.lstObjet.append( RoueL(self.monde, event.x, event.y ) ) elif self.currentObjet == 2 :# Placer une RoueR self.lstObjet.append( RoueR(self.monde, event.x, event.y ) ) elif self.currentObjet == 3 :# Placer une Branche (2 etapes) if self.firstCoordBrche == None : #Placer 1 er point de la branche self.firstCoordBrche = ( event.x, event.y, ) self.idCrant1 = None #recupere tous les objet dans les environs du clic tupleItem = self.monde.find_enclosed( event.x -10, event.y -10, event.x +10, event.y +10 ) for item in tupleItem : if self.monde.gettags( item )[0] == 'R' : coordsItem = self.monde.coords(item) x = (coordsItem[0] + coordsItem[2]) /2 y = (coordsItem[1] + coordsItem[3]) /2 self.firstCoordBrche = ( x, y ) self.idCrant1 = item break if self.monde.gettags( item )[0] == 'B' : coordsItem = self.monde.coords(item) x = (coordsItem[0] + coordsItem[2]) /2 y = (coordsItem[1] + coordsItem[3]) /2 self.firstCoordBrche = ( x, y ) self.idCrant1 = item else :# #Placer 2 eme point de la branche idCrant2 = None tupleItem = self.monde.find_enclosed( event.x -10, event.y -10, event.x +10, event.y +10 ) x1, y1 = self.firstCoordBrche x2, y2 = event.x, event.y for item in tupleItem : if self.monde.gettags( item )[0] == 'R' : posCrant = self.monde.coords(item) x2 = (posCrant[0] + posCrant[2]) /2 y2 = (posCrant[1] + posCrant[3]) /2 idCrant2 = item break if self.monde.gettags( item )[0] == 'B' : posCrant = self.monde.coords(item) x2 = (posCrant[0] + posCrant[2]) /2 y2 = (posCrant[1] + posCrant[3]) /2 idCrant2 = item self.lstObjet.append( Branche(self.monde, x1, y1, x2, y2 ) ) self.lstObjet[-1].crt1Master = self.idCrant1 self.lstObjet[-1].crt2Master = idCrant2 self.firstCoordBrche = None def run(self): self.running = True self.butStartStop.configure(text = "Stop", command = self.stop, bg = "green" ) while self.running : for myObject in self.lstObjet : myObject.run() time.sleep( self.sleepTime ) def stop(self) : self.running = False self.butStartStop.configure(text = "Restart", command = self.stop, bg = "red" ) del self def sub_object(self, objet): lstObjet.remove(objet) game = app(15) game.root.mainloop()