-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWater.py
157 lines (154 loc) · 7.26 KB
/
Water.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
155
156
157
import pygame
import numpy as np
import random
pygame.init()
clock = pygame.time.Clock()
display = pygame.display.set_mode((1280, 720))
screen = pygame.display.get_surface()
width,height = screen.get_width(), screen.get_height()
pygame.display.set_caption('water cellular automata')
rows = int(height / 8)
columns = int(width / 8)
field = np.zeros((rows, columns), dtype=np.int32)
for a in range(0, int((rows * columns) / 500)):
r1 = random.randint(0, int((rows / 4) * 3))
r2 = random.randint(0, int(columns - 1))
field[r1, r2] = 21
def draw_text(text, pos):
font = pygame.font.SysFont("arial", int(height / rows))
y_pos = pos[1]
x_pos = pos[0]
for line in text.splitlines():
for char in range(1, len(line)+1):
text = font.render(line[:char], 1, (255, 0, 0))
screen.blit(text, (x_pos, y_pos))
def physics():
for y in np.arange(field.shape[0]):
for x in np.arange(field.shape[1]):
value = field[y, x]
u = y - 1
d = y + 1
l = x - 1
r = x + 1
if not value >= 20:
if not y - 1 < 0:
uppervalue = field[u, x]
else:
uppervalue = 20
if not y + 1 > (rows - 1):
undervalue = field[d, x]
else:
undervalue = 20
if not x - 1 < 0:
leftvalue = field[y, l]
else:
leftvalue = 20
if not x + 1 > (columns - 1):
rightvalue = field[y, r]
else:
rightvalue = 20
if value >= 1 and value <= 10:
if undervalue < 10:
field[y, x] -= 1
field[d, x] += 1
elif leftvalue < 10 and rightvalue < 10 and value >= 2:
field[y, x] -= 2
field[y, l] += 1
field[y, r] += 1
elif rightvalue < 10 and value > 1:
field[y, x] -= 1
field[y, r] += 1
elif leftvalue < 10 and value > 1:
field[y, x] -= 1
field[y, l] += 1
quitting = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quitting = True
break
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quitting = True
if event.key == pygame.K_1:
Mouse_x, Mouse_y = pygame.mouse.get_pos()
column_width = width / columns
row_height = height / rows
for anzahl_columns in np.arange(field.shape[1]):
column = column_width * anzahl_columns
nächster_column = column_width * (anzahl_columns + 1)
for anzahl_rows in np.arange(field.shape[0]):
row = row_height * anzahl_rows
nächste_row = row_height * (anzahl_rows + 1)
if Mouse_x > column and Mouse_x <= nächster_column and Mouse_y > row and Mouse_y <= nächste_row:
field[anzahl_rows, anzahl_columns] = 21
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
Mouse_x, Mouse_y = pygame.mouse.get_pos()
column_width = width / columns
row_height = height / rows
for anzahl_columns in np.arange(field.shape[1]):
column = column_width * anzahl_columns
nächster_column = column_width * (anzahl_columns + 1)
for anzahl_rows in np.arange(field.shape[0]):
row = row_height * anzahl_rows
nächste_row = row_height * (anzahl_rows + 1)
if Mouse_x > column and Mouse_x <= nächster_column and Mouse_y > row and Mouse_y <= nächste_row:
if not field[anzahl_rows, anzahl_columns] >= 10:
field[anzahl_rows, anzahl_columns] += 1
if event.button == 2:
Mouse_x, Mouse_y = pygame.mouse.get_pos()
column_width = width / columns
row_height = height / rows
for anzahl_columns in np.arange(field.shape[1]):
column = column_width * anzahl_columns
nächster_column = column_width * (anzahl_columns + 1)
for anzahl_rows in np.arange(field.shape[0]):
row = row_height * anzahl_rows
nächste_row = row_height * (anzahl_rows + 1)
if Mouse_x > column and Mouse_x <= nächster_column and Mouse_y > row and Mouse_y <= nächste_row:
field[anzahl_rows, anzahl_columns] = 20
if event.button == 3:
Mouse_x, Mouse_y = pygame.mouse.get_pos()
column_width = width / columns
row_height = height / rows
for anzahl_columns in np.arange(field.shape[1]):
column = column_width * anzahl_columns
nächster_column = column_width * (anzahl_columns + 1)
for anzahl_rows in np.arange(field.shape[0]):
row = row_height * anzahl_rows
nächste_row = row_height * (anzahl_rows + 1)
if Mouse_x > column and Mouse_x <= nächster_column and Mouse_y > row and Mouse_y <= nächste_row:
field[anzahl_rows, anzahl_columns] = 0
if quitting == True:
break
display.fill((55, 55, 55))
for y in np.arange(field.shape[0]):
for x in np.arange(field.shape[1]):
value = field[y, x]
d = y + 1
if not y + 1 > (rows - 1):
undervalue = field[d, x]
else:
undervalue = 20
if value == 21:
if undervalue < 10:
d = y + 1
field[d, x] += 1
physics()
for y in np.arange(field.shape[0]):
ypos = y * height / field.shape[0]
for x in np.arange(field.shape[1]):
xpos = x * width / field.shape[1]
val = field[y, x]
if field[y, x] > 0 and not field[y, x] > 10:
pygame.draw.rect(display, (0, val / 12, 255 / val ), (xpos, ypos, np.math.ceil(width / field.shape[1]), np.math.ceil(height / field.shape[0])))
#draw_text(str(val), (xpos, ypos))
elif (field[y, x] == 20):
pygame.draw.rect(display, (255, 255, 255), (xpos, ypos, np.math.ceil(width / field.shape[1]), np.math.ceil(height / field.shape[0])))
#draw_text(str(val), (xpos, ypos))
elif (field[y,x] == 21):
pygame.draw.rect(display, (255, 0, 0), (xpos, ypos, np.math.ceil(width / field.shape[1]), np.math.ceil(height / field.shape[0])))
#draw_text(str(val), (xpos, ypos))
pygame.display.update()