-1

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()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Pydude
  • 149
  • 13
  • 5
    Seems like you're using a really low resolution, so your perlin noise basically looks like random static. If you want something like islands or rivers, that's not built in to perlin noise, so you'd have to figure that out on your own or look at other examples. I think if you generated some 3d noise, then cut off everything below a certain value, that'd get you your islands (you'd have to play around with the parameters, I suggest making a GUI with sliders or something that lets you mess around). As for rivers, maybe you could do like a random Brownian walk? This is pretty open-ended honestly. – Random Davis Nov 23 '21 at 16:58

3 Answers3

1

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()


 
king conor
  • 26
  • 1
0

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.

PoliszOwl
  • 1
  • 1
0

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/

KdotJPG
  • 811
  • 4
  • 6