pythonpygameperlin-noise

How to generate perlin noise in pygame?


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

Solution

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