Guest User

Untitled

a guest
Feb 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. from tkinter import Tk , Canvas
  2.  
  3. #different type of cell with id and color
  4.  
  5. class type:
  6.  
  7.  
  8. empty = [ 0 , "grey"]
  9. start = [ 1 , "green"]
  10. finish = [ 2 , "red"]
  11. wall = [ 3 , "black"]
  12. to_test = [ 4 , "violet"]
  13. tested = [ 5 , "blue"]
  14. path = [ 6 , "orange"]
  15. provisoire= [ 7 , "yellow"]
  16.  
  17. class cell():
  18.  
  19.  
  20. def __init__(self, master, x, y, type , size):
  21.  
  22. self.master = master
  23. self.abs = x
  24. self.ord = y
  25. self.type= type
  26. self.size= size
  27.  
  28. #change wall <-> empty
  29. def _switch(self):
  30. if self.type[0] == type.empty[0]:
  31. self.type = type.wall
  32. elif self.type[0] == type.wall[0]:
  33. self.type=type.empty
  34.  
  35. # draw cell
  36. def draw(self):
  37. fill = self.type[1]
  38.  
  39.  
  40. xmin = self.abs * self.size
  41. xmax = xmin + self.size
  42. ymin = self.ord * self.size
  43. ymax = ymin + self.size
  44.  
  45. self.master.create_rectangle(xmin, ymin, xmax, ymax, fill = fill)
  46.  
  47. class grid(Canvas):
  48.  
  49. def __init__(self,master, rownbr, Columnbr, cellsize):
  50.  
  51. Canvas.__init__(self, master, width = cellsize * Columnbr , height = cellsize * rownbr)
  52.  
  53. self.cellsize = cellsize
  54.  
  55. #matrix
  56. self.grid = []
  57. for row in range(rownbr):
  58. line = []
  59. for column in range(Columnbr):
  60. line.append(cell(self, column, row, type.empty, cellsize))
  61.  
  62. self.grid.append(line)
  63. #place start and finish
  64. self.grid[4][4].type=type.start
  65. self.grid[rownbr-4][Columnbr-4].type=type.finish
  66.  
  67. #mouse1
  68. self.bind("<Button-1>", self.Bout1)
  69. #mouse mouvement
  70. self.bind("<B1-Motion>", self.BoutDep)
  71.  
  72. self.draw()
  73.  
  74. #draw grid
  75. def draw(self):
  76. for ligne in self.grid:
  77. for case in ligne:
  78. case.draw()
  79.  
  80. #grid position
  81. def _CoordEven(self, event):
  82. Column = int(event.y / self.cellsize)
  83. row = int(event.x / self.cellsize)
  84. return row, Column
  85.  
  86. #action depending on type click
  87. #special action on start and finish
  88. def Bout1(self, event):
  89. x, y = self._CoordEven(event)
  90. type = self.grid[x][y].type
  91.  
  92. if self.type[0] == type.debut[0]:
  93. self.type=type.provisoire
  94. #self.bind("<Button-11>", self.Bout11)
  95. elif self.type[0] == type.arrive[0]:
  96. self.type=type.provisoire
  97. #self.bind("<Button-11>", self.Bout11)
  98. elif self.type[0] == type.vide[0]:
  99. self.type= type.mur
  100. elif self.type[0] == type.mur[0]:
  101. self.type = type.vide
  102. elif self.type[0] == type.mur[0]:
  103. self.type = type.vide
  104. self.grid[x][y].draw()
  105.  
  106. #if first click on start or finish , 2nd click will move start or finish
  107. #don't know how to code it
  108. #def Bout11(self, event):
  109.  
  110.  
  111. #if left click is pressed and moved , change only wall <-> empty
  112. def BoutDep(self,event):
  113. x, y = self._eventCoords(event)
  114. cell = self.grid[x][y]
  115.  
  116. cell._switch()
  117. cell.draw()
  118.  
  119. if __name__ == "__main__" :
  120.  
  121. app = Tk()
  122.  
  123. cellgrid = grid(app, 50, 50, 10)
  124. cellgrid.pack()
  125.  
  126. app.mainloop()
Add Comment
Please, Sign In to add comment