-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sand.py
154 lines (117 loc) · 4.28 KB
/
Sand.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
144
145
146
147
148
149
150
151
152
153
154
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
for i in range(rows):
Matrix[i][rows-1] = 2
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, (0, 0, 0), pygame.Rect(pixel_size*i, pixel_size*j, pixel_size, pixel_size))
elif Matrix[i][j] == 1:
pygame.draw.rect(window, (242, 209, 107), pygame.Rect(pixel_size*i, pixel_size*j, pixel_size, pixel_size))
elif Matrix[i][j] == 2:
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):
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))
for i in range(Size):
Matrix[i][Size-1] = 2
pygame.display.update()
def moveCells():
global Matrix
MatrixNewGen = makeGrid(Size, Size)
for i in range(Size):
for j in range(Size):
try:
surroundingCells = [Matrix[i][j+1], Matrix[i+1][j+1], Matrix[i-1][j+1]]
if Matrix[i][j] == 1:
if surroundingCells[0] == 0:
MatrixNewGen[i][j+1] = 1
MatrixNewGen[i][j] = 0
else:
if surroundingCells[1] == 0 and surroundingCells[2] == 0:
choice = random.randint(0, 1)
if choice == 1:
MatrixNewGen[i+1][j+1] = 1
MatrixNewGen[i][j] = 0
elif choice == 0:
MatrixNewGen[i-1][j + 1] = 1
MatrixNewGen[i][j] = 0
elif surroundingCells[1] == 0:
MatrixNewGen[i + 1][j + 1] = 1
MatrixNewGen[i][j] = 0
elif surroundingCells[2] == 0:
MatrixNewGen[i - 1][j + 1] = 1
Matrix[i][j] = 0
else:
MatrixNewGen[i][j] = 1
except:
pass
Matrix = MatrixNewGen
def randomize():
global Matrix
for object in Matrix:
for i in range(len(object)):
object[i] = random.randint(0, 1)
for i in range(Size):
Matrix[i][Size-1] = 2
def clear():
global Matrix
for object in Matrix:
for i in range(len(object)):
object[i] = 0
for i in range(Size):
Matrix[i][Size-1] = 2
def MouseSetting():
global Matrix
MouseCords = pygame.mouse.get_pos()
MouseX = int(MouseCords[0] // (width/Size))
MouseY = int(MouseCords[1] // (height/Size))
Matrix[MouseX][MouseY] = 1
## SETUP VARIABLES ##
width, height = 650, 650
FPS = 30
pygame.display.set_caption("Falling Sand")
window = pygame.display.set_mode((width, height))
Size = 100
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
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()