Skip to content

Instantly share code, notes, and snippets.

@Psycojoker
Last active March 13, 2018 23:14
Show Gist options
  • Save Psycojoker/a79f8e081646f866224a8f31145dd0b1 to your computer and use it in GitHub Desktop.
Save Psycojoker/a79f8e081646f866224a8f31145dd0b1 to your computer and use it in GitHub Desktop.
import os
import sys
import shutil
import noise
import moviepy
from PIL import Image
from moviepy.editor import ImageClip, ImageSequenceClip, CompositeVideoClip
width = 500
height = 500
values = []
for i in range(height):
values.append([0]*width)
octaves = 10
freq = 16.0 * octaves
image = Image.new("RGBA", (width, height))
pixels = image.load()
for i in range(0, width):
for j in range(0, height):
number = int((noise.snoise2(i / freq, j / freq, octaves=octaves) * 127) + 128)
values[i][j] = number
pixels[(i, j)] = (0, 0, 0)
if os.path.exists("map"):
shutil.rmtree("map")
os.makedirs("map")
image.save("map/0.png")
images = ["map/0.png"]
def add_level(x, y, color):
global a, previous_percent, images
for i in range(0, width):
for j in range(0, height):
number = values[i][j]
# color = (number, number, number)
if x <= number < y:
pixels[(j, i)] = color
if (a % fps) == 0:
image.save("map/%s.png" % a)
images.append("map/%s.png" % a)
a += 1
percent = int((float(a) / total) * 100)
if percent != previous_percent:
sys.stdout.write("%s%%\r" % (percent))
sys.stdout.flush()
previous_percent = percent
a = 1
previous_percent = 0
total = width * height * 8
frames = 4000
fps = 60
add_level(0, 100, (0, 0, 255)) # deep water
add_level(100, 115, (30, 100, 255)) # water
add_level(115, 130, (64, 188, 255)) # coast water
add_level(130, 140, (235, 197, 158)) # beatch
add_level(140, 160, (0, 197, 0)) # grass
add_level(160, 175, (0, 154, 0)) # forest
add_level(175, 190, (172, 108, 43)) # montains
add_level(190, 1000, (229, 254, 299)) # snow
CompositeVideoClip([ImageSequenceClip(images + images[-1:]*(4 * (frames / fps)), fps=(frames / fps))]).write_videofile("map.mp4", fps=30)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment