0

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

Samuel
  • 11
  • 10
  • 1
    Please consider removing the pygame tag as your question is loosely coupled to it (you don't import pygame in the posted code). – qouify Jan 05 '22 at 16:32
  • 1
    Of _course_ it's possible -- the beautiful thing about Turing-complete languages is that anything you can do in one, you can do in every other. Mind, you simply _can't have_ something like a `randomGen` function that just gets called once and returns your complete map; instead, you need something that asks for generation _for a given location on the map_, potentially (but not necessarily) using a specific seed. So the whole interface needs to change. – Charles Duffy Jan 05 '22 at 16:53
  • @qouify This is only a snippet of my code. It relates to pygame, I promise. – Samuel Jan 05 '22 at 17:00
  • Does the answer depends on the fact that you're using pygame? I believe it does not and if I'm not wrong then the pygame tag is superfluous. – qouify Jan 05 '22 at 17:27
  • Ok, I will remove the tag. – Samuel Jan 05 '22 at 17:37

1 Answers1

1

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.

JohnAlexINL
  • 615
  • 7
  • 15
  • 2
    Note the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer), and particularly the bullet point regarding questions that "...require too much guidance for you to answer in full, or request answers to multiple questions". I'd argue that fully answering this question (discussing the range of algorithms available expansively) would be a topic for a book, and that the question is a whole is far too broad to be on-topic here. – Charles Duffy Jan 05 '22 at 16:52