Advertisement
here2share

# Tk_rainbow_sq_spiral.py

Mar 18th, 2022
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. # Tk_rainbow_sq_spiral.py
  2.  
  3. from Tkinter import *
  4. from math import sqrt, atan2
  5. from random import shuffle as rs
  6.  
  7. def oRGB(rgb): # pass
  8.     r,g,b = rgb
  9.     return "#%02x%02x%02x" % (r,g,b)
  10.  
  11. ww = 600
  12. hh = 600
  13. root = Tk()
  14. root.title("# Tk_rainbow_sq_spiral")
  15. root.geometry("%dx%d+0+0"%(ww,hh))
  16. canvas = Canvas(root, width=ww, height=hh)
  17. canvas.grid()
  18.  
  19. def switch(state):
  20.     return {
  21.         0 : (x + stepSize, y),
  22.         1 : (x, y - stepSize),
  23.         2 : (x - stepSize, y),
  24.         3 : (x, y + stepSize),
  25.     }[state]
  26.    
  27. # set up spiral
  28. step = 0
  29. state = 0
  30. numSteps = 1
  31. turnCounter = 1
  32. stepSize = 5
  33. cols = ww / stepSize
  34. rows = hh / stepSize
  35. totalSteps = cols * rows
  36. x = ww / 2
  37. y = hh / 2
  38.  
  39. xy = []
  40. while 1:
  41.     xy.append((x,y))
  42.    
  43.     # Change state
  44.     if step % numSteps == 0:
  45.         state = (state + 1) % 4
  46.         turnCounter += 1
  47.         if turnCounter % 2 == 0:
  48.             numSteps += 1
  49.     step += 1
  50.     x, y = switch(state)
  51.  
  52.     # Are we done?
  53.     if step > totalSteps:
  54.         break
  55.  
  56. COLORS = '''
  57. red
  58. orange
  59. yellow
  60. green
  61. blue
  62. purple
  63. '''.strip().splitlines()
  64. t = stepSize
  65. Lc = len(COLORS)
  66.  
  67. color = -1
  68. loop = 0
  69. for x,y in xy:
  70.     if not loop:
  71.         color += 1
  72.         loop = color+1
  73.     canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[int(color/3+(loop/3))%6], outline='')
  74.     canvas.update()
  75.     loop -= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement