here2share

# Tk_basic_forest_fire.py

Jul 5th, 2022 (edited)
255
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_basic_forest_fire_v2.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import time
  6. import random
  7.  
  8. rdi = random.randint
  9.  
  10. from math import sin, radians
  11.  
  12. def deg2sine(deg):
  13.     deg = deg % 360
  14.     return min(255,int(sin(radians(deg))*256))
  15.  
  16. e = 180/1000.0
  17. dot = {i:deg2sine(i*e) for i in range(0, 1001)}
  18.  
  19. nx = 120
  20. ny = 120
  21.  
  22. root = Tk()
  23. root.title("Tk_Basic_Forest_Fire")
  24. root.geometry("%dx%d+50+50"%(nx,ny))
  25.  
  26. canvas = Canvas(root, width=nx, height=ny)
  27. canvas.place(x=0, y=0)
  28.  
  29. def RGB__(rgb): # pass
  30.     return "#%02x%02x%02x" % tuple(int(max(0,min(255,x))) for x in rgb)
  31.  
  32. mouseX, mouseY = None, None
  33. def mouse_click(event):
  34.     global mouseX, mouseY
  35.     mouseX, mouseY = event.x, event.y
  36.  
  37. def mouse_release(event):
  38.     global mouseX, mouseY
  39.     mouseX, mouseY = None, None
  40.  
  41. def mouse_drag(event):
  42.     global mouseX, mouseY
  43.     if mouseX and mouseY:
  44.         mouseX, mouseY = event.x, event.y
  45.  
  46. canvas.bind("<Button-1>", mouse_click)
  47. canvas.bind("<Button-3>", mouse_release)
  48. canvas.bind("<B1-Motion>", mouse_drag)
  49.  
  50. active = {(int(nx/2),int(ny/2)):500}
  51. cycle = [(x,y) for x in range(1,nx) for y in range(1,ny)] # for version 2
  52. random.shuffle(cycle)
  53. mid = int(len(cycle)*0.7)
  54.  
  55. def display():
  56.     canvas.create_rectangle((i,j,i+1,j+1), fill=RGB__((ccc,0,0)), outline='')
  57.  
  58. while 1: # infinite loop
  59.     canvas.delete('all')
  60.    
  61.     # mouse click
  62.     if mouseX and mouseY:
  63.         x = min(max(1, mouseX), nx-1)
  64.         y = min(max(1, mouseY), ny-1)
  65.         active[(x,y)] = 500
  66.         cycle.remove((x,y))
  67.         cycle.append((x,y))
  68.        
  69.     # fire spread
  70.     active_loop = list(active)
  71.     for i,j in active_loop:
  72.         # affect self
  73.         try:
  74.             active[(i,j)] += (10+rdi(0,7))
  75.             ccc = dot[active[(i,j)]]
  76.             display()
  77.         except:
  78.             del active[(i,j)]
  79.             continue
  80.         # affect neighbors     
  81.         for x,y in ((i+1,j+1),(i-1,j-1),(i-1,j+1),(i+1,j-1)):
  82.             if (0 < x < nx) and (0 < y < ny):
  83.                 try:
  84.                     active[(x,y)] += (1+rdi(0,3))
  85.                 except:
  86.                     if len(active) < 1600:
  87.                         if (x,y) in cycle[:mid]:
  88.                             cycle.remove((x,y))
  89.                             cycle.append((x,y))
  90.                             active[(x,y)] = (1+rdi(0,1))
  91.    
  92.     root.update()
  93.     time.sleep(0.0001)
RAW Paste Data Copied