This repository was archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtk_terrain.py
More file actions
74 lines (61 loc) · 2.49 KB
/
tk_terrain.py
File metadata and controls
74 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import random
import time
import tkinter as tk
from noise import RandomNoise
"""
CANVAS EXAMPLE
"""
def render(smoothing_passes, LAND=False):
p = smoothing_passes
colours = random_noise_obj.smoothNoise2d(smoothing_passes=p)
# Gen hexadeciamal color code if LAND is set. Else, grey scale is used.
for x in range(0, len(colours)):
for y in range(0, len(colours[x])):
col = int(colours[x][y] * 255)
if LAND:
if col in range(0,int(255 * water_amount)): # Water
final = f'#0000{col:02X}'
elif col in range(int(255 * (water_amount)), int(255 * (water_amount+sand_amount))): # Sand
final = f'#{col:02X}{col:02X}00'
elif col in range(int(255 * (water_amount+sand_amount)), int(255 * (water_amount+sand_amount+land_amount))): # Land
final = f'#00{col:02X}00'
elif col in range(int(255 * (water_amount+sand_amount+land_amount)), 255): # Mountain
final = f'#{col:02X}{col:02X}{col:02X}'
else:
final = f'#{col:02X}{col:02X}{col:02X}'
else:
final = f'#{col:02X}{col:02X}{col:02X}'
canvas.create_rectangle(x*sq, y*sq, (x*sq)+sq, (y*sq)+sq,
fill=final, width=0, tags=(f'RCT{p}'))
root.update()
canvas.delete(f'RCT{p-2}')
# Tk code
root = tk.Tk()
w = int(root.winfo_screenwidth() // 1.5)
h = int(root.winfo_screenheight() // 1.5)
sq = 10
FPS = 60
color = True
smoothing_passes = 30
random_noise_obj = RandomNoise(w//sq, h//sq, 255, smoothing_passes)
random_noise_obj.randomize()
canvas = tk.Canvas(root, height=h, width=w, bg='black', highlightthickness=0)
canvas.pack()
smoothingl = tk.Label(root, text=f'Size: {w//sq}x{h//sq} | Smoothing pass: 0/{smoothing_passes}')
smoothingl.pack()
# Fraction of 1.0
# These values determine the amount of each element in the render
water_amount = 0.46
sand_amount = 0.007
land_amount = 0.44
# DEBUG
print('water_range', 0,int(255*water_amount))
print('sand_range', int(255*(water_amount)), int(255*(water_amount+sand_amount)))
print('land_range', int(255*(water_amount+sand_amount)), int(255*(water_amount+sand_amount+land_amount)))
print('mountain_range', int(255*(water_amount+sand_amount+land_amount)), 255)
step = 1
for p in range(0, smoothing_passes, 1):
render(p, color)
smoothingl.config(text=f'Size: {w//sq}x{h//sq} | Smoothing pass: {p+1}/{smoothing_passes}')
time.sleep(1/FPS)
root.mainloop()