Skip to content

Instantly share code, notes, and snippets.

@Psycojoker
Last active March 13, 2018 23:17
Show Gist options
  • Save Psycojoker/2ff58e386f0148801ed777d0744cf2d3 to your computer and use it in GitHub Desktop.
Save Psycojoker/2ff58e386f0148801ed777d0744cf2d3 to your computer and use it in GitHub Desktop.
import os
import sys
import shutil
import noise
import moviepy
from random import randint
from PIL import Image
from moviepy.editor import ImageClip, ImageSequenceClip, CompositeVideoClip
width = 500
height = width
# base = randint(0, 100000)
base = 0
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((base + i) / freq, (base + 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"]
first_pixel_written_down = False
def add_level(x, y, color):
global a, previous_percent, images, first_pixel_written_down
for level in range(x, y):
have_written_this_loop = False
for i in range(0, width):
for j in range(0, height):
a += 1
percent = int((float(a) / total) * 100)
if percent != previous_percent:
sys.stdout.write("%s%%\r" % (percent))
sys.stdout.flush()
previous_percent = percent
number = values[i][j]
if number != level:
continue
# color = (number, number, number)
have_written_this_loop = True
if x <= number < y:
first_pixel_written_down = True
pixels[(j, i)] = color
# if (a % fps) == 0:
if first_pixel_written_down and have_written_this_loop:
image.save("map/%s.png" % a)
images.append("map/%s.png" % a)
a = 1
previous_percent = 0
total = width * height * 8 * 255
frames = 1000
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)
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