Random Generation

Understanding the language, error messages, etc.

Random Generation

Postby Skyrunner65 » Sat Apr 11, 2015 10:18 pm

I know this is kind of a n00b question, but how can I randomly generate a map?
To get a better grip on this:

Say I have 3 objects: a pit, spikes, and a crawlspace in a horizontal scrolling game.
The player will continually move right, but these randomly pop up to make something along the lines of Canabalt.

It's another of my little ideas, but this is something I don't understand.
User avatar
Skyrunner65
 
Posts: 371
Joined: Thu Mar 20, 2014 5:37 pm
Location: NC,USA

Re: Random Generation

Postby rodot » Mon Apr 13, 2015 9:41 am

Hey,

First, you have to generate random numbers. To do so, you can use the random() function from the Arduino Library.

If you want you pseudo-random numbers to be different each time you start the game, you should first pick a random seed... the Gamebuino library includes a function which picks a random random seed : gb.pickRandomSeed() (by mixing time elapsed since startup, battery voltage and ambient light). You should call it once on startup, for example in you setup() function just after gb.titleScreen().

You can use the random numbers you generate to fill your tile map. there is several examples on the forum about tiles maps, and also the example from the library : TileMapRAM. Note that you have to store your map in the RAM as in the linked example, you can't use maps stored in progmem (as in the ufo-race game) as progmem is read-only (except if you use Myndale's bootloader function but I think it's too advanced for your application).
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 61 guests

cron