Skip to content

Commit 5652c4f

Browse files
committed
video code
1 parent 42e7358 commit 5652c4f

File tree

4 files changed

+84
-92
lines changed

4 files changed

+84
-92
lines changed

grid.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
import pygame
2-
from particle import SandParticle
3-
from particle import RockParticle
42

53
class Grid:
64
def __init__(self, width, height, cell_size):
75
self.rows = height // cell_size
86
self.columns = width // cell_size
97
self.cell_size = cell_size
108
self.cells = [[None for _ in range(self.columns)] for _ in range(self.rows)]
11-
9+
1210
def draw(self, window):
13-
for row in range(self.rows):
11+
for row in range(self.rows):
1412
for column in range(self.columns):
1513
particle = self.cells[row][column]
1614
if particle is not None:
1715
color = particle.color
18-
pygame.draw.rect(window, color, (column * self.cell_size, row * self.cell_size, self.cell_size, self.cell_size))
16+
pygame.draw.rect(window, color,
17+
(column * self.cell_size, row * self.cell_size, self.cell_size, self.cell_size))
1918

2019
def add_particle(self, row, column, particle_type):
2120
if 0 <= row < self.rows and 0 <= column < self.columns and self.is_cell_empty(row, column):
22-
self.cells[row][column] = particle_type()
21+
self.cells[row][column] = particle_type()
2322

24-
def set_cell(self, row, column, particle):
23+
def remove_particle(self, row, column):
2524
if 0 <= row < self.rows and 0 <= column < self.columns:
26-
self.cells[row][column] = particle
25+
self.cells[row][column] = None
2726

2827
def is_cell_empty(self, row, column):
2928
if 0 <= row < self.rows and 0 <= column < self.columns:
3029
if self.cells[row][column] is None:
3130
return True
3231
return False
3332

34-
def clear(self):
35-
for row in range(self.rows):
36-
for column in range(self.columns):
37-
self.set_cell(row, column, None)
33+
def set_cell(self, row, column, particle):
34+
if not(0 <= row < self.rows and 0 <= column < self.columns):
35+
return
36+
self.cells[row][column] = particle
3837

39-
def remove_particle(self, row, column):
38+
def get_cell(self, row, column):
4039
if 0 <= row < self.rows and 0 <= column < self.columns:
41-
self.cells[row][column] = None
40+
return self.cells[row][column]
41+
return None
4242

43-
def get_cell(self, row, column):
44-
if 0 <= row < self.rows and 0 <= column < self.columns:
45-
return self.cells[row][column] # Return the cell content
46-
return None
47-
43+
def clear(self):
44+
for row in range(self.rows):
45+
for column in range(self.columns):
46+
self.remove_particle(row, column)

main.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import pygame, sys
1+
import pygame
22
from simulation import Simulation
3-
from particle import SandParticle
43

5-
GREY = (29, 29, 29)
4+
pygame.init()
5+
pygame.mouse.set_visible(False)
6+
67
WINDOW_WIDTH = 800
78
WINDOW_HEIGHT = 600
89
CELL_SIZE = 6
9-
1010
FPS = 120
11-
pygame.init()
12-
pygame.mouse.set_visible(False)
11+
GREY = (29, 29, 29)
1312

1413
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
1514
pygame.display.set_caption("Falling Sand")
1615

1716
clock = pygame.time.Clock()
1817
simulation = Simulation(WINDOW_WIDTH, WINDOW_HEIGHT, CELL_SIZE)
1918

20-
# main loop
19+
#Simulation Loop
2120
while True:
22-
#1. Event Handling
21+
22+
# 1. Event Handling
2323
simulation.handle_controls()
2424

25-
#2. Updating State
25+
# 2. Updating State
2626
simulation.update()
2727

28-
#3. Drawing
28+
# 3. Drawing
2929
window.fill(GREY)
3030
simulation.draw(window)
3131

particle.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
import random, colorsys
1+
import random
2+
import colorsys
23

34
class SandParticle:
45
def __init__(self):
56
self.color = random_color((0.1, 0.12), (0.5, 0.7), (0.7, 0.9))
67

78
def update(self, grid, row, column):
8-
if grid.is_cell_empty(row + 1, column):
9-
return row + 1, column
10-
11-
offsets = [1, -1]
12-
random.shuffle(offsets)
13-
for offset in offsets:
14-
new_column = column + offset
15-
if grid.is_cell_empty(row + 1, new_column): # Check diagonal cells
16-
return row + 1, new_column
9+
if grid.is_cell_empty(row + 1, column):
10+
return row + 1, column
11+
else:
12+
offsets = [-1, 1]
13+
random.shuffle(offsets)
14+
for offset in offsets:
15+
new_column = column + offset
16+
if grid.is_cell_empty(row +1, new_column):
17+
return row + 1, new_column
1718

18-
return row, column
19+
return row, column
1920

2021
class RockParticle:
2122
def __init__(self):
2223
self.color = random_color((0.0, 0.1), (0.1, 0.3), (0.3, 0.5))
2324

2425
def random_color(hue_range, saturation_range, value_range):
25-
hue = random.uniform(*hue_range)
26-
saturation = random.uniform(*saturation_range)
27-
value = random.uniform(*value_range)
28-
r, g, b = colorsys.hsv_to_rgb(hue, saturation, value)
29-
return int(r * 255), int(g * 255), int(b * 255)
26+
hue = random.uniform(*hue_range)
27+
saturation = random.uniform(*saturation_range)
28+
value = random.uniform(*value_range)
29+
r, g, b = colorsys.hsv_to_rgb(hue, saturation, value)
30+
return int(r * 255), int(g * 255), int(b * 255)

simulation.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
class Simulation:
77
def __init__(self, width, height, cell_size):
8-
self.cell_size = cell_size
98
self.grid = Grid(width, height, cell_size)
9+
self.cell_size = cell_size
1010
self.mode = "sand"
1111
self.brush_size = 3
1212

@@ -15,66 +15,63 @@ def draw(self, window):
1515
self.draw_brush(window)
1616

1717
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)
2526

2627
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)
3231
else:
33-
for column in range(self.grid.columns):
34-
self.process_particle(row, column)
32+
column_range = reversed(range(self.grid.columns))
3533

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)
4341

4442
def restart(self):
4543
self.grid.clear()
4644

47-
def remove_particle(self, row, column):
48-
self.grid.remove_particle(row, column)
49-
5045
def handle_controls(self):
5146
for event in pygame.event.get():
5247
if event.type == pygame.QUIT:
5348
pygame.quit()
5449
sys.exit()
55-
5650
if event.type == pygame.KEYDOWN:
5751
self.handle_key(event)
58-
52+
5953
self.handle_mouse()
6054

6155
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"
7167

7268
def handle_mouse(self):
7369
buttons = pygame.mouse.get_pressed()
7470
if buttons[0]:
7571
pos = pygame.mouse.get_pos()
7672
row = pos[1] // self.cell_size
7773
column = pos[0] // self.cell_size
74+
7875
self.apply_brush(row, column)
7976

8077
def apply_brush(self, row, column):
@@ -89,23 +86,18 @@ def apply_brush(self, row, column):
8986
self.add_particle(current_row, current_col)
9087

9188
def draw_brush(self, window):
92-
9389
mouse_pos = pygame.mouse.get_pos()
9490
column = mouse_pos[0] // self.cell_size
9591
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
9894
color = (255, 255, 255)
9995

10096
if self.mode == "rock":
101-
color = (100, 100, 100)
97+
color = (100, 100, 100)
10298
elif self.mode == "sand":
103-
color = (185, 142, 66)
99+
color = (185, 142, 66)
104100
elif self.mode == "erase":
105-
color = (255, 105, 180)
101+
color = (255, 105, 180)
106102

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

Comments
 (0)