Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import math
- import random
- root=tk.Tk()
- #root.attributes("-fullscreen", True)
- root.bind('<Escape>', lambda _ : root.quit())
- canvas = tk.Canvas(root)
- canvas.pack(fill=tk.BOTH, expand=tk.YES)
- CELL = 24
- width = 0
- height = 0
- field = []
- all_chars = set()
- all_streams = set()
- font = ("Consolas", 24, "bold")
- colors = []
- for i in range(0, 30):
- if i < 10:
- r = math.floor(255 * (10 - i) / 10)
- b = r
- g = 255
- colors.append("#" + format(r, '02x') + format(g, '02x') + format(b, '02x') )
- elif i <= 20:
- g = math.floor(255 * (20 - i) / 10)
- colors.append("#00" + format(g, '02x') + "00")
- else:
- colors.append("black")
- class Char:
- def __init__(self, stream, column, row):
- self.column = column
- self.row = row
- self.parent = stream
- self.prev = None
- self.next = None
- self.time = 0
- self.tick = False
- self.id = canvas.create_text((CELL * self.column, CELL * self.row),
- text=chr(random.randint(0, 26) + ord('a')),
- font = font,
- fill=colors[0])
- all_chars.add(self)
- def upd(self):
- self.tick = self.tick + 1
- if self.tick < 3:
- return
- self.tick = 0
- self.time = self.time + 1
- if self.time >= 20:
- self.kill()
- else:
- canvas.itemconfig(self.id, fill=colors[self.time])
- if random.randint(0,20) == 0:
- canvas.itemconfig(self.id, text=chr(random.randint(0, 26) + ord('a')))
- def kill(self):
- field[self.column * height + self.row] = None
- self.parent.chars.remove(self)
- canvas.delete(self.id)
- all_chars.remove(self)
- class Stream:
- def __init__(self, column, row):
- self.column = column
- self.row = row
- self.chars = set()
- self.ticks = 0
- self.grows = random.randint(2, 4)
- all_streams.add(self)
- def upd(self):
- self.ticks = self.ticks + 1
- if self.ticks < self.grows:
- return
- self.ticks = 0
- self.grow()
- def kill(self):
- all_streams.remove(self)
- def grow(self):
- if self.row >= height:
- if len(self.chars) == 0:
- self.kill()
- return
- old_char = field[self.column * height + self.row]
- if old_char:
- old_char.kill()
- char = Char(self, self.column, self.row)
- field[self.column * height + self.row] = char
- self.row = self.row + 1
- self.chars.add(char)
- def add_stream():
- if random.randint(0,3) == 0:
- column = random.randint(0, width - 1)
- row = 0
- s = Stream(column, row)
- def reset(evt):
- global width, height, field, all_chars, all_streams
- width = math.ceil(evt.width / CELL)
- height = math.ceil(evt.height / CELL)
- field = [None] * (width * height)
- all_chars = set()
- all_streams = set()
- canvas.delete("all")
- canvas.create_rectangle(0, 0, evt.width, evt.height, fill="black")
- def upd():
- for stream in list(all_streams):
- stream.upd()
- for char in list(all_chars):
- char.upd()
- add_stream()
- root.after(math.ceil(1000 / 60.0), upd)
- def main():
- canvas.bind("<Configure>", reset)
- root.after(math.ceil(1000 / 60.0), upd)
- root.mainloop()
- root.destroy()
- import cProfile
- cProfile.run('main()')
Add Comment
Please, Sign In to add comment