Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. from random import randrange
  4. master = Tk()
  5.  
  6.  
  7. class StartUp:
  8. canvasWidth = 800
  9. canvasHeight = 800
  10. def __init__(self, canvasWidth, canvasHeight):
  11. self.canvasWidth = canvasWidth
  12. self.canvasHeight = canvasHeight
  13. self.cnv = Canvas(master, width = self.canvasWidth, height = self.canvasHeight)
  14. self.cnv.pack()
  15. def drawPoint(self, x, y, z, w ):
  16. self.cnv.create_rectangle(x, y, z, w, fill = "#222222")
  17. def clear(self):
  18. self.cnv.delete("all")
  19.  
  20.  
  21. class Simulation:
  22. grid = []
  23. mirrorGrid = []
  24. vectorX = 0
  25. vectorY = 0
  26. def __init__(self, width):
  27. self.width = width
  28. self.createGrid(self.grid)
  29. self.createGrid(self.mirrorGrid)
  30. def createGrid(self, grid):
  31. for x in range(self.width):
  32. innerGrid = []
  33. for y in range(self.width):
  34. innerGrid.append(0)
  35. grid.append(innerGrid)
  36. def populateGrid(self, quantity):
  37. for x in range(self.width):
  38. for y in range(self.width):
  39. if randrange(quantity) == 1:
  40. self.grid[x][y] = 1
  41. def drawGrid(self, drawPoint, vector):
  42. for x in range(self.width):
  43. self.vectorX = self.vectorX + vector
  44. for y in range(self.width):
  45. self.vectorY = self.vectorY + vector
  46. if self.grid[x][y] == 1:
  47. drawPoint((x + self.vectorX), (y + self.vectorY), (x + self.vectorX + vector), (y + self.vectorY + vector))
  48. self.vectorY = 0
  49. def updateGrid(self):
  50. for x in range(1, self.width-1, 1):
  51. for y in range(1, self.width-1, 1):
  52. totalCells = 0
  53. totalCells = totalCells + self.grid[x - 1][y - 1]
  54. totalCells = totalCells + self.grid[x - 1][y]
  55. totalCells = totalCells + self.grid[x - 1][y + 1]
  56. totalCells = totalCells + self.grid[x][y - 1]
  57. totalCells = totalCells + self.grid[x][y + 1]
  58. totalCells = totalCells + self.grid[x + 1][y - 1]
  59. totalCells = totalCells + self.grid[x + 1][y]
  60. totalCells = totalCells + self.grid[x + 1][y + 1]
  61. if totalCells == 2:
  62. self.mirrorGrid[x][y] = self.grid[x][y]
  63. elif totalCells == 3:
  64. self.mirrorGrid[x][y] = 1
  65. else:
  66. self.mirrorGrid[x][y] = 0
  67. for x in range(1, self.width-1, 1):
  68. self.mirrorGrid[x][0] = self.mirrorGrid[x][self.width - 3]
  69. self.mirrorGrid[x][self.width - 2] = self.mirrorGrid[x][1]
  70. self.mirrorGrid[0][x] = self.mirrorGrid[self.width - 3][x]
  71. self.mirrorGrid[self.width - 2][x] = self.mirrorGrid[1][x]
  72. self.grid = self.mirrorGrid.copy()
  73. def clearVectors(self):
  74. self.vectorX = 0
  75. self.vectorY = 0
  76.  
  77. canvas = StartUp(1200, 1200)
  78. i = True
  79. simulation = Simulation(50)
  80. simulation.populateGrid(11)
  81. simulation.drawGrid(canvas.drawPoint, 20)
  82. while i == True:
  83. time.sleep(0.090)
  84. simulation.clearVectors()
  85. canvas.clear()
  86. simulation.updateGrid()
  87. simulation.drawGrid(canvas.drawPoint, 20)
  88. master.update()
  89. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement