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. (Date?)

This is how to create a sprite-clone system is used to create terrain for a moving sprite.

First, you need to make a sprite with at least 4 different costumes. 1 will be used for the ground. Put these scripts in that sprite:

define Generate terrain
show
switch costume to [what costume is the floor]
go to x: (0) y: (depends on where the ground is)
repeat (however many clones you want to make)
create clone of [myself v]
end
wait (3) secs //I made it 3 with estimation you can do a wait until block with a variable then it changes from false to true after other scripts
broadcast [done v]
when I start as a clone
switch costume to (pick random (1) to (costume number of ground costume, preferably last- 1)
go to [random position v]
repeat until <touching color [your ground (also preferably land) color] ?>
if <not <touching color [your ground (also preferably land) color] ?>>then
go to [random position v]
end
when green flag clicked
Generate terrain

It has some flaws however. For example, the floor and structures are the same color, floating islands can generate, or the player can get stuck.

Examples

See Also

Links