Student Subscription site screenshot Subscribe now

Subscribe to our Student Site to access all of our Games Coding Lessons for just £8 per month


Scratch games coding lesson 241

Terrain generator

Use the pen and cloning to create a self generating terrain for your player sprite to walk on.

 

NOTE: You must use Scratch 3 or higher.

 

Complete these steps ..

 

1. Open Scratch, OR use File > New to create a new game ..

 

 

2. Download, save and then open this game file ..

 

 

3. Select the Stage.

Load the Pen code blocks if you need to.

Now add this code to begin and end the game ..

 

 

4. Select the terrain sprite.

Create these 2 variables for this sprite only ..

 

The Y randomiser variable will be used to help the Pen to draw a more realistic and random undulating (up and down) terrain.

 

Create these 2 blocks to run without screen refresh ..

 

Every time the Cat leaves the right or left edge of the stage it will reappear on the opposite edge and a new terrain will be generated.

Add this code to the Terrain sprite ..

 

Add this code to the Terrain sprite to prepare your pen to draw a terrain from left to right across the stage.

Add this code to the Terrain sprite ..

 

We need the pen to move until it reaches the right edge of the stage.

Notice how we are using the sine of operator and an ever changing Y randomiser variable value to create a a calculation that undulates the position of the pen.

Add this code to the Terrain sprite ..

 

Each time the pen moves (changes its x position) it will draw the terrain from the pen position (Y) to the bottom of the stage.

As it moves down we will change its colour so we get green grass on top and brown dirt below.

Add this code to the Terrain sprite ..

Press the B key on your keyboard to test your code.

When you are ready, press the E key to end your game and continue working on it.

 

 

5. Select the Tree sprite.

We will clone this sprite to appear on the grass.

Add this code to the Tree sprite ..

 

Now add the cloning code ..

 

Create a new block named tree fall to run without screen refresh and then add this code

Press the B key on your keyboard to test your code.

When you are ready, press the E key to end your game and continue working on it.

 

 

6. Select the Cat sprite.

The player will control this sprite.

Add its properties code ..

 

The Cat will be able to run along the top of the Terrain.

Add the code to move left and right ..

Press the B key on your keyboard to test your code.

When you are ready, press the E key to end your game and continue working on it.

 

Now add the code so the Cat can reappear whenever it leaves the edge of the stage ..

Notice how we generate a new Terrain each time the Cat leaves the edge of the stage.

Press the B key on your keyboard to test your code.

When you are ready, press the E key to end your game and continue working on it.

 

Now we need to create the code to make the Cat forever fall unless it is touching the green grass.

Create a new variable for this sprite only named Y speed, and a new block named walk on terrain set to run without screen refresh.

Now add this code ..

 

Finally add the code to push the Cat back up onto the grass ..

Press the B key on your keyboard to test your code.

When you are ready, press the E key to end your game and continue working on it.

 

 

7. Use File > Save to your computer to save your game. Name it Terrain generator v1.sb3

 

 

8. Well done, you're finished this lesson.