5
5
6
6
class Simulation :
7
7
def __init__ (self , width , height , cell_size ):
8
- self .cell_size = cell_size
9
8
self .grid = Grid (width , height , cell_size )
9
+ self .cell_size = cell_size
10
10
self .mode = "sand"
11
11
self .brush_size = 3
12
12
@@ -15,66 +15,63 @@ def draw(self, window):
15
15
self .draw_brush (window )
16
16
17
17
def add_particle (self , row , column ):
18
- if self .mode == "sand" :
19
- if random .random () < 0.2 :
20
- particle = SandParticle
21
- self .grid .add_particle (row , column , particle )
22
- elif self .mode == "rock" :
23
- particle = RockParticle
24
- self .grid .add_particle (row , column , particle )
18
+ if self .mode == "sand" :
19
+ if random .random () < 0.15 :
20
+ self .grid .add_particle (row , column , SandParticle )
21
+ elif self .mode == "rock" :
22
+ self .grid .add_particle (row , column , RockParticle )
23
+
24
+ def remove_particle (self , row , column ):
25
+ self .grid .remove_particle (row , column )
25
26
26
27
def update (self ):
27
- orientation = random .randint (0 , 1 )
28
- for row in range (self .grid .rows - 1 , - 1 , - 1 ): # Start from the bottom
29
- if orientation == 0 :
30
- for column in range (self .grid .columns - 1 , - 1 , - 1 ):
31
- self .process_particle (row , column )
28
+ for row in range (self .grid .rows - 2 , - 1 , - 1 ):
29
+ if row % 2 == 0 :
30
+ column_range = range (self .grid .columns )
32
31
else :
33
- for column in range (self .grid .columns ):
34
- self .process_particle (row , column )
32
+ column_range = reversed (range (self .grid .columns ))
35
33
36
- def process_particle ( self , row , column ) :
37
- particle = self .grid .get_cell (row , column )
38
- if isinstance (particle , SandParticle ):
39
- new_pos = particle .update (self .grid , row , column )
40
- if new_pos != (row , column ):
41
- self .grid .set_cell (new_pos [0 ], new_pos [1 ], particle )
42
- self .grid .set_cell (row , column , None )
34
+ for column in column_range :
35
+ particle = self .grid .get_cell (row , column )
36
+ if isinstance (particle , SandParticle ):
37
+ new_pos = particle .update (self .grid , row , column )
38
+ if new_pos != (row , column ):
39
+ self .grid .set_cell (new_pos [0 ], new_pos [1 ], particle )
40
+ self .grid .remove_particle (row , column )
43
41
44
42
def restart (self ):
45
43
self .grid .clear ()
46
44
47
- def remove_particle (self , row , column ):
48
- self .grid .remove_particle (row , column )
49
-
50
45
def handle_controls (self ):
51
46
for event in pygame .event .get ():
52
47
if event .type == pygame .QUIT :
53
48
pygame .quit ()
54
49
sys .exit ()
55
-
56
50
if event .type == pygame .KEYDOWN :
57
51
self .handle_key (event )
58
-
52
+
59
53
self .handle_mouse ()
60
54
61
55
def handle_key (self , event ):
62
- if event .type == pygame .KEYDOWN :
63
- if event .key == pygame .K_SPACE :
64
- self .restart ()
65
- elif event .key == pygame .K_s :
66
- self .mode = "sand"
67
- elif event .key == pygame .K_r :
68
- self .mode = "rock"
69
- elif event .key == pygame .K_e :
70
- self .mode = "erase"
56
+ if event .key == pygame .K_SPACE :
57
+ self .restart ()
58
+ elif event .key == pygame .K_s :
59
+ print ("Sand Mode" )
60
+ self .mode = "sand"
61
+ elif event .key == pygame .K_r :
62
+ print ("Rock Mode" )
63
+ self .mode = "rock"
64
+ elif event .key == pygame .K_e :
65
+ print ("Eraser Mode" )
66
+ self .mode = "erase"
71
67
72
68
def handle_mouse (self ):
73
69
buttons = pygame .mouse .get_pressed ()
74
70
if buttons [0 ]:
75
71
pos = pygame .mouse .get_pos ()
76
72
row = pos [1 ] // self .cell_size
77
73
column = pos [0 ] // self .cell_size
74
+
78
75
self .apply_brush (row , column )
79
76
80
77
def apply_brush (self , row , column ):
@@ -89,23 +86,18 @@ def apply_brush(self, row, column):
89
86
self .add_particle (current_row , current_col )
90
87
91
88
def draw_brush (self , window ):
92
-
93
89
mouse_pos = pygame .mouse .get_pos ()
94
90
column = mouse_pos [0 ] // self .cell_size
95
91
row = mouse_pos [1 ] // self .cell_size
96
-
97
- cursor_size = self .brush_size * self .cell_size
92
+
93
+ brush_visual_size = self .brush_size * self .cell_size
98
94
color = (255 , 255 , 255 )
99
95
100
96
if self .mode == "rock" :
101
- color = (100 , 100 , 100 )
97
+ color = (100 , 100 , 100 )
102
98
elif self .mode == "sand" :
103
- color = (185 , 142 , 66 )
99
+ color = (185 , 142 , 66 )
104
100
elif self .mode == "erase" :
105
- color = (255 , 105 , 180 )
101
+ color = (255 , 105 , 180 )
106
102
107
- pygame .draw .rect (
108
- window ,
109
- color ,
110
- (column * self .cell_size , row * self .cell_size , cursor_size , cursor_size ),
111
- )
103
+ pygame .draw .rect (window , color , (column * self .cell_size , row * self .cell_size , brush_visual_size , brush_visual_size ))
0 commit comments