tinyevil

Untitled

Feb 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import tkinter as tk
  2. import math
  3. import random
  4.  
  5. root=tk.Tk()
  6. #root.attributes("-fullscreen", True)
  7. root.bind('<Escape>', lambda _ : root.quit())
  8.  
  9. canvas = tk.Canvas(root)
  10. canvas.pack(fill=tk.BOTH, expand=tk.YES)
  11.  
  12. CELL = 24
  13. width = 0
  14. height = 0
  15.  
  16. field = []
  17.  
  18. all_chars = set()
  19. all_streams = set()
  20.  
  21. font = ("Consolas", 24, "bold")
  22.  
  23. colors = []
  24. for i in range(0, 30):
  25. if i < 10:
  26. r = math.floor(255 * (10 - i) / 10)
  27. b = r
  28. g = 255
  29. colors.append("#" + format(r, '02x') + format(g, '02x') + format(b, '02x') )
  30. elif i <= 20:
  31. g = math.floor(255 * (20 - i) / 10)
  32. colors.append("#00" + format(g, '02x') + "00")
  33. else:
  34. colors.append("black")
  35.  
  36. class Char:
  37. def __init__(self, stream, column, row):
  38. self.column = column
  39. self.row = row
  40. self.parent = stream
  41. self.prev = None
  42. self.next = None
  43.  
  44. self.time = 0
  45. self.tick = False
  46.  
  47. self.id = canvas.create_text((CELL * self.column, CELL * self.row),
  48. text=chr(random.randint(0, 26) + ord('a')),
  49. font = font,
  50. fill=colors[0])
  51.  
  52. all_chars.add(self)
  53.  
  54. def upd(self):
  55. self.tick = self.tick + 1
  56. if self.tick < 3:
  57. return
  58. self.tick = 0
  59. self.time = self.time + 1
  60. if self.time >= 20:
  61. self.kill()
  62. else:
  63. canvas.itemconfig(self.id, fill=colors[self.time])
  64. if random.randint(0,20) == 0:
  65. canvas.itemconfig(self.id, text=chr(random.randint(0, 26) + ord('a')))
  66.  
  67. def kill(self):
  68. field[self.column * height + self.row] = None
  69. self.parent.chars.remove(self)
  70. canvas.delete(self.id)
  71. all_chars.remove(self)
  72.  
  73. class Stream:
  74. def __init__(self, column, row):
  75. self.column = column
  76. self.row = row
  77. self.chars = set()
  78. self.ticks = 0
  79. self.grows = random.randint(2, 4)
  80. all_streams.add(self)
  81.  
  82. def upd(self):
  83. self.ticks = self.ticks + 1
  84. if self.ticks < self.grows:
  85. return
  86. self.ticks = 0
  87. self.grow()
  88.  
  89. def kill(self):
  90. all_streams.remove(self)
  91.  
  92. def grow(self):
  93. if self.row >= height:
  94. if len(self.chars) == 0:
  95. self.kill()
  96. return
  97.  
  98. old_char = field[self.column * height + self.row]
  99. if old_char:
  100. old_char.kill()
  101.  
  102. char = Char(self, self.column, self.row)
  103. field[self.column * height + self.row] = char
  104. self.row = self.row + 1
  105. self.chars.add(char)
  106.  
  107.  
  108. def add_stream():
  109. if random.randint(0,3) == 0:
  110. column = random.randint(0, width - 1)
  111. row = 0
  112. s = Stream(column, row)
  113.  
  114. def reset(evt):
  115. global width, height, field, all_chars, all_streams
  116.  
  117. width = math.ceil(evt.width / CELL)
  118. height = math.ceil(evt.height / CELL)
  119.  
  120. field = [None] * (width * height)
  121. all_chars = set()
  122. all_streams = set()
  123.  
  124. canvas.delete("all")
  125. canvas.create_rectangle(0, 0, evt.width, evt.height, fill="black")
  126.  
  127. def upd():
  128. for stream in list(all_streams):
  129. stream.upd()
  130. for char in list(all_chars):
  131. char.upd()
  132.  
  133. add_stream()
  134.  
  135. root.after(math.ceil(1000 / 60.0), upd)
  136.  
  137. def main():
  138. canvas.bind("<Configure>", reset)
  139. root.after(math.ceil(1000 / 60.0), upd)
  140. root.mainloop()
  141. root.destroy()
  142.  
  143. import cProfile
  144. cProfile.run('main()')
Add Comment
Please, Sign In to add comment