We will add a script to the sprite we just created that selects a costume at random and copies it onto the stage. With a double for
loop, we will make sure the tile generator steps around the entire stage, so no space is left blank.
To set up this game, we will create a small control script in the Stage object to set everything in motion as follows:
This message will trigger when a new maze should be generated. It can be activated by pressing the Spacebar key and at a later stage, through the script when the player completes a level.
With the basics out of the way, we can start with the fun stuff; drawing levels automatically. We will draw the level from the lower-left to the upper-right of the screen using the following steps:
Basically, we make the tileGenerator sprite go to a new position on the stage repeatedly. What should be filled in on the blanks requires some calculations. We will leave those until the end of this step, so we can test the results while working on that formula as follows:
With the basic structure of the script done, we still have to figure out where to place the tileGenerator sprite before we stamp an image. This requires some calculations based on the number of repeats the loops are currently at. To save these values, we use the xIndex and yIndex variables as counters. Perform the following steps:
tileSize * xIndex – 240 and tileSize * yIndex – 180 respectively.tileSize * xIndex – 240 – tileSize / 2
tileSize * yIndex – 180 – tileSize / 2
The following screenshot shows the formulae:
This fixes our offset problem. The maze is now correctly aligned with the stage boundaries.
This one script is all that is needed to create a new interesting level each time the Space bar key is pressed. Try it a few times to see what kind of designs the computer comes up with. Due to the randomized costume selection, you'll notice that the paths through the maze will be different each time.
You might notice that in many cases, the paths will be relatively short with walls blocking the passage at fairly short distances. This is because each tile has an equal chance of being selected by the randomizer. However, some tiles, the straight corridors especially, contain more wall segments than open passageways.
We can manipulate the random selection of tiles to favor the ones that are more open. We already made sure that the more closed tiles are at the front of the costume list while the more open paths are towards the end with the open square closing the list.
We use this knowledge to perform a calculation on the random selection which favors higher numbers over lower ones. We calculate this using the following steps:
1
to 144
.Why does this work? This is because the square root of the higher numbers in the extended range resolve to a higher value more often. For example, only square root of 1
and 2
rounded off will resolve as 1
. But all the numbers from 133
up to 144
will resolve as 12
. That means 12
chances to come up with costume 12
and only 2
chances to come up with costume 1
.
3.129.218.69