Advertisement
here2share

# Tk_2D_Terrain_Gen.py

Oct 22nd, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. # Tk_2D_Terrain_Gen.py -- too slow
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import random
  6. import time
  7.  
  8. w = h = 200
  9.  
  10. pp = [(x,y) for x in xrange(w) for y in xrange(h)]
  11. random.shuffle(pp)
  12.  
  13. __xy = [z%254 for z in xrange(w*h)]
  14. __xy.sort()
  15. v = __xy.index(20)
  16. xy = []
  17. for z in xrange(0,w*h,v):
  18.     t = __xy[z-v:z]
  19.     v = max(0,v-5)
  20.     random.shuffle(t)
  21.     xy.extend(t)
  22.  
  23. def climb(x,y):
  24.     t = x%2 # to double the speed
  25.     if t:
  26.         z = [(x+1, y), (x, y+1), (x, y-1), (x-1, y)]
  27.     else:
  28.         z = [(x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1)]
  29.     random.shuffle(z)
  30.     return z
  31.  
  32. root = Tk()
  33. root.title("Tk 2D Terrain Gen")
  34. canvas = Canvas(root, width=w, height=h, bg='black')
  35. canvas.pack()
  36.  
  37. peaks = 10
  38. rnd_peaks = [0 for z in range(1,w*h)]+range(1,10)+[1]
  39. random.shuffle(rnd_peaks)
  40. zzz,pp = pp[:peaks],pp[peaks:]
  41. aaa = zzz[:]
  42. t = 0
  43. p = 2
  44. print v
  45. while 1:
  46.     if aaa:
  47.         x,y = aaa.pop(0)
  48.         try:
  49.             ccc = '#%02x%02x%02x' % tuple([xy.pop()]*3)
  50.         except:
  51.             break
  52.         canvas.create_line((x,y,x+1,y), fill=ccc)
  53.         ppp = climb(x,y)
  54.        
  55.         for z in ppp[:p]:
  56.             try:
  57.                 aaa.append(pp.pop(pp.index(z)))
  58.             except:
  59.                 0
  60.         for z in ppp[p:]:
  61.             try:
  62.                 zzz.append(pp.pop(pp.index(z)))
  63.             except:
  64.                 0
  65.         if rnd_peaks.pop():
  66.             aaa = [pp.pop()] + aaa
  67.     else:
  68.         aaa,zzz = zzz[:400],zzz[400:]
  69.         random.shuffle(aaa)
  70.     if t < time.time():
  71.         print len(pp)
  72.         t = time.time()+5
  73.         if len(pp) < w*h*0.5:
  74.             p = 1
  75.     root.update()
  76. 0
  77. print '***'
  78. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement