A more complex terrain generator with variable properties. It generates a different, randomized terrain each time the project is run.

Terrain Generators are projects that combine art and programming to generate a nature-like environment known as terrain. Terrain can contain slopes, dirt, trees, cliffs, water, and other things present in the environment. They are often created using stamping and pen techniques and tagged under games, simulations, and art. They can range from simple 2D textured blocks to complex 3D terrains.

Usage

Many terrain generators are simply made to showcase the landscapes. Sometimes terrain generators can be incorporated into other types of projects, such as games. A randomly-generated terrain can create a different field of play each time the game is restarted. This can increase the enjoyment every time the game is replayed since the terrain is completely unique. Terrain can also be generated in a scrolling project in real-time as the background scrolls, allowing the screen to scroll an infinite distance. In platformers, randomly generated obstacles can provide a unique challenge.

Creating a Simple 2D Generator

A series of textured tiles that fill up the stage is the easiest example of a basic terrain generator. Some predefined parameters need to be known for this to work: the different number of terrain tiles, the resolution of the terrain tiles, and the costume center point of the tiles. The resolution of the stage is 480x360 pixels, so using tiles with dimensions divisible by the stage's dimensions is preferable to ensure everything fits properly.

Depending on the exact position of the costume center, the scripts need to be tweaked to display the grid perfectly on the stage. Otherwise, all the tiles may end up shifted at a slight offset. For this example, assume the center of the tile costumes is in the very top-left corner of the tile. This is simplest since it requires no offset correction.

Using bitmap graphics is preferred for tile-based terrain generators. Since the tiles are stamped to the stage, vector graphics would lose their sharpness in full screen mode anyway. Bitmap graphics have an exact width and height in pixels, allowing them to be precisely designed for the project.

If the stage is to be filled with 100 tiles in a 10x10 configuration, the resolution of the tiles would need to be 48x36 pixels. This is calculated by dividing the stage's width and height by the number of tiles per column and row. Once all the costumes are designed, a custom block as follows can be used to instantly print out a randomly generated terrain onto the stage.


Note Note: The following custom block should have the "run without screen refresh" option enabled.
define generate terrain with (rows) rows and (columns) columns and (diff tiles) types of tiles
set [tile width v] to ((480) / (columns))
set [tile height v] to ((360) / (rows))
go to x: (-240) y: (180)
show
repeat (rows)
  repeat (columns)
    switch costume to (pick random (1) to (diff tiles))
    stamp
    change x by (tile width)
  end
  change y by ((0) - (tile height)) //jump down a row
  set x to (-240) //start on left side again
end
hide

There are a couple downsides of this very simple custom block. Firstly, it does not support any type of scrolling or zooming; support for those would require more complexity. Secondly, it simply picks an entirely random tile rather than placing similar tiles adjacent to one another. For example, water is generally found as a body, such as a pond or lake. With the above script, water tiles may be placed sporadically apart from each other and surrounded by other vastly different tiles. This may not be a good representation of an actual terrain.

This can be fixed by incorporating logic into what specific tiles are chosen to be placed. The best method is to create a list that will contain the costume numbers of the tiles meant to be stamped. An AI script can then begin filling up the list by starting with a random tile but then only picking a similar tiles for the ones surrounding that initial tile. If similar tiles are placed around each other, there can be more smooth transitions into different parts of the terrain.

Creating Terrain for Games (2D)

Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (June 2019)

To create a sprite-clone system that generates terrain for a moving sprite, begin by making a sprite with at least 4 different costumes. Make one of them a ground tile. Next, add the following code to the sprite:

define Generate terrain
show
switch costume to (floor costume v)
go to x: (0) y: (wanted ground y)
repeat (needed amount of clones)
create clone of (myself v)
end
wait (3) secs //this can be a "wait until" block to wait for other scripts to complete
broadcast (done v)
when I start as a clone
switch costume to (pick random (1) to ((ground costume #) - (1) )
go to (random position v)
repeat until <touching color [ground color v] ?>
if <not <touching color [ground color v] ?>>then
go to (random position v)
end
when green flag clicked
Generate terrain

However, this code contains some flaws. For example, the floor and structures are the same color, floating islands can generate, or the player can get stuck.

Examples

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.