Samuel
Samuel

Reputation: 31

Is it possible to have infinite world generation in pygame?

I am making a game in pygame, and would like to add infinite World generation. I already have implemented the drawing mechanism, and the randomGen method, which I will list here. I am wondering if there is a way to do infinite world generation.

Random Generation:


def randomGen(graph):
  print("[Debug] Begin randomGen...")
  # We assume the user has not done anything with the graph, so we add the sky and grass
  for i in range(20):
    newgraph.append('O')
  newgraph.append('NL')
  
  for i in range(20):
    newgraph.append('O')
  newgraph.append('NL')
  for i in range(20):
    newgraph.append('O')
  newgraph.append('NL')
  for i in range(20):
    newgraph.append('O')
  newgraph.append('NL')
  for i in range(20):
    newgraph.append('O')
  newgraph.append('NL')
  for i in range(20):
    newgraph.append('G')
  newgraph.append('NL')

  # Next begins the random ore gen
  currenty = 5
  
  for i in range(20):
    x = 0
    for i in range(20):
      # Chance of coal - 1 in 15
      iscoal = random.randint(1,15)
      
      if iscoal == 6:
        graph.append("C")
       
      else:
        graph.append("S")
      x += 48
    graph.append('NL')

Upvotes: 0

Views: 544

Answers (1)

JohnAlexINL
JohnAlexINL

Reputation: 626

Absolutely. Python doesn't have a built-in upper limit on memory, so you could just use a seed to calculate points on the world and commit them to memory -- maybe in a dict. Then, you'd just need to store any changes separately. When you save the gamestate, you'd actually only need to save the changes, and the original seed.

The more complicated part is creating your method for converting the seed and an x,y position into a single pseudorandom number -- because you want the output to always be the same, given the same inputs, but still seem random.

One great method for doing this is Perlin Noise, used by games like Minecraft. You generate a small grid two dimensional grid, and calculate outwards to whatever point of interest. Given a seed and a point, you will always get the same results.

Upvotes: 1

Related Questions