Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Gradient_Life.py
- import tkinter as tk
- import random
- import math
- ww, hh = 600, 600
- root = tk.Tk()
- root.title("Gradient Life")
- canvas = tk.Canvas(root, width=ww, height=hh, bg="black")
- canvas.pack()
- CELL_SIZE = 10
- GRID_WIDTH = ww // CELL_SIZE
- GRID_HEIGHT = hh // CELL_SIZE
- SPEED = 1
- r = 255
- g = 255
- b = 0
- rgb = []
- idx = {}
- for i in range(200):
- r -= 1
- g -= 1.2
- b += 1.2
- rgb.append('#{0:02x}{1:02x}{2:02x}'.format(*tuple(int(i) for i in (r, g, b))))
- idx[rgb[-1]] = i
- L = len(rgb)
- DEFAULT_GRID = [[rgb[-1] for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
- grid = rgb * (GRID_WIDTH * GRID_HEIGHT // L)
- grid = grid[:GRID_WIDTH*GRID_HEIGHT]
- random.shuffle(grid)
- grid = [grid[i:i+GRID_WIDTH] for i in range(0, GRID_WIDTH*GRID_HEIGHT, GRID_HEIGHT)]
- while 1:
- canvas.delete("all")
- new_grid = DEFAULT_GRID[:]
- for y in range(GRID_HEIGHT):
- for x in range(GRID_WIDTH):
- i = idx[grid[y][x]]
- if i < 100:
- for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
- ny, nx = (y + dy) % GRID_HEIGHT, (x + dx) % GRID_WIDTH
- if idx[grid[ny][nx]] > L-2:
- new_grid[ny][nx] = rgb[0]
- break
- if i < L-1:
- new_grid[y][x] = rgb[i+1]
- color = new_grid[y][x]
- canvas.create_rectangle(
- x * CELL_SIZE, y * CELL_SIZE,
- (x + 1) * CELL_SIZE, (y + 1) * CELL_SIZE,
- fill=color, outline=color
- )
- grid = new_grid
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement