SHARE
TWEET

# Tk_2D_Terrain_Gen.py

here2share Oct 22nd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Tk_2D_Terrain_Gen.py -- slow
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. from math import *
  6. import random
  7. import re
  8. import time
  9.  
  10. w = h = 200
  11. vxy = (1533.0)/(w*h)
  12. print vxy
  13.  
  14. sss = '''
  15. 000333000
  16. 003222300
  17. 032111230
  18. 321111123
  19. 321101123
  20. 321111123
  21. 032111230
  22. 003222300
  23. 000333000
  24. '''.strip().split()
  25.  
  26. '''
  27. dots = []
  28. for y in range(len(sss)):
  29.     for x in range(len(sss)):
  30.         if sss[x][y] not in '0':
  31.             a,b = x-4,y-4
  32.             t = abs(a)+abs(b)
  33.             s = '(x+%d, y+%d)' % (a,b)
  34.             s = s.replace('+0','')
  35.             s = s.replace('+-','-')
  36.             dots.append([t,s])
  37. dots.sort()
  38. dots = [z for a,z in dots]
  39. print len(dots)
  40. dots = ', '.join(dots)
  41. '''
  42.  
  43. rainbow=[]
  44. def z(r,g,b):
  45.     rainbow.append((r,g,b))
  46. r,g,b=255,0,0
  47. for g in range(256):
  48.     z(r,g,b)
  49. for r in range(254, -1, -1):
  50.     z(r,g,b)
  51. for b in range(256):
  52.     z(r,g,b)
  53. for g in range(254, -1, -1):
  54.     z(r,g,b)
  55. for r in range(256):
  56.     z(r,g,b)
  57. for b in range(254, -1, -1):
  58.     z(r,g,b)
  59. 0
  60.  
  61. z = rainbow.index((0,255,0))
  62. rainbow = rainbow[z:] + rainbow[:z]
  63.  
  64. xy = []
  65. for y in range(0,h):
  66.     for x in range(0,w):
  67.         xy.append((x,y))
  68. random.shuffle(xy)
  69.  
  70. def climb(x,y):
  71.     z = [(x+1, y), (x, y+1), (x, y-1), (x-1, y), (x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1)]
  72.     random.shuffle(z)
  73.     return z
  74.     '''
  75.     return [(x+1, y), (x, y+1), (x, y-1), (x-1, y), (x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1), (x+2, y), (x, y+2), (x, y-2), (x-2, y), (x+1, y+2), (x+1, y-2), (x+2, y+1), (x+2, y-1), (x+3, y), (x, y+3), (x, y-3), (x-1, y+2), (x-1, y-2), (x-2, y+1), (x-2, y-1), (x-3, y), (x+1, y+3), (x+1, y-3), (x+2, y+2), (x+2, y-2), (x+3, y+1), (x+3, y-1), (x+4, y), (x, y+4), (x, y-4), (x-1, y+3), (x-1, y-3), (x-2, y+2), (x-2, y-2), (x-3, y+1), (x-3, y-1), (x-4, y), (x+1, y+4), (x+1, y-4), (x+2, y+3), (x+2, y-3), (x+3, y+2), (x+3, y-2), (x+4, y+1), (x+4, y-1), (x-1, y+4), (x-1, y-4), (x-2, y+3), (x-2, y-3), (x-3, y+2), (x-3, y-2), (x-4, y+1), (x-4, y-1)][:random.randint(10,56)]
  76.     '''
  77.  
  78. def mv():
  79.     x,y = zzz[0]
  80.     zzz.pop(0)
  81.     ccc = '#%02x%02x%02x' % rainbow[int((len(bbb)/5)*vxy)]
  82.     canvas.create_line((x,y,x+1,y), fill=ccc)
  83.     bbb.append(1)
  84.     ppp = climb(x,y)
  85.     p,ppp = ppp[:2],ppp[2:]
  86.     for z in p:
  87.         try:
  88.             zzz.append(xy.pop(xy.index(z)))
  89.             x,y = z
  90.             canvas.create_line((x,y,x+1,y), fill=ccc)
  91.             bbb.append(1)
  92.         except:
  93.             0
  94.     for z in ppp:
  95.         try:
  96.             aaa.append(xy.pop(xy.index(z)))
  97.         except:
  98.             0
  99. 0
  100.  
  101. root = Tk()
  102. root.title("Tk 2D Terrain Gen")
  103. canvas = Canvas(root, width=w, height=h)
  104. canvas.pack()
  105.  
  106. peaks = 10
  107. next = peaks-1
  108. zzz,xy = xy[:peaks],xy[peaks:]
  109. aaa = []
  110. bbb = []
  111. t = 0
  112. while 1:
  113.     random.shuffle(zzz)
  114.     if zzz:
  115.         mv()
  116.     else:
  117.         z,aaa = aaa[:120],aaa[120:]
  118.         zzz.extend(z)
  119.         if not zzz:
  120.             break
  121.     if t < time.time():
  122.         print len(xy)
  123.         t = time.time()+5
  124.     root.update()
  125. 0
  126. print '***'
  127. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top