Reputation: 65
I'm trying to make a 2d tile based game in pygame. I've implemented an algorithm that converts the characters in a 2d array into the tiles, with the posiiton derived from the row and column of the character in the array. Eg-
The Array- (manually typed "terrain")
The game- (The g's and s' in the array correspond to grass and stone respectively. )
Here
Now, i want to generate the "G"'s in the list with some kind of noise, so I can further implement it to generate infinitely. The few documentations I've seen on this were really hard to understand :(. So how do I use noise to autogenerate the "G"'s in the terrain? Thanks!
Upvotes: 2
Views: 581
Reputation: 260580
I would suggest to generate your world column by column. As I presume this is some kind of platform left to right game, this would make more sense.
Below is a small snippet to show you a possible simple logic to do so. There is a fixed number of S, G can vary +1/0/-1 at each new step. Finally the last line is to convert to your line format to display:
import random
l = 9 # world height
g = 2 # initial number of G
out = []
for i in range(15):
g += random.randint(-1, 1)
g = max(0, min(g, l))
s = ' '*(l-g-1)+'G'*g+'S'
out.append(s)
worldArray = [''.join(s) for s in zip(*map(list, out))]
Example:
[' ',
' ',
' ',
' ',
' ',
'G G G',
'GG GGG G GGGG',
'GGGGGGG GGGGGGG',
'SSSSSSSSSSSSSSS']
The most important and tricky part now will be to find nice parameters to change the world randomly. For example you might want to have more or less frequent changes in altitude (then use for example a non uniform random function)
Upvotes: 2