Very Simple Random Pixelated Planet Generation

in #gamedev6 years ago

1529503346.png

Above is a screenshot from my game Amber Sol, currently in development. You can try the latest demo version out by downloading it from the projects itch page.

The above planet is generated with filled cells each being drawn every frame using a random seed value which is also derived based on a master seed and the location of the cell. So given the same master seed and the location the same planet image will be generated. This is a really cool property of pseudo-random number generators. It is also a really useful technique used to create large unique but reproducible worlds.

Pseudo-Random Number Generators...

So pseudo-random number generators use seed values to derive a unique list of numbers with an even distribution.

Meaning the order will be unique but you will also see each number close to the same number of times. This means that we have some predictability to the sequence as well as reproducibility, which is really cool.

Effects of seed values

In other words, if I use one of these pseudo random number generators to generate a random number between 1 and 10 with a specific seed value. I will get the same sequence with that seed each run of the program. Or each time I reset the seed value for that generator.

In the solitaire game that I wrote available on itch and android I save games simply by saving the seed value used to shuffle the cards. As well as every move you made since you started. Those moves are then used when loading a game to get you back to the state you were in when you left the game. This minimizes the game saves. Is very quick to save and reload. And provides a way to undo all moves you've ever made even after loading an old save.

Even distribution

If I generate 10,000 integers I will get each of the ten numbers approximately the same number of times. One thing that you want to do in many games is give something a chance to happen. Maybe it's dice roles or whether a monster attacks you or not. You can apply chance to these generators pretty easily, by picking x in y chances.

Writing a chance function

So you can write a simple chance function knowing that every number in any sequence will occur a similar number of times. Say you want 1 in 5 odds that a statement is true. Have your generator generate numbers between 1 and 5 pick a number lets just say 1. If the number is 1 then the statement is true. And the function will return true approximately once for every five generations. But depending on the sequence it could happen at seemingly random intervals.

Here is an example of a chance function. For the Love2D engine which is what I am writing my games in:

function chance(once, everyn)
  return love.math.random(1,everyn) == once
end

We can write this in just plain lua using the math.random function instead by just removing the love. part.

Here is what happens if we add a random amount of green to each cell with a 1 in five chance for a cell to contain green if the planet is gray.

1529515941.png

So each cell has a 1 in 5 chance of being randomly some amount of green. In the future I hope to add multiple types of random features to each planet making them more distinguishable and unique. Things like lakes, and rivers, mountains. Volcano etc.

Here is another one I added.

1529516095.png

P.S. The green rectangle is where enemy ships appear orbiting the planet that you need to eliminate in the game.

Sort:  

Pixelated ... never go out of style :) I see you have quite a method to genrate this... appreciated.

Hi @zealouscoder!

Your post was upvoted by utopian.io in cooperation with steemstem - supporting knowledge, innovation and technological advancement on the Steem Blockchain.

Contribute to Open Source with utopian.io

Learn how to contribute on our website and join the new open source economy.

Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV