Advertisement
here2share

# Tk_no_ones_sq_spiral.py

Feb 26th, 2022
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. # Tk_no_ones_sq_spiral.py
  2.  
  3. from Tkinter import *
  4. from math import sqrt
  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_no_ones_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 isAlive(num):
  20.     for a in RandomAlive:
  21.         if not num%a:
  22.             return RandomAlive.index(a)
  23.     return (color + 7) % 12
  24.  
  25.  
  26. def switch(state):
  27.     return {
  28.         0 : (x + stepSize, y),
  29.         1 : (x, y - stepSize),
  30.         2 : (x - stepSize, y),
  31.         3 : (x, y + stepSize),
  32.     }[state]
  33.    
  34.    
  35. # set up spiral
  36. step = 1
  37. state = 0
  38. numSteps = 1
  39. turnCounter = 1
  40. stepSize = 5
  41. cols = ww / stepSize
  42. rows = hh / stepSize
  43. totalSteps = cols * rows
  44. x = ww / 2
  45. y = hh / 2
  46.  
  47. xy = []
  48. while 1:
  49.     xy.append((x,y))
  50.    
  51.     # Change state
  52.     if step % numSteps == 0:
  53.         state = (state + 1) % 4
  54.         turnCounter += 1
  55.         if turnCounter % 2 == 0:
  56.             numSteps += 1
  57.     step += 1
  58.     x, y = switch(state)
  59.  
  60.     # Are we done?
  61.     if step > totalSteps:
  62.         break
  63.  
  64. COLORS = 'red orange yellow green blue purple'.split()
  65. t = stepSize
  66. i = 12345
  67. iii = 1
  68. s = ''
  69. Lc = len(COLORS)
  70. while 1:
  71.     canvas.delete('all')
  72.     for x,y in xy:
  73.         if len(s) < 100:
  74.             s += str(i).replace('1','')
  75.             i += 1
  76.         iii = int(s[:3])
  77.         s = s[3:]
  78.         color = int(iii)%Lc
  79.         canvas.create_rectangle((x,y,x+t,y+t), fill=COLORS[color], outline='')
  80.     canvas.update()
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement