Artist's concept showing the Perseverance rover collecting samples on Mars

Overview

In this activity, students use the Scratch programming language to create a video game that simulates the process of collecting samples on Mars. This activity is designed to be completed after the Explore Mars with Scratch activity. Students will engage in computational thinking as well as use math and elements of real rover-mission planning to design their game.

Materials

Management

  • This activity is divided into multiple sections. The first section is an introduction to setting up a Mars rover game using the Scratch programming language. It can be completed as a stand-alone lesson, or in conjunction with any of the following sections, depending on student and teacher familiarity with Scratch and block-based programming languages.
  • If you plan to complete multiple sections of the lesson, one or more sections can be completed per class session.
  • It is important to emphasize that the rover and game created will only do what the code tells it to do and in the order that commands are sequenced. As students create code, allow them to explore what happens when they try different command blocks. There are many ways to achieve certain results, and not every student or group will create the same code or game.
  • As they develop their game, students should take turns testing out other students’ games to see if they are working as planned. Student players should provide feedback about what happened when they played the game as well as what they thought should have happened. Game testing can be done by running the game in editor mode or clicking See project page in the top section of the editing window on Scratch. The game can also be tested and played in full-screen mode by clicking the full-screen mode icon.
  • Students will need individual Scratch accounts, or instructors can sign up for a teacher account to create and manage accounts for groups of students.

Background

The Perseverance Mars rover will collect rock cores, place them in sample tubes, and leave the tubes on the surface for a future rover to collect for eventual return to Earth. The future rover, called a fetch rover, will drive around Mars to collect the sample tubes and place them in a Mars ascent vehicle – the rocket that will carry the samples from the surface into orbit around Mars. An additional spacecraft will be required to capture the samples in orbit and return them to Earth.

Procedures

Students should create a drivable rover and complete a teacher-determined number of game elements listed below. Students can view the Code a Mars Sample Collection Video Game project page and work independently to complete the tasks if appropriate.

Section 1 - Create a Drivable Rover (10-20 minutes)

  1. Download the Jezero crater surface images, Mars fetch rover sprite image, and the Mars sample-collection tube sprite image, and save them to a computer. Unzip the surface images zip file into a new folder.
  2. On the Scratch website, sign in and click Create to begin a new project.
  3. Create a new backdrop by moving the mouse over Choose a Backdrop in the Stage window and selecting Upload Backdrop. Pick the Jezero crater surface image of your choice. The Stage area of the screen should now display the selected backdrop image.
  4. Create a new sprite by moving the mouse over Choose a sprite in the Sprite window and selecting Upload sprite. Choose the Fetch rover sprite image. There should now be a rover placed over the backdrop image along with the original cat sprite.
  5. Delete the existing cat sprite by clicking on it in the Sprites window and clicking the “x” in the corner.
  6. With the rover sprite selected, create the code to control the rover. Click the Code tab and choose Events.

    Starting with the event command When __ key pressed , help students understand that they can command the rover to do something when a particular key is pressed.

    Drag the When __ key pressed block onto the workspace on the right. Ask students what keys they want to press to make the rover move. This may elicit various responses, but ultimately students should settle on the up, down, left, and right arrows to move the rover.

    After dragging the When __ key pressed block into the workspace four times, students should select one of the four arrow keys from the dropdown menu in each block.
  7. Students will notice that the rover does not move yet. They need to code the rover to do something when the indicated key is pressed.

    Allow students to explore the blocks within the Motion scripts by connecting them to When __ key pressed one at a time and seeing what happens when the keys are pressed. Students may want to press a key to turn the rover in the indicated direction. They may want to make the rover move when the key is pressed. They will create code that does both. Tell students there are multiple ways to accomplish the same tasks, but for this step, students should find the move __ steps block and drag that under the When __ key pressed block.
  8. Students will likely notice the rover doesn’t turn and only moves in one direction. Ask them to explore which command they could add that would make the rover turn and move. The most straightforward method is to add the point in direction __ block to the command block. Students will have to explore which direction variable to select to make their rover do what they want.
  9. Students may notice that the rover still seems to be moving unnaturally. Ask them what is happening in their code. As they study their code, they may realize that the commands make the rover move in a direction and then turn to face that direction. Ask them if that’s the order in which they move when they walk. By reordering the blocks, they can create command blocks that make the rover turn in the direction it is going to drive first and then move in that direction the desired number of steps. It is important for students to realize the program will run strictly in the order that it was programmed.

Students may experiment with their code by adjusting the number of steps their rover moves each time an arrow key is pressed. Each arrow key should move the rover the same number of steps. The lower the number, the slower the rover will move. Entering a number less than 1 will cause the rover to move one step only after the arrow key has been pressed multiple times. Consider setting an upper limit on the number students should use. (5 is recommended as an upper limit.)

At this point, it is a good time to show students that there are multiple ways to write code to accomplish a given task.

  1. Have students right-click on the rover control blocks and click Delete Block.
  2. Introduce students to the When flag clicked script. The run program flag is essentially a start button located above the upper left corner of the Stage area. When clicked, any sprite containing a When flag clicked script will behave as programmed. Students should drag the When flag clicked script onto the work area for the sprite.
  3. Introduce students to if-then statements as a way of making the program behave a certain way if a specific condition exists. The When __ key pressed is essentially a completed "if" statement (“if a key is pressed”) and the blocks connected to it are a "then" statement (“then turn and move”). Have students find the if-then script and drag it into the code workspace. The hexagon is a space in which another script element can be inserted and the "then" element of the "if" condition can be defined.
  4. Define the "if" portion of the statement. In this game, players will drive the rover using the arrow keys. To program what should happen if an arrow key is pressed, direct students to look in the Sensing scripts. Ask for ideas about what should go into the hexagon by having them identify which shapes will and won’t fit (e.g., round edges won’t fit into the pointed container). Have them use the word “if" in describing their ideas to see if there might be a command that makes sense. For this program, select the key __ pressed script and select up arrow from the dropdown list.
  5. Now students need to define the "then" portion of the statement. Ask them what they want to happen to the rover when the up-arrow key is pressed. They may realize that they need to use the point in direction __ and move __ steps as they did previously. They can simply click and drag those blocks into the if-then statement.
  6. If students connect the script block to When flag clicked and click run program flag, they will notice the rover doesn't move when they use the up-arrow key. Now is the time to introduce the forever script. When the flag was clicked, the script checked one time to see if the up-arrow key was pressed and then stopped checking. By placing the if-then block into a forever script, the script will continue to check to see if the up arrow is being pressed and move the rover if so.
  7. When students are satisfied with their script, introduce them to the duplicate feature. Have them right-click on the block of scripts and select duplicate. They should do this three times and modify the key __ pressed and point in direction __ scripts so that all four arrow keys will move the rover in the appropriate directions when the run program flag is clicked.
  8. Students may ask why they should make a script that is longer if it does the same thing as a shorter script. By using the When flag clicked script, they’re giving their code more versatility. If they would like, allow them to keep one arrow key command in the original format. When they develop more advanced scripts, they will see that they are limited in what can be controlled with that one key.

These command blocks are the basis for the Mars sample collection game students will develop. They can create any game elements and challenges they can figure out how to code, but for starters, students should use some of the following challenges, which would be part of a future mission to collect samples from Mars.

Section 2 - Add Sample Collection Tubes (10-20 minutes)

The purpose of a fetch rover sent to Mars would be to collect rock cores left in sample tubes by a previous rover and bring them to a Mars ascent vehicle. Students should add sample collection tubes to the gameplay area to provide destinations for their rovers.

  1. Have students add a new sprite by moving the mouse over Choose a sprite in the Sprite window and selecting Upload sprite. Choose the Mars sample collection tube sprite image. There should now be a sample tube placed over the backdrop image.
  2. Students can click and drag the sample collection tube sprite to any spot they want on the backdrop image.
  3. Remind students about the When flag clicked script. Students should drag the When flag clicked script onto the work area for the sprite.
  4. Remind students of the if-then statements as a way of making an element in a program behave a certain way if a specific condition exists. Have students find the if-then script and drag it to the workspace.
  5. Define the "if" portion of the statement. Ask students what they want to happen in the game, or what they want players of the game to do. In this game, players will drive the rover to the sample tube and collect it. To program what should happen if the rover reaches its target, direct students to look in the Sensing scripts. Ask for ideas about what should go into the hexagon by having them identify which shapes fit. For this program, selecting the touching __ script and the rover sprite (fetchrover) from the dropdown menu allows for the most flexibility in the program.
  6. Now students need to define the "then" portion of the statement. Ask them what they want to happen to the sample collection tube sprite when the rover touches it (e.g., "The sample tube should disappear to make it seem as if it's been collected"). (Instructions for adding a scoring system when tubes are collected is addressed further down and builds off of what will be coded here.) Have students view scripts in the Looks section. There are different options for how to complete the if-then statement. In this example, the hide script completes the "then" portion of the statement.

    Attaching this block of scripts to the When flag clicked script will not hide the sample collection tube sprite when it is touched by the rover as students might expect. (Allow students to connect the hide script to the When flag clicked script to see for themselves.) Remind students that the reason this doesn’t work as expected is because, as coded, the program only runs this command once – when the run program flag is clicked. For the script to work, the if-then statement must be running the whole time the game is being played. Ask students to identify which Control script will make the if-then statement run throughout the game. They should recall the forever script. This script can be dragged around the if-then statement to make it run throughout the game.
  7. When students connect this block of scripts to the When flag clicked script and click the run program flag, they will see the sample collection tube sprite disappear when the rover sprite drives into it. However, they will also notice that once it disappears, it does not return when the run program flag is clicked again. Ask students why it doesn’t return. They may notice that nowhere in the script does it say to make the sample collection tube sprite reappear. Ask them to identify which Looks script will make the sample collection tube sprite reappear.

    They should identify the show script. Ask students where they think it should go. Remind them the computer will run the script in order. Allow them place the show script into their code and test their game to see if they are correct. If the show script is placed inside the forever script, the sprite will return as soon as the rover moves away from it. To show the sprite only when the run program flag is clicked and the game is restarted, the show script must be placed outside the forever script.

    If students’ scripts look like the example images shown but the sample collection tube sprite still doesn’t disappear when clicked, double check that students have clicked the run program flag above the Stage area to start the script and that the rover sprite is not located directly on top of the sample collection tube sprite.
  8. Students can explore ways to modify the if-then statement to create additional actions when the rover hits the sample collection tube sprite. When they are satisfied with their commands, remind them to use the duplicate feature. Have them click the stop button to stop their scripts from running. They should then right-click on sample_tube and select duplicate. This will create a second sample collection tube that contains the exact same code as the first. They can click and drag this sprite to a new location. They should create multiple sample collection tube sprites, but not so many that the game is not winnable or becomes tedious.

Section 3 - Add a Countdown Timer (10-20 minutes)

NASA’s missions to Mars and other destinations have a planned life expectancy. Some missions, including every rover to date, have been extended and operated beyond their planned mission life. Some missions end after their expected mission life, while others experience problems that shorten their mission. Students can create a timer that counts down to the end of the mission and ends the game. As an added challenge, students can create code to extend or shorten the planned mission time.

  1. Click the rover sprite. Ask students which Events script they should use to start their timer code when the game starts. They should select the When flag clicked script and drag it to the workspace.
  2. Clicking on Variables scripts reveals no scripts because students have to create the variable they want to change. In this case, they are making a timer variable that will change as the game progresses. They should click Make a Variable and name it “timer.” Select the For all sprites option to make this variable available for use with other sprites. This will create a selection of scripts to choose from, all related to the timer variable.
  3. Ask students which of the new scripts should run first when the run program flag is clicked. They should choose set timer to __ and connect it to the When flag clicked script. They will need to enter the number of seconds they want their time to run. This may need to be adjusted as game elements are added and more time is required to complete the game. Students should enter enough time to allow players of the game to complete the game before the time runs out, but not so much time that the timer won’t reach zero if players aren’t focused on completing the game.
  4. Students will likely see the change timer by __ script and want to use that script to change the time. However, that can’t be used alone. It will need a combination of Control scripts. Ask students which Control scripts they think the timer might need, and how it might be structured. Students who select wait __ sec will find that either nothing happens (if they place it underneath set timer to __) the time will change by one second in the wrong direction, or the time doesn’t change (if they place it above set timer to __ ). Ask students why the timer only counted once and not down. They may identify the need for a forever script that will repeat the wait __ sec and change timer by __ scripts throughout the game. They may also identify the need to adjust change timer by __ to -1 instead of 1.
  5. When students run the timer, they may notice two things when the timer reaches zero – it will continue counting down into negative numbers, and nothing will happen. Ask students why. They should respond that the timer continues counting down because they haven’t created the code to make the timer stop or make something happen when it reaches zero.
  6. Students should start by creating another When flag clicked script in the rover’s code space. Ask students which Control script they should use if they want something to happen when the timer reaches zero. (You can emphasize the “if” in your question to clue them into the if-then script.)
  7. They will need to define the "if" portion of the script. They may remember they’ll need to find a hexagon shape, and because this deals with numbers, they should look in the Operators scripts. The _ = _ allows students to define what happens if the timer reaches zero. They will need to drag the timer variable into the hexagon and enter zero into the other white box.
  8. Next, students will need to define the "then" portion of the script. Ask them to look at the Control scripts to decide what should happen next. They should choose stop all and drag it into the if-then script. Students can experiment to see what happens when they choose all, this script, or other scripts in sprite from the dropdown menu.
  9. Students who connect the if-then script to their When flag clicked script and click the run program flag will find that nothing changes when their timer reaches zero, and it will continue to count down past zero. This is because when the run program flag was clicked, the script checked to see if the timer equaled zero just once. Since it did not, the script’s "then" portion was not carried out. Ask students which Command script is needed to make the if-then statement run for the entire game. They should identify the forever script.
  10. Students who kept one (or more) of the original arrow-key scripts (with the When __ key pressed script) will notice that when the timer reaches zero, the rover still moves in the directions indicated in the simple arrow-key scripts, but not in the directions controlled by the When flag clicked scripts. The timer stops at zero, and any additional game elements stop working as well. (The sample tube sprites no longer disappear if the rover is able to reach them using the simple arrow-key scripts.)
  11. Students can explore additional actions they would like to have happen when the timer reaches zero. If their additional actions are not working, it could be because the actions are set to occur at the same time as the stop all script. Students will need to make sure their additional actions occur before the stop all script, as in the example shown. In this case, the script begins running with 1 second remaining, and stops after waiting for 1 second.

Section 4 - Add a Scoring System (10-20 minutes)

Though rover missions on Mars don’t have a scoring system, the science collected is valuable, and some discoveries may be more exciting, interesting, or important than others. To determine how well students are doing when playing the game, they can develop a scoring system. How points are totaled is up to the students, but they may want to include points for reaching the sample collection tubes as well as collecting them. Students may also include code that reduces point totals in certain scenarios.

  1. Click the rover sprite. Ask students which Events script they should use to start the scoring code when the game is started. They should select the When flag clicked script and drag it to the workspace.
  2. If students have already created a timer (Section 3), clicking on Variables scripts will reveal the timer scripts. In this case, students will need to make a score variable that will change as the game progresses. They should click Make a Variable and name it “score.” Select the For all sprites option to make this variable available for use with other sprites. This will add a selection of scripts to choose from, all related to the score variable.
  3. Ask students which score Variables script they should use to start the game. Recalling the previous variable, they will likely identify set score to __ as the correct script.
  4. Ask students which script they should use to add a point to the score. Recalling the previous variable, they will likely identify the change score by __ script. Next, ask them where that script should go. Remind them that they will want to give their players a point if they reach a sample collection tube. This should provide a hint that they need to add this script to the sample collection tube sprites (sample_tube). Because there is already a set of scripts defining what happens when the rover reaches a sprite, that set can be modified to include the change score by __ script. Ask students where they think the script should go. Because the score should change if the sprite is touched by the rover, the script should go within the if-then script.
  5. Allow students to run their game and observe their score change. The change score by __ script will need to be added to each sample collection tube sprite individually. If students find that their score doesn’t change when they reach a sprite, check to see if they forgot to add the change score by __ script to each sprite. If a collection tube sprite is placed over a particularly interesting spot on the map, or is especially difficult to get to, students may choose to reward players by making those targets worth more than one point.
  6. To make the rover report that it has collected a sample tube, students will have to click on the rover sprite and create scripts that will send a message that a tube has been collected. Ask students what blocks might allow them to do this. There are several ways to achieve this, but one example is provided below.

Section 5 - Add Hazards (10-20 minutes)

Space and alien worlds are fraught with hazards. From steep slopes and deep sand to sharp rocks and high radiation, there are countless ways a mission can be delayed or end unexpectedly. Students should create code that produces hazards players will need to avoid.

Note: Students who have completed a Mars surface features activity may have an easier time identifying hazards (e.g., craters, gullies) in the backdrop images, but those activities are not required to complete this programming challenge. Generally, potential hazards can be identified by sudden changes between light and dark colors in the backdrop images, indicating a shadow caused by a steep slope.

  1. Students should study the backdrop images to identify potentially hazardous areas.
  2. Students can mark these potentially hazardous areas on their backdrop. Start by clicking on the backdrop thumbnail and selecting the Backdrops tab.
  3. Have students select a color that does not already appear on their sample tube sprite. They will create code that causes something negative to happen if their rover sprite touches that color, so it’s important that players don't confuse it with the sample tubes.

    Students may be tempted to select colors in the images to make the hazards more difficult to see on the backdrop, but this should be avoided. If the rover drives over an area of that color, even if it’s part of the original backdrop image, it will produce a negative action for the game player. Students who wish to make hazards invisible can explore code that makes that possible later in this section.
  4. Students can draw straight or freehand lines, circles, or other shapes to outline hazards.
  5. Next, students should click the rover sprite and the Code tab to create the code that will interact with the hazards they drew.
  6. Ask students which Events script they should use to start the hazard code when the game is started. They should select the When flag clicked script and drag it to the workspace.
  7. Ask students how they could make a script that would run throughout the game and cause something to happen when certain conditions are met. They may realize that the if-then Control script can be placed inside the forever script before they define the "if" and "then" portions of their statement.
  8. Students should recognize the need for a hexagonal block. Ask them to identify which block from the Sensing scripts they think will be needed for this script. Touching color _ is the block needed for this script. Once the block is inserted, students should click the color box, click the color picker icon at the bottom of the menu, move the pointer over the hazard color in the Stage window, and click to select that color in the touching color _ script.
  9. Students must now decide what will happen if a hazard color is touched. It could be that a point is subtracted, time is subtracted, the game ends, or a variety of other options. In the example shown here, change score by __ is used to define the "then" portion of the statement, which will reduce the score when a hazard color is touched. As written, the code will continue reducing the score as long as the rover is touching that color. Challenge students to create code that will reduce the score only once each time the rover touches the hazard color.
  10. Students may decide to give different hazards different penalties. There are a variety of ways to code that into the game. Here are some examples:
  11. Students can explore ways to make hazards harder to detect by creating code that causes something negative to happen if the rover drives over specific x and y coordinates or a range of coordinates. Challenge students to explore the scripts on their own to make this happen. Students may even explore code that could impact the rover based on random numbers generated by the game.

Section 6 - Add Landing Code (10-20 minutes)

Every rover on Mars had to first land on the Red Planet before it could begin exploring. Students can create a landing site by creating scripts that place the rover at a specific location at the beginning of the game.

  1. Click the rover sprite. Ask students which Events script they should use to start the landing code when the game starts. They should select the When flag clicked script and drag it to the workspace.
  2. Have students explore the Motion scripts and pick which one they should use to move the rover to a specific location on the map when the game is started. They should choose go to x:__ y:__ and connect it to the When flag clicked script.
  3. They will need to identify a starting spot for the rover using the x and y coordinates on the backdrop. As students move the mouse on the backdrop, the x and y coordinates will be shown below the lower-right corner of the backdrop. Enter the desired coordinates into the go to x:__ y:__ script.

Note: Students should be careful not to select a starting point for the rover on a game-ending hazard. They should also not start the rover behind an impassable hazard. Students may need to adjust the speed of the rover or change the total time for the game depending on how far from the sample tubes the rover will be when the game begins.

Mission planners can pick a general area where they want a rover to land, but there is still a level of uncertainty about the exact spot where the rover will touch down within that area. Students can create code that will place the rover at a random location within a landing area.

  1. Instead of entering coordinates into go to x:__ y:__, students can drag the pick random__ to__ into the x and y coordinate spaces. Allow students to explore what happens when different values are entered into each field. Entering -240 to 240 for x and -180 to 180 for y allows the rover to start at any location on the backdrop, including on potential hazards that have been programmed into the game. Choose a smaller range to limit the possible starting points for the rover.

Discussion

  • The backdrop images used in this activity were taken by an orbiting satellite high above Mars. From that distance, rovers would appear as small dots in the images. For visibility and functionality, the rover sprite is much larger than it would appear if it were actually shown in any of the images.
  • Mars is far away from Earth. It is so far that driving with a joystick (or with cursor keys, like in this game) is not possible. There is actually a delay of several minutes when rover mission operators send commands between Earth and Mars, so operators send an entire set of commands for the rover to perform throughout its day.

Assessment

  • Does the game do what it’s supposed to as described by the game creator?
  • Is the game winnable (i.e., can players complete the tasks, or was the game designed to frustrate players or be unbeatable)?
  • Did the game creators use coding to eliminate potential ways that players can “cheat”?
  • Refer to the coding rubric.


Extensions

  • Instead of including scripts to control the rover in their game, have students create a backdrop with hazards and collection tubes and set a landing location. Then, challenge them to create code so players of the game can input a series of instructions (e.g., inputs) to have the rover drive and collect the sample tubes while avoiding hazards.

Explore More

Check out these related resources for kids from NASA Space Place: