gptperlin.py

Created by alexandre-merle

Created on April 25, 2026

1.65 KB


from math import sin
from kandinsky import fill_rect

# -------- ECRAN --------
W, H = 64, 44   # 1 pixel = 1 cellule

Cols = [
(40,40,195),   # eau profonde
(50,50,255),   # eau
(220,220,165), # sable
(80,215,80),   # herbe
(60,180,60),   # forêt
(140,140,140), # montagne
(240,240,240)  # neige
]

# -------- HASH SEED --------
def hash32(n):
    n = (n ^ 61) ^ (n >> 16)
    n = n + (n << 3)
    n = n ^ (n >> 4)
    n = n * 0x27d4eb2d
    n = n ^ (n >> 15)
    return n & 255

# -------- PARAMETRES SINUS --------
def make_params(seed):
    params = []

    for i in range(10):  # plus de détails
        s1 = hash32(seed + i * 11)
        s2 = hash32(seed + i * 23)
        s3 = hash32(seed + i * 37)

        A = 8 + (s1 % 60)
        k = 0.01 + (s2 % 25) * 0.004
        p = s3
        mode = i % 4

        params.append((A, k, p, mode))

    return params

# -------- HAUTEUR --------
def height(x, y, params):
    v = 128

    for A, k, p, m in params:

        if m == 0:
            v += A * sin(k*x + p)
        elif m == 1:
            v += A * sin(k*y - p)
        elif m == 2:
            v += A * sin(k*(x+y) + p)
        else:
            v += A * sin(k*(x-y) - p)

    return v

# -------- BIOMES --------
def t(v):
    if v < 90: return 0
    if v < 110: return 1
    if v < 135: return 2
    if v < 165: return 3
    if v < 195: return 4
    if v < 225: return 5
    return 6

# -------- RENDU PIXEL PAR PIXEL --------
def render(seed):
    params = make_params(seed)

    for y in range(H):
        for x in range(W):

            v = height(x, y, params)
            fill_rect(x*5, y*5, 5, 5, Cols[t(v)])

# -------- EXECUTION --------
render(42)

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.