-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.py
57 lines (46 loc) · 2.03 KB
/
body.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
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)###WHY
def Conway(self, off_color, on_color, surface):
for x in range(self.rows):
for y in range(self.columns):
y_pos = y * self.scale
x_pos = x * self.scale
###What does this do
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))
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 ####why all these things. explain why
def get_neighbours(self, x, y):
total = 0
for n in range(-1,2):#why in range(-1,2)
for m in range(-1,2):
x_edge = (x+n+self.rows) % self.rows###Explain this please
y_edge = (y+m+self.columns) % self.columns
total += self.grid_array[x_edge][y_edge]
total -= self.grid_array[x][y]
return total