Level randomization?

Good morning all :slight_smile:

I was wonder if there was a code to creation randomization in levels, objects, sounds, bgmusic, etc.

What I have done is draw 20 Background images, 50 background muic tracks, 15 types of space debri, and Enemy ships.

What I would like to happen is once the player has reached the goal assigned to level 1, which mind you I am using as the “Get used to the controls” level, the game will Grab one of each item listed above (Randome BG, Random BGmusic, Random Space Junk etc), and put those items in the next level. Doing this will make the game different every time the player starts it up.

Also, is there a High Score Board code? I would like to have like 5 places that allow the player to put in their name in on a scoreboard that saves indeffinately. Also, I would like to have a level score and a total score. The Level score resets with each new level or the restart of the game, and the Total score stays constant until the last player life is gone at which time if the player has reached a high enough score, they will be prompten to put their name on the High Scoreboard.

And as always, thank you all so much for helping a noob create a game :slight_smile:

1 Like

Two good questions that one day I’ll create a video tutorial for… Until then…

Before you start, declare five global variables, highscore1, highscore2, highscore3, highscore4, highscore5.

Do you know how to do scores? If not, watch this up until the part where I store a score in a variable: youtube.com/watch?v=KjRV-Pu6Y38

Then, at the end of the scene, you want to compare the score the player got with highscore5 (fifth highest score). If the player’s score is lower, they didn’t get a high score. Otherwise, compare against each of the other high scores until you find out where the player’s score should rank. Do all this with the actions in Variables → Global Variables, where you can compare GlobalVariable(highscorex) with Variable(playerscore).

Once you have done this, copy each score to it’s new, correct position. For instance, if the player got the 3rd highest score, then highscore4 is copied into highscore5, highscore3 is copied into highscore4 and Variable(playerscore) is going to go in highscore3.

Yes, there is! You want to do it using random numbers like this:

Actions: Do = Random(3) to Variable(whichpicture)

Then in sub events to this:

Conditions / Actions

Variable(whichpicture) = 0 / Create object picture1 at x,y
Variable(whichpicture) = 1 / Create object picture2 at x,y
Variable(whichpicture) = 2 / Create object picture3 at x,y

Hopefully this helps?

That solution is useful for easy-fast generation of graphic based maps (background images, and objects in a free to move space like Mordios game)
But usually it’s much more complex, since random levels in most of games has block-way objects, you need an algorithm to avoid a non-exit level and more (no overlapping objects, sense… in the way you shouldn’t generate a ladder inside a wall :stuck_out_tongue: )

Of course I haven’t a better idea for a space game than yours, but maybe you could take some of this into account for video tutorials :slight_smile:

Thank you Lizard.

Ok, I’ll get the the score thing later since it is not as high of a priority as the Randomization ATM.

I want to have the game randomly choose a BG, Music, Enemyships, ect. I am assuming that I need to create groups of the images, all BG’s go into one group objects, all music goes into another, all asteroids go into another, etc? And a Variable for each group?

Actions: Do = Random(3) to Variable(whichpicture)
= in this code, the (whichpicture). Is that the name of the image? And the 3 in Random(3) = how many total BG, music, ships, space junk?

Variable(whichpicture) = 0 / Create object picture1 at x,y
Variable(whichpicture) = 1 / Create object picture2 at x,y
Variable(whichpicture) = 2 / Create object picture3 at x,y

In this code, if whichpicture is the actual name of the item, create object picture# isthat name of the image I want to use?

So I make the following code for each image group I want to take an item from

Do = Random(#) to Variable(Backgrounds)
Variable(Backgrounds) = 0 / Create object BG1 at x,y
Variable(Backgrounds) = 1 / Create object BG2 at x,y
Variable(Backgrounds) = 2 / Create object BG3 at x,y

Where do I insert this code? I think it goes at the top. Using

(Condition) At the start of the scene II Do
(Sub(Condition)) Do = Random(#) to Variable(Backgrounds)

(Action) Variable(Backgrounds) = 0 / Create object BG1 at x,y
(Action) Variable(Backgrounds) = 1 / Create object BG2 at x,y
(Action) Variable(Backgrounds) = 2 / Create object BG3 at x,y

Do I need to number each image seperately? BG1,BG2,BG3, etc.?

Looking at the code again:

Variable(Enemyships) = 0 / Create object Enemyship at Random(1920),-50
Variable(Enemyships) = 1 / Create object EnemyShip2 at Random(1920),-50
Variable(Enemyships) = 2 / Create objectEnemyshi3 at Random(1920),-50

About random background… take in mind that one background or another are the same with a different image (if your game has backgrounds with different sizes and/or different behaviors please stop reading because my tip will be useless), so you could create ONE object Background and add animations on it instead of add many objects, and change the animation of Background randomly:

Background: -Animation 0: "Background01.png" -Animation 1: "Background02.png" -Animation 2: "Background03.png"
Then you can set your code at the beginning of the scene:

At the beginning of the scene || Create object Background at X;Y || Do = Random(2) the number of animation of Background
And you’ll save some code. Usually, when you have to repeat a piece of code modifying only some part (in this case the Background object 1, 2 or 3) you could make it shorter through variables, animations or… did I mention variables? :stuck_out_tongue:

Finally, and it’s more advanced, you could create random different objects easily if you like to set them as different objects (because each ship has many animations itself, very different behaviors, etc.) you could name them with a number (as you did) like: Enemy1, Enemy2, Enemy3.
Then GD has an action “Create an object from its name”, What name?.. “Enemy”+a_number
of course a_number must be random, and GD has another function ToString() to convert a number to string, with this event you should create any enemyship randomly:

Condition to create an enemy		||		Create object "Enemy"+ToString(Random(2)+1) from its name at X;Y

Random(2) returns 0,1 or 2… so Random(2)+1 returns 1,2 or 3, the number of the ships :laughing:

I’m actually writing a roguelike at the moment that features random level generation. Last week, I wrote the code to randomly generate a village. I will use this as the example, as it needs connected roads and houses that are not in stupid places. I’ll also do a tutorial on a dungeon generation algorithm too.

Variable(whichpicture) is just a scene variable. I just gave it a name so you can know what it is referring too. I am now used to there being 100+ variables in a scene and so it’s really important to give them a name from which you can quickly realize what it is for.

Variable(whichpicture) is just a scene variable. Create object picture1, picture2 etc is the name of the sprite you want to use. You need to attach the image to a sprite to use it in game.

And yes, for your background, do that at start of scene. And also yes - For random enemy generation, you can use this method too.

1 Like

I don’t have exact link, but there was post not too long ago explaining how Spelunky randomizes it levels. You may want to look for it, it should get you some ideas.

1 Like