Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_basic_forest_fire_v2.py
- from tkinter import *
- from PIL import Image, ImageTk
- import time
- import random
- rdi = random.randint
- from math import sin, radians
- def deg2sine(deg):
- deg = deg % 360
- return min(255,int(sin(radians(deg))*256))
- e = 180/1000.0
- dot = {i:deg2sine(i*e) for i in range(0, 1001)}
- nx = 120
- ny = 120
- root = Tk()
- root.title("Tk_Basic_Forest_Fire")
- root.geometry("%dx%d+50+50"%(nx,ny))
- canvas = Canvas(root, width=nx, height=ny)
- canvas.place(x=0, y=0)
- def RGB__(rgb): # pass
- return "#%02x%02x%02x" % tuple(int(max(0,min(255,x))) for x in rgb)
- mouseX, mouseY = None, None
- def mouse_click(event):
- global mouseX, mouseY
- mouseX, mouseY = event.x, event.y
- def mouse_release(event):
- global mouseX, mouseY
- mouseX, mouseY = None, None
- def mouse_drag(event):
- global mouseX, mouseY
- if mouseX and mouseY:
- mouseX, mouseY = event.x, event.y
- canvas.bind("<Button-1>", mouse_click)
- canvas.bind("<Button-3>", mouse_release)
- canvas.bind("<B1-Motion>", mouse_drag)
- active = {(int(nx/2),int(ny/2)):500}
- cycle = [(x,y) for x in range(1,nx) for y in range(1,ny)] # for version 2
- random.shuffle(cycle)
- mid = int(len(cycle)*0.7)
- def display():
- canvas.create_rectangle((i,j,i+1,j+1), fill=RGB__((ccc,0,0)), outline='')
- while 1: # infinite loop
- canvas.delete('all')
- # mouse click
- if mouseX and mouseY:
- x = min(max(1, mouseX), nx-1)
- y = min(max(1, mouseY), ny-1)
- active[(x,y)] = 500
- cycle.remove((x,y))
- cycle.append((x,y))
- # fire spread
- active_loop = list(active)
- for i,j in active_loop:
- # affect self
- try:
- active[(i,j)] += (10+rdi(0,7))
- ccc = dot[active[(i,j)]]
- display()
- except:
- del active[(i,j)]
- continue
- # affect neighbors
- for x,y in ((i+1,j+1),(i-1,j-1),(i-1,j+1),(i+1,j-1)):
- if (0 < x < nx) and (0 < y < ny):
- try:
- active[(x,y)] += (1+rdi(0,3))
- except:
- if len(active) < 1600:
- if (x,y) in cycle[:mid]:
- cycle.remove((x,y))
- cycle.append((x,y))
- active[(x,y)] = (1+rdi(0,1))
- root.update()
- time.sleep(0.0001)
Add Comment
Please, Sign In to add comment