-
Notifications
You must be signed in to change notification settings - Fork 12
/
grid.py
64 lines (52 loc) · 2.21 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pygame
import numpy as np
import random
class Grid:
def __init__(self, width, height, scale, offset):
self.scale = scale
self.columns = int(height/scale)
self.rows = int(width/scale)
self.size = (self.rows, self.columns)
self.grid_array = np.ndarray(shape=(self.size))
self.offset = offset
def random2d_array(self):
for x in range(self.rows):
for y in range(self.columns):
self.grid_array[x][y] = random.randint(0,1)
def Conway(self, off_color, on_color, surface, pause):
for x in range(self.rows):
for y in range(self.columns):
y_pos = y * self.scale
x_pos = x * self.scale
#random_color = (random.randint(10, 255), random.randint(10, 255), random.randint(10, 255))
if self.grid_array[x][y] == 1:
pygame.draw.rect(surface, on_color, [x_pos, y_pos, self.scale-self.offset, self.scale-self.offset])
else:
pygame.draw.rect(surface, off_color, [x_pos, y_pos, self.scale-self.offset, self.scale-self.offset])
next = np.ndarray(shape=(self.size))
if pause == False:
for x in range(self.rows):
for y in range(self.columns):
state = self.grid_array[x][y]
neighbours = self.get_neighbours( x, y)
if state == 0 and neighbours == 3:
next[x][y] = 1
elif state == 1 and (neighbours < 2 or neighbours > 3):
next[x][y] = 0
else:
next[x][y] = state
self.grid_array = next
def HandleMouse(self, x, y):
_x = x//self.scale
_y = y//self.scale
if self.grid_array[_x][_y] != None:
self.grid_array[_x][_y] = 1
def get_neighbours(self, x, y):
total = 0
for n in range(-1, 2):
for m in range(-1, 2):
x_edge = (x+n+self.rows) % self.rows
y_edge = (y+m+self.columns) % self.columns
total += self.grid_array[x_edge][y_edge]
total -= self.grid_array[x][y]
return total