Gate Check #3 - Player Controls
Go Back to the Assignment Description
Currently, your simulation knows when the rocket touches the ground and/or the sides of the screen. At this time, however, your program has no way of determining whether or not the rocket successfully touched down on the landing boat. For the last part of this gate check, we show you how to remedy this problem.
The first thing we need to do is figure out where we are going to add this logic. If you recall, our main animation loop is structured as follows:
# Initializes the Simulation At Least Once
initialize_simulation(True)
# Main "Game Loop"
while pythonGraph.window_not_closed():
if is_simulation_over() == False:
erase_objects()
draw_objects()
get_input()
update_objects()
else:
analyze_results()
initialize_simulation(False)
The analyze_results() function is responsible for assessing how the player performed during a given simulation run. This function is only called when is_simulation_over() returns True, and gives us the opportunity to see if the rocket landed/crashed (and assign a score) before resetting the entire game.
To implement analyze_results(), we need to do two things:
How do we know that the rocket landed or crashed? Here is a participation activity to help you think about this problem:
Depending on the outcome, we will update the crash and/or landing variables we created in the previous section. This will allow us to keep a running total over the course of multiple runs.
Each time the rocket lands, our code needs to assign it a score. How you do this is up to you. You can, for example, assign a fixed amount of points for landing on the boat. You can then assign additional points based on how fast it landed, how gently it hit the boat, etc.
You may be wondering WHY we are creating a score. This will become clear during the next Gate Check. 5b.For now, all you need to know is that analyze_results() function will calculate a score based on whether or not the rocket landed on the boat. Your function will then update the maximum score (created in the previous section) if the score for the current run is higher.
Now that we have addressed both of these problems, go to the analyze_results() function and implement it using the below logic design as a guide:
def analyze_results()
global
1. Calculate the left x coordinate of the rocket
2. Calculate the right x coordinate of the rocket
3. Calculate the left x coordinate of the boat
4. Calculate the right x coordinate of the boat
5. If the boat's left x <= rocket's left x, AND the boat's right x >= rocket's right x
5a. assign a score to the rocket
5b. increment the successful landing variable by 1
else
5c. assign a score to the rocket
5d. increment the crashed variable by 1
6. Update your maximum score global variable if needed
To verify that everything is running, run the simulator for several runs. Each time a new run starts, you should see updated values in the HUD for the max score and/or crash vs. landings counters.
If you see the above image (or something similar), you have completed this gate check.
Proceed to the next section.