How to generate perlin noise in pygame?

2024/10/9 2:26:21

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 += 50s += 1x = 0y += 50x = 0y = 0s = 0#Update#pygame.display.update()
Answer

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

  from PIL import Imageimport numpy as npfrom perlin_noise import PerlinNoiseimport randomimport pygamepygame.init()noise = PerlinNoise(octaves=6, seed=random.randint(0, 100000))xpix, ypix = 500, 500pic = [[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 = Falsefor 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 classpygame.display.update()pygame.quit()quit()
https://en.xdnf.cn/q/118637.html

Related Q&A

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…

Implementing a Python algorithm for solving the n-queens problem efficiently

I am working on a project that requires me to solve the n-queens problem efficiently using Python. I have already implemented a basic recursive algorithm to generate all possible solutions, but I am lo…

Annotations with pointplot

I am using a pointplot in seaborn.import seaborn as sns sns.set_style("darkgrid") tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hu…

How do I get data in tuples and tuples in lists?

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).manhattan=[[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11…

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…

How to add and subtract in python

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering whats wrong with it?numberstoadd = input(…

Python: deferToThread XMLRPC Server - Twisted - Cherrypy?

This question is related to others I have asked on here, mainly regarding sorting huge sets of data in memory.Basically this is what I want / have:Twisted XMLRPC server running. This server keeps seve…

How do I make a linear gradient with Python Turtle?

Im currently trying to replicate this image: https://i.sstatic.net/fymWE.jpg Im trying to make that gradient in the background but I have zero clue how to do it and theres basically nothing on the inte…

Python - Converting an array to a list causes values to change

>>> import numpy as np >>> a=np.arange(0,2,0.2) >>> a array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8]) >>> a=a.tolist() >>> a [0.0, 0.2, …