Rocket Landing Simulator

Gate Check #3 - Player Controls
Go Back to the Assignment Description

Part 1 (Overview) - Part 2 (Problem Description) - Part 3 (Rocket Controls) - Part 4 (Collision Check) - Part 5 (Metric Tracking) - Part 6 (Crash/Landing Detection)

Let's Check for Collisions!

Having a controllable rocket is fun for awhile. However, once you get over the initial thrill, you will realize that your rocket can fly through the ground and the boat without consequence. Let's do something about that!

In this section, we are going to modify the is_simulation_over() function. This function is responsible for checking to see if one of the simulation's end conditions have been met. If so, the function returns True; otherwise, it returns False.

So what exactly are the end conditions for the simulation? I'm so glad you asked!

Now that we know our end conditions, let's figure out how we can detect them.

The first end condition is pretty trivial. All we have to do is calculate the x coordinate of the left and right side of the rocket, using the x-coordinate and rocket width variables we created in Gate Check 1. We then see if either side of the rocket is going past the left or right side of the screen. If either of these cases is true, return True.

The second end condition (i.e., landing/crashing) is a little more complicated. We need to check underneath the rocket to see if any part of it is touching the ground. To better understand why we can't just look at a single coordinate, look at the following picture:

In the above example, the rocket's right side is not touching the water, but the left side is touching the ground. To account for these situations, we are going to need to be able to look at every x coordinate along the bottom of the rocket, and see what the corresponding height of the ground is (remember, in Gate Check 1, you created a list to store the heights). You then compare the terrain height to the rocket's y coordinate, and return True if the rocket is below the ground/water.

Here is a diagram to help you if the above paragraph is confusing.

The last thing we need to remember is that the simulation can only end once the Boost phase has ended. In other words, there is no point checking to see if the simulation is over until the simulation enters the Landing phase.

With these points in mind, code the is_simulation_over function(), using the following logic design as a guide:

def is_simulation_over():
    1.  If the simulation is NOT in the boost phase
        1a.  Calculate the x coordinate of the left and right side of the rocket
        1b.  If the right side of the rocket is past the right side of the window/screen
            - return True
        1c.  If the left side of the rocket is past the left side of the window/screen:
            - return True
        1d.  For every x coordinate from the left side to the right side of the rocket
            - Get the terrain's height at that x coordinate
            - If the rocket's y coordinate is below the terrain, return True
    
    2.  If none of the above is true, return False

As you are coding this, you might be wondering why we are checking to see if the rocket is hitting the ground, but NOT if the rocket is landing on the boat. The reason is because the boat is floating on the water. Your code will already check to see if we are hitting the water, and in the next section, we will add some additional code to see if we hit the water where the boat is.

Testing

Once you have finished writing the drawing code, press play. You should be able to pilot the rocket using the arrow keys. When you hit the ground, the simulation should automatically restart!

If your code appears to be working, proceed to the next section.

Proceed to the next section.