Created
March 21, 2020 22:18
-
-
Save greg76/4132b5dd4c0bd6416d1411f0daa0ee11 to your computer and use it in GitHub Desktop.
terrain generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
size = (40, 25) | |
field_types = "🏔⛰🌲🌳🌴🏜🌊" | |
def voronoi_map(): | |
no_seeds = 20 | |
seeds = [ | |
{ | |
"type": random.choice(field_types), | |
"loc": ( | |
random.randint(0, size[0]), | |
random.randint(0, size[1]) | |
) | |
} | |
for _ in range(no_seeds) | |
] | |
def euclidean_dist(a, b): | |
d1 = a[0]-b[0] | |
d2 = a[1]-b[1] | |
return math.sqrt(d1**2 + d2**2) | |
def manhattan_dist(a, b): | |
d1 = a[0]-b[0] | |
d2 = a[1]-b[1] | |
return abs(d1) + abs(d2) | |
def nearest(loc): | |
d = None | |
field = field_types[0] | |
for seed in seeds: | |
nd = manhattan_dist(loc, seed["loc"]) | |
if d is None or nd < d: | |
field = seed["type"] | |
d = nd | |
return field | |
map = [] | |
for y in range(size[1]): | |
map_line = [] | |
for x in range(size[0]): | |
new_field = nearest((x,y)) | |
map_line.append(new_field) | |
map.append(map_line) | |
return map | |
def main(): | |
map = voronoi_map() | |
for line in map: | |
for field in line: | |
print(f"{field} ", end="") | |
print() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment