Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import Tk , Canvas
- #different type of cell with id and color
- class type:
- empty = [ 0 , "grey"]
- start = [ 1 , "green"]
- finish = [ 2 , "red"]
- wall = [ 3 , "black"]
- to_test = [ 4 , "violet"]
- tested = [ 5 , "blue"]
- path = [ 6 , "orange"]
- provisoire= [ 7 , "yellow"]
- class cell():
- def __init__(self, master, x, y, type , size):
- self.master = master
- self.abs = x
- self.ord = y
- self.type= type
- self.size= size
- #change wall <-> empty
- def _switch(self):
- if self.type[0] == type.empty[0]:
- self.type = type.wall
- elif self.type[0] == type.wall[0]:
- self.type=type.empty
- # draw cell
- def draw(self):
- fill = self.type[1]
- xmin = self.abs * self.size
- xmax = xmin + self.size
- ymin = self.ord * self.size
- ymax = ymin + self.size
- self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill)
- class grid(Canvas):
- def __init__(self,master, rownbr, Columnbr, cellsize):
- Canvas.__init__(self, master, width = cellsize * Columnbr , height = cellsize * rownbr)
- self.cellsize = cellsize
- #matrix
- self.grid = []
- for row in range(rownbr):
- line = []
- for column in range(Columnbr):
- line.append(cell(self, column, row, type.empty, cellsize))
- self.grid.append(line)
- #place start and finish
- self.grid[4][4].type=type.start
- self.grid[rownbr-4][Columnbr-4].type=type.finish
- #mouse1
- self.bind("<Button-1>", self.Bout1)
- #mouse mouvement
- self.bind("<B1-Motion>", self.BoutDep)
- self.draw()
- #draw grid
- def draw(self):
- for ligne in self.grid:
- for case in ligne:
- case.draw()
- #grid position
- def _CoordEven(self, event):
- Column = int(event.y / self.cellsize)
- row = int(event.x / self.cellsize)
- return row, Column
- #action depending on type click
- #special action on start and finish
- def Bout1(self, event):
- x, y = self._CoordEven(event)
- type = self.grid[x][y].type
- if self.type[0] == type.debut[0]:
- self.type=type.provisoire
- #self.bind("<Button-11>", self.Bout11)
- elif self.type[0] == type.arrive[0]:
- self.type=type.provisoire
- #self.bind("<Button-11>", self.Bout11)
- elif self.type[0] == type.vide[0]:
- self.type= type.mur
- elif self.type[0] == type.mur[0]:
- self.type = type.vide
- elif self.type[0] == type.mur[0]:
- self.type = type.vide
- self.grid[x][y].draw()
- #if first click on start or finish , 2nd click will move start or finish
- #don't know how to code it
- #def Bout11(self, event):
- #if left click is pressed and moved , change only wall <-> empty
- def BoutDep(self,event):
- x, y = self._eventCoords(event)
- cell = self.grid[x][y]
- cell._switch()
- cell.draw()
- if __name__ == "__main__" :
- app = Tk()
- cellgrid = grid(app, 50, 50, 10)
- cellgrid.pack()
- app.mainloop()
Add Comment
Please, Sign In to add comment