fire_simulator.py

Created by archange

Created on August 02, 2024

1.83 KB

Welcome ! In this project, the forest is represented by a matrix of dimension N, and contains forest plots in different states: alive, on fire or burnt. You can change many of the parameters, including the probability of burning (wind strength), the colors of the 3 possible states, the size of the plots and their spacing, the position of the matrix…

The link to the github project: https://github.com/Archange-py/Fire_Simulation


from kandinsky import fill_rect
from random import random
from time import sleep

class Case:
    def __init__(self, x: int, y: int, value: int = 1): self.x,self.y,self.v = x,y,value
    def __repr__(self) -> str: return str(self.v)
    def isfire(self) -> bool: return self.v == 0

class Matrice:
    def __init__(self, x: int = 110, y: int = 60, N: int = 25, p: float = 0.5, t: float = 0.01, width: int = 3, pas: int = 1, colors: dict = {"1":(0,255,0),"0":(255,0,0),"-1":(85,85,85)}):
        self.x,self.y,self.N,self.p,self.t,self.colors,self.width,self.pas,self.cases = x,y,N,p,t,colors,width,pas,None

    def __getitem__(self, xy: int) -> Case: return self.cases[xy]
    def __iter__(self) -> iter: cases = [] ; _l = [[cases.append(case) for case in line] for line in self.cases] ; del _l ; return iter(cases)
    def fire(self, p: float) -> int: return 1 if random() < p else 0
    def neighbors(self, case: Case) -> bool: return (case.x-1 >= 0 and case.y-1 >= 0 and self[case.x-1][case.y-1].isfire()) or (case.x-1 >= 0 and case.y+1 < self.N and self[case.x-1][case.y+1].isfire()) or (case.x-1 >= 0 and self[case.x-1][case.y].isfire())

    def draw(self):
        for case in self: fill_rect(self.x+case.x*(self.width+self.pas),self.y+case.y*(self.width+self.pas),self.width,self.width,self.colors[str(case.v)])

    def run(self):
        def updates(): self.draw() ; sleep(self.t)
        self.cases = [[Case(x,y) for y in range(self.N)] for x in range(self.N)] ; updates()
        for case in self[0]: case.v = 0 if self.fire(self.p) else 1
        updates()
        for n in range(1,self.N):
            for case in self[n]: case.v = 0 if self.fire(self.p) and self.neighbors(case) else 1
            for line in self[:n]:
                for case in line: case.v = -1 if case.isfire() and self.fire(0.2) else case.v
            updates()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.