Rocket Landing Simulator

Gate Check #1 - Initial Functionality
Go Back to the Assignment Description

Part 1 (Overview) - Part 2 (Getting Started) - Part 3 (Initializing the Sim) - Part 4 (Terrain) - Part 5 (Rocket) - Part 5 (Boat)

Initializing the Terrain

The first thing we are going to initialize the terrain (i.e., the water and the ground). To do this, we are going to need to perform the following tasks:

STEP 1: Define Our Variables

Let's think about what variables we need to create in order to generate the terrain. Obviously, we will need variables that will keep track of the ground height and the ground width.

We also need to keep track of the water's height. You might also be tempted to create a variable for the water's width. However, in the following section, we will explain below why this variable is not necessary.

Finally, we need a way to determine the height of the terrain at each X-Coordinate of our screen. In other words, we need a way to be able to store a BUNCH of Y-coordinates so that we can ask our program "What's the height of the ground/water at x = 100". I wonder how we can do that...

So, it looks like we need to create 4 variables:

So let's get to it! Go to the top of the template and look for the following line:

#Terrain

Create your 4 terrain variables under this comment and give them whatever value you want. As an important side note, variables declared outside of a function are called global variables in Python. Global variables are special because every part of your program (your main code and functions) can access their values. In the template, for example, we already created the global variables SCREEN_WIDTH and SCREEN_HEIGHT to keep track of the width and height, respectively. You will use these variables throughout your program, so make sure you get familiar with them!

Functions can also change a global variable's value without having to return it. To do so, however, we add an additional line to their code, as shown in the following code example:

# This is a global variable
GROUND_HEIGHT = 0

# This is a function
def initialize_terrain(generate_new_scenario):
    global GROUND_HEIGHT                           # <----- THIS TELLS PYTHON YOU WANT TO CHANGE A GLOBAL VALUE
    
    GROUND_HEIGHT = random.randint(100,200)        # <----- This sets GROUND_HEIGHT to a random integer between 100-200
                                                   # <----- THIS IS JUST AN EXAMPLE, YOUR REAL GROUND HEIGHT WILL BE BIGGER!

STEP 2: Initialize Variable Values

Now that we have declared our variables, we need to actually assign them values. Go to the initialize_terrain function and delete the print statement. Then write code to assign them values, using the logic design below as a guide.

As you are writing this code, you may be asking yourself "what random number(s) should I give to the ground width, ground height, and water height variables?" That's a really good question! Here are some participation activities that should get you on the right track.

Once you have completed this function, run your code. Nothing will happen! This is to be expected, as you have not yet wrote any code to DRAW your terrain. You will accomplish this in the following step.

STEP 3: Drawing the Terrain

The time has finally come for us to draw the terrain! In order to draw the terrain, we need to find the function(s) in the main loop that are responsible for drawing and erasing each frame. Let's see if you can figure it out:

Testing

When you have finished, press play. You should see something similar to the following:

If your program looks similar this, then you have successfully completed this step! Move on to the next section.

Once you see this, proceed to the next section.