Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Langtons_Ant_Color_Gradient.py
- import tkinter as tk
- WW, HH = 600, 600
- root = tk.Tk()
- root.geometry(f"{WW}x{HH}+0+0")
- canvas = tk.Canvas(root, width=WW, height=HH, bg="white")
- canvas.pack()
- GRID_SIZE = 10
- ROWS, COLS = HH // GRID_SIZE, WW // GRID_SIZE
- grid = [[0] * COLS for _ in range(ROWS)]
- ant_x, ant_y = ROWS // 2, COLS // 2
- ant_direction = 0 # 0: up, 1: right, 2: down, 3: left
- painting = {}
- r, g, b = 128, 0, 0
- color_step = 15
- while True:
- grid[ant_x][ant_y] ^= 1
- try:
- r0, g0, b0 = painting[ant_x, ant_y]
- except:
- r0, g0, b0 = 128, 0, 0
- if ant_direction == 0:
- r = (r + color_step) % 512
- elif ant_direction == 1:
- g = (g + color_step) % 512
- elif ant_direction == 2:
- b = (b + color_step) % 512
- elif ant_direction == 3:
- r = (r + color_step) % 512
- g = (g + color_step) % 512
- b = (b + color_step) % 512
- painting[ant_x, ant_y] = r, g, b
- rx, gx, bx = [255 - abs(255 - i) for i in (r, g, b)]
- color = f"#{rx:02x}{gx:02x}{bx:02x}"
- r, g, b = r0, g0, b0
- canvas.create_rectangle(ant_x * GRID_SIZE, ant_y * GRID_SIZE,
- (ant_x + 1) * GRID_SIZE, (ant_y + 1) * GRID_SIZE,
- fill=color, outline="")
- if grid[ant_x][ant_y]: # White -> Right turn
- ant_direction = (ant_direction + 1) % 4
- else: # Black -> Left turn
- ant_direction = (ant_direction - 1) % 4
- if ant_direction == 0:
- ant_x -= 1
- elif ant_direction == 1:
- ant_y += 1
- elif ant_direction == 2:
- ant_x += 1
- elif ant_direction == 3:
- ant_y -= 1
- ant_x %= ROWS
- ant_y %= COLS
- root.update()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement