-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pattern.py
143 lines (107 loc) · 3.8 KB
/
Pattern.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import pygame
import random
pygame.init()
def makeGrid(rows, columns):
Matrix = [[0 for x in range(rows)] for y in range(columns)]
for object in Matrix:
for i in range(len(object)):
object[i] = 0
return Matrix
def updateWin():
global Size, Matrix, window, height
# Sets the pixels
pixel_size = width / Size
for i in range(Size):
for j in range(Size):
if Matrix[i][j] == 0:
pygame.draw.rect(window, (255, 255, 255), pygame.Rect(pixel_size*i, pixel_size*j, pixel_size, pixel_size))
elif Matrix[i][j] == 1:
pygame.draw.rect(window, (0, 0, 0), pygame.Rect(pixel_size*i, pixel_size*j, pixel_size, pixel_size))
# draws grid lines
for line in range(Size):
pass
pygame.draw.line(window, (116, 116, 116), (0, line * width/Size), (width, line * width/Size))
pygame.draw.line(window, (116, 116, 116), (line * height/Size, 0), (line * height/Size, height))
pygame.display.update()
def moveCells():
global Matrix
MatrixNewGen = makeGrid(Size, Size)
for i in range(Size):
for j in range(Size):
surroundingCount = 0
try:
surroundingCells = [Matrix[i-1][j-1], Matrix[i-1][j+0], Matrix[i-1][j+1],
Matrix[i+0][j-1], Matrix[i+0][j+1],
Matrix[i+1][j-1], Matrix[i+1][j+0], Matrix[i+1][j+1]]
except:
if i==(Size-1):
surroundingCells = [Matrix[i-1][j]]
if j == (Size-1):
surroundingCells = [Matrix[i + 0][j - 1]]
for cell in surroundingCells:
surroundingCount += cell
if Matrix[i][j] == 0:
if surroundingCount == 1:
MatrixNewGen[i][j] = 1
elif Matrix[i][j] == 1:
MatrixNewGen[i][j] = Matrix[i][j]
Matrix = MatrixNewGen
def randomize():
global Matrix
for object in Matrix:
for i in range(len(object)):
chance = random.randint(0, 50)
if chance == 1:
object[i] = 1
def clear():
global Matrix
for object in Matrix:
for i in range(len(object)):
object[i] = 0
def MouseSetting():
global Matrix
MouseCords = pygame.mouse.get_pos()
MouseX = int(MouseCords[0] // (width/Size))
MouseY = int(MouseCords[1] // (height/Size))
if Matrix[MouseX][MouseY] == 1:
Matrix[MouseX][MouseY] = 0
elif Matrix[MouseX][MouseY] == 0:
Matrix[MouseX][MouseY] = 1
## SETUP VARIABLES ##
width, height = 650, 650
FPS = 30
pygame.display.set_caption("Pattern 0/1/1/M")
window = pygame.display.set_mode((width, height))
Size = 150
Matrix = makeGrid(Size, Size)
## SETUP VARIABLES ##
print("Press up key to unpause, down key to pause, left to randomize, and right to clear")
def main():
clock = pygame.time.Clock()
move = False
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left Mouse Button
MouseSetting()
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
move = False
if keys[pygame.K_UP]:
move = True
if keys[pygame.K_LEFT]:
randomize()
if keys[pygame.K_RIGHT]:
clear()
if move:
moveCells()
updateWin()
pygame.quit()
quit()
if __name__ == "__main__":
main()