terrain_gen.py

Created by squarepoint

Created on September 25, 2023

2.04 KB

A nice 3d ish terrain generator that uses my fake perlin algorithm (it’s actually called blur noise iirc). Move with arrows, press ok to generate a new map, zoom with + and -, you can also raise or lower the water level but i forgor the keys for it. Yeah the height isn’t synced w/ the zoom but i’m too lazy to rewrite the whole thing, have fun :D


from ion import keydown as kdn
from math import floor
from kandinsky import fill_rect as rct
from random import randint as r
def gen(n):
  global m
  for i in range(400):m.append(r(0,255))
  for j in range(n):
    m2=[]
    for i in range(400):
      a=[]
      a.append(m[i])
      if i%20>0:a.append(m[i-1])
      if i%20<19:a.append(m[i+1])
      if floor(i/20)>0:a.append(m[i-20])
      if floor(i/20)<19:a.append(m[i+20])
      e=sum(a)
      m2.append(int(e/len(a)))
    m=m2
def render(ms,mx,my,w):
  rct(0,0,320,222,(255,255,255))
  global m
  for i in range(400):
    x=i%20
    y=floor(i/20)
    s=ms
    g=((i-y%2)%2)
    if m[i]<w+10:
      gr=(200,200-20*g,150)
      b1=(170,170-20*g,130)
      b2=(185,185-20*g,140)
    else:
      gr=(0,200-20*g,100)
      b1=(120-10*g,70,70)
      b2=(130-10*g,80,80)
    for j in range(0,s):
      rct(x*s-y*s+mx+j,my+y*s+x*s-j-m[i],1,2*j,gr)
      rct(x*s-y*s+mx+j,my+y*s+x*s+j-m[i],1,20,b1)
      rct(x*s-y*s+mx+j,my+20+y*s+x*s+j-m[i],1,m[i]-30,(120-10*g,120-10*g,120-10*g))
      if m[i]<w:
        rct(x*s-y*s+mx+j,y*s+x*s-j-w+my,1,2*j+w-m[i],(0,100,200-20*g))
        rct(x*s-y*s+mx+j,my+y*s+x*s-j-w,1,2*j,(0,140,240-20*g))
    for j in range(s,0,-1):
      rct(x*s-y*s+mx+2*s-j,my+y*s+x*s-j-m[i],1,2*j,gr)
      rct(x*s-y*s+mx+2*s-j,my+y*s+x*s+j-m[i],1,20,b2)
      rct(x*s-y*s+mx+2*s-j,my+20+y*s+x*s+j-m[i],1,m[i]-30,(130-10*g,130-10*g,130-10*g))
      if m[i]<w:
        rct(x*s-y*s+mx+2*s-j,y*s+x*s-j-w+my,1,2*j+w-m[i],(0,110,220-20*g))
        rct(x*s-y*s+mx+2*s-j,my+y*s+x*s-j-w,1,2*j,(0,140,240-20*g))

mx=150
my=100
ms=6
w=110
m=[]
gen(6)
render(ms,mx,my,w)
while True:
  if kdn(45):
    ms+=1
    render(ms,mx,my,w)
  elif kdn(46):
    ms-=1
    render(ms,mx,my,w)
  if kdn(3):
    mx-=50
    render(ms,mx,my,w)
  elif kdn(0):
    mx+=50
    render(ms,mx,my,w)
  if kdn(1):
    my+=50
    render(ms,mx,my,w)
  elif kdn(2):
    my-=50
    render(ms,mx,my,w)
  if kdn(39):
    w+=10
    render(ms,mx,my,w)
  elif kdn(40):
    w-=10
    render(ms,mx,my,w)
  if kdn(4):
    m=[]
    gen(6)
    render(ms,mx,my,w)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.