Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time
- from random import randrange
- master = Tk()
- class StartUp:
- canvasWidth = 800
- canvasHeight = 800
- def __init__(self, canvasWidth, canvasHeight):
- self.canvasWidth = canvasWidth
- self.canvasHeight = canvasHeight
- self.cnv = Canvas(master, width = self.canvasWidth, height = self.canvasHeight)
- self.cnv.pack()
- def drawPoint(self, x, y, z, w ):
- self.cnv.create_rectangle(x, y, z, w, fill = "#222222")
- def clear(self):
- self.cnv.delete("all")
- class Simulation:
- grid = []
- mirrorGrid = []
- vectorX = 0
- vectorY = 0
- def __init__(self, width):
- self.width = width
- self.createGrid(self.grid)
- self.createGrid(self.mirrorGrid)
- def createGrid(self, grid):
- for x in range(self.width):
- innerGrid = []
- for y in range(self.width):
- innerGrid.append(0)
- grid.append(innerGrid)
- def populateGrid(self, quantity):
- for x in range(self.width):
- for y in range(self.width):
- if randrange(quantity) == 1:
- self.grid[x][y] = 1
- def drawGrid(self, drawPoint, vector):
- for x in range(self.width):
- self.vectorX = self.vectorX + vector
- for y in range(self.width):
- self.vectorY = self.vectorY + vector
- if self.grid[x][y] == 1:
- drawPoint((x + self.vectorX), (y + self.vectorY), (x + self.vectorX + vector), (y + self.vectorY + vector))
- self.vectorY = 0
- def updateGrid(self):
- for x in range(1, self.width-1, 1):
- for y in range(1, self.width-1, 1):
- totalCells = 0
- totalCells = totalCells + self.grid[x - 1][y - 1]
- totalCells = totalCells + self.grid[x - 1][y]
- totalCells = totalCells + self.grid[x - 1][y + 1]
- totalCells = totalCells + self.grid[x][y - 1]
- totalCells = totalCells + self.grid[x][y + 1]
- totalCells = totalCells + self.grid[x + 1][y - 1]
- totalCells = totalCells + self.grid[x + 1][y]
- totalCells = totalCells + self.grid[x + 1][y + 1]
- if totalCells == 2:
- self.mirrorGrid[x][y] = self.grid[x][y]
- elif totalCells == 3:
- self.mirrorGrid[x][y] = 1
- else:
- self.mirrorGrid[x][y] = 0
- for x in range(1, self.width-1, 1):
- self.mirrorGrid[x][0] = self.mirrorGrid[x][self.width - 3]
- self.mirrorGrid[x][self.width - 2] = self.mirrorGrid[x][1]
- self.mirrorGrid[0][x] = self.mirrorGrid[self.width - 3][x]
- self.mirrorGrid[self.width - 2][x] = self.mirrorGrid[1][x]
- self.grid = self.mirrorGrid.copy()
- def clearVectors(self):
- self.vectorX = 0
- self.vectorY = 0
- canvas = StartUp(1200, 1200)
- i = True
- simulation = Simulation(50)
- simulation.populateGrid(11)
- simulation.drawGrid(canvas.drawPoint, 20)
- while i == True:
- time.sleep(0.090)
- simulation.clearVectors()
- canvas.clear()
- simulation.updateGrid()
- simulation.drawGrid(canvas.drawPoint, 20)
- master.update()
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement