-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (121 loc) · 4.32 KB
/
main.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
import pygame
import random
import os
# Define colors
GREEN = (9, 150, 100)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
BLACK = (0, 0, 0) #lime green
WHITE = (255, 255, 255)
# Define constants
WIDTH = 1920
HEIGHT = 1080
CELL_SIZE = 10
FOREST_SIZE = (WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE)
TREE_DENSITY = 0.6
# Initialize pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption("Forest Fire Simulation")
# Load images
tree_img = pygame.image.load(os.path.join('assets', 'tree.png')).convert_alpha()
tree_fire_img = pygame.image.load(os.path.join('assets', 'tree_fire.png')).convert_alpha()
# Create forest
def create_forest():
forest = [[None for y in range(FOREST_SIZE[1])] for x in range(FOREST_SIZE[0])]
for x in range(FOREST_SIZE[0]):
for y in range(FOREST_SIZE[1]):
if random.random() < TREE_DENSITY:
forest[x][y] = tree_img
return forest
forest = create_forest()
# Define function to simulate fire spread
def spread_fire(x, y):
if x < 0 or x >= FOREST_SIZE[0] or y < 0 or y >= FOREST_SIZE[1]:
return
if forest[x][y] == tree_img and random.random() < FIRE_SPREAD_PROBABILITY:
forest[x][y] = tree_fire_img
spread_fire(x - 1, y)
spread_fire(x + 1, y)
spread_fire(x, y - 1)
spread_fire(x, y + 1)
# Ask user for input
fuel_moisture = float(input("Enter fuel moisture: "))
wind_speed = float(input("Enter wind speed: "))
slope_factor = float(input("Enter slope factor: "))
k = 0.2 # set a constant value for k
# Calculate fire spread probability
FIRE_SPREAD_PROBABILITY = k * (1 - fuel_moisture) * wind_speed * slope_factor
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Simulate fire spread from clicked cell
x, y = pygame.mouse.get_pos()
x //= CELL_SIZE
y //= CELL_SIZE
if forest[x][y] == tree_img:
forest[x][y] = tree_fire_img
spread_fire(x, y)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
forest = create_forest()
FIRE_SPREAD_PROBABILITY = k * (1 - fuel_moisture) * wind_speed * slope_factor
# Draw forest
def draw_forest():
for x in range(FOREST_SIZE[0]):
for y in range(FOREST_SIZE[1]):
if forest[x][y] == tree_img:
img = tree_img
elif forest[x][y] == tree_fire_img:
img = tree_fire_img
spread_fire(x - 1, y)
spread_fire(x + 1, y)
spread_fire(x, y - 1)
spread_fire(x, y + 1)
else:
continue
screen.blit(img, (x * CELL_SIZE, y * CELL_SIZE))
# Main game loop
reset = False
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Simulate fire spread from clicked cell
x, y = pygame.mouse.get_pos()
x //= CELL_SIZE
y //= CELL_SIZE
if forest[x][y] == tree_img:
forest[x][y] = tree_fire_img
spread_fire(x, y)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
# Reset the simulation and generate a new forest
forest = [[None for y in range(FOREST_SIZE[1])] for x in range(FOREST_SIZE[0])]
for x in range(FOREST_SIZE[0]):
for y in range(FOREST_SIZE[1]):
if random.random() < TREE_DENSITY:
forest[x][y] = tree_img
reset = True
# Reset the simulation if Q is pressed
if reset:
reset = False
continue
# Draw forest
screen.fill(BLACK)
draw_forest()
# Update display
pygame.display.flip()
# Limit frame rate
clock.tick(30)
# Clean up
pygame.quit()