Python: Set Fire on Digital Forest Simulation

Forest whose trees are constantly growing and being burn down. On each step, there is 1 percent chance of a blank space grows into tree and 1 percent chance of a tree struck by lightning and burns. Fires burn down adjacent trees so a densely packed forest will be having large fire than sparsely packed one.

Fire in Forest Code

forest fire cover
forest fire cover
  1. Define the variables WIDTH, HEIGHT, TREE, FIRE and EMPTY
  2. Define constant INITIAL_TREE_DENSITY, GROW_CHANCE, FIRE_CHANCE and PAUSE_LENGTH
  3. Create a displayForest function
    1. Set the display to 0,0
    2. Get the forest dictionary height and width range
      1. When TREE is detected in forest x,y array, set the canvas foreground color to green
      2. When FIRE is detected in forest x,y array, set the canvas foreground color to red.
      3. When EMPTY is detected in forest x,y array, print space
forest code1
forest code1
  1. Create a createNewforest function
  2. Set forest dictionary with width and height from constant WIDTH and HEIGHT.
    1. Get each width and height value from 0,0 to the 79,22
    2. Get random *100 match with INITIAL TREE DENSITY (0.2)
      1. If yes, create TREE into forest x,y dictionary
      2. If no, set to EMPTY.
forest code2
forest code2
  1. Add the main function to call createNewForest function.
  2. Clear canvas and display the forest dictionary value by calling displayForest
  3. Set the nextforest dictionary value copy from forest dictionary.
  4. Again go through the canvas WIDTH and HEIGHT
    1. If forest x,y dictionary is EMPTY and random chance is 0.01 – GROW CHANCE then set the nextforest x,y to TREE
    2. If forest x,y dictionary is TREE and random chance is 0.01 -FIRE CHANCE then set the nextforest x,y to FIRE
    3. If forest x,y dictionary is FIRE and check nearby forest x,y is TREE having FIRE.
      1. If YES, then nextforest nearby (x,y) change to FIRE. And TREE burn down set to EMPTY.
    4. Finally, copy nextforest from forest dictionary.

forest code3
forest code3
forest fire demo
forest fire demo

Here is the link to the code. In summary, NIcky Case forest simulation is ideal for our setup to follow.

Thanks for reading and good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :