How to generate perlin noise in pygame?

Question:

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:

enter image description here

But I want something like islands or rivers.

Here’s my code:

#SetUp#
import pygame, sys, random
pygame.init()
win = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Isom')
x = 0
y = 0
s = 0
tilel = list()
random.seed(5843)
MAP = [random.randint(0, 1) for _ in range(192)]

#Tiles#
class tile():
    grass = pygame.image.load('Sprites/Images/Grass.png')
    water = pygame.image.load('Sprites/Images/Water.png')

#Loop#
while True:
    for key in pygame.event.get():
        if key.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    #World#
    for a in range(12):
        for b in range(16):
            if MAP[s] == 0:
                win.blit((tile.grass), (x, y))
            elif MAP[s] == 1:
                win.blit((tile.water), (x, y))
            x += 50
            s += 1
        x = 0
        y += 50
    x = 0
    y = 0
    s = 0
    #Update#
    pygame.display.update()
Asked By: Pydude

||

Answers:

I recommend installing the noise package.
Then use noise.pnoise1(x) for 1 dimensional Perlin noise, noise.pnoise2(x, y) for 2 dimensional Perlin noise, and noise.pnoise3(x, y, z) for 3 dimensional Perlin noise.

Answered By: PoliszOwl

First, the critical thinking: Perlin is a popular term but the actual "Perlin" noise algorithm is old and visibly square-aligned. Better, as a general rule, to use a Simplex-type noise.

I suggest PyFastNoiseLite: https://github.com/tizilogic/PyFastNoiseLite Follow the install instructions, then mirror the C++ example in the FastNoiseLite documentation here: https://github.com/Auburn/FastNoiseLite/tree/master/Cpp Be sure to note its internal frequency multiplication, which you can change with SetFrequency(f)

You can also use the Python noise library for Simplex-type noise, with noise snoise2(x, y) though if you wish to use snoise3(x, y, z) I would first consider the info here: https://www.reddit.com/r/proceduralgeneration/comments/qr6snt/countdown_timer_simplex_patent_expiration/

Answered By: KdotJPG

image of randomly genrated terrain
code for perlin noise in pygame:

  from PIL import Image
  import numpy as np
  from perlin_noise import PerlinNoise
  import random
  import pygame
  pygame.init()

  noise = PerlinNoise(octaves=6, seed=random.randint(0, 100000))
  xpix, ypix = 500, 500
  pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]



  screen = pygame.display.set_mode ((500, 500),  pygame.RESIZABLE)
  while True:
        for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  pygame.quit()
                  run = False
        for i, row in enumerate(pic):
              for j, column in enumerate(row):
                    if column>=0.6:
                          pygame.draw.rect(screen, (250, 250, 250), pygame.Rect(j, i, 1, 1))
                    elif column>=0.2:
                          pygame.draw.rect(screen, (80, 80, 80), pygame.Rect(j, i, 1, 1))                 
                    elif column>=0.09:
                          pygame.draw.rect(screen, (30, 90, 30), pygame.Rect(j, i, 1, 1))
                    elif column >=0.009:
                          pygame.draw.rect(screen, (10, 100, 10), pygame.Rect(j, i, 1, 1))
                    elif column >=0.002:
                          pygame.draw.rect(screen, (100, 150, 0), pygame.Rect(j, i, 1, 1))
                    elif column >=-0.06:
                          pygame.draw.rect(screen, (30, 190, 0), pygame.Rect(j, i, 1, 1))
                    elif column >=-0.02:
                          pygame.draw.rect(screen, (40, 200, 0), pygame.Rect(j, i, 1, 1))
                    elif column >=-0.1:
                          pygame.draw.rect(screen, (10, 210, 0), pygame.Rect(j, i, 1, 1))
                    elif column >=-0.8:
                          pygame.draw.rect(screen, (0, 0, 200), pygame.Rect(j, i, 1, 1))

        #------------
        #run the game class

        pygame.display.update()
  pygame.quit()
  quit()


 
Answered By: king conor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.