PIPER MAKE

EDUCATOR GUIDES


YOU ARE HERE

Gaming expedition

Mission 2 of 5

Gaming expedition: Mission 2 of 5

Frog Frenzy


Advanced

45 minutes

Grades 3 - 8

MISSION OBJECTIVE

Learn how to build a Game Controller and program the code for a game that uses (x,y) coordinates.

The Earth team has lost connection to a mining ship on Pipax, but our scientists have scrambled to get a primitive view and mining control back up! In this mission, code a game where ore randomly appears and use your Controller to play.

View student interface at make.playpiper.com
MISSION CHARACTERS

Scientist

Commander
MISSION MATERIALS

Computer with USB port and Chrome or Edge browser
Piper Make Base Station or Starter Kit
Game Controller

MISSION RESOURCES

Learning Goals

  1. Students will create hardware using tools such as a screwdriver.
  2. Students will review key electronics understandings, including wire and pin positions for specific inputs and outputs.
  3. Students will practice the computational concepts of
    • Loops: Running the same sequence multiple times.
    • Events: While a pin’s condition is on or off, another action happens.
  4. Students will create programs that use variables to store and modify data.
  5. Students will create programs that include events, loops, and conditionals.
  6. Students will practice organizing problems into smaller, manageable tasks.
  7. Students will create programs by incorporating smaller portions of existing programs to develop something new or add more advanced features.
  8. Students will test and debug a program or algorithm to ensure it accomplishes the intended task.

Career Connections

Software Quality Assurance Analyst: Salary $99,620/yr
Web and Digital Interface Designer: Salary $83,240/yr
Computer User Support Specialist: Salary $71,530/yr
Video Game Designer: Salary $83,240/yr

Hardware Diagram




Code Diagram




Tutorial Steps

Frog Frenzy

Hmm, it looks like we need to build the Controller first.
Once we build the Controller, we can connect it and use it to navigate the Rover on Pipax!
Excellent, let's get going!
Follow the build instructions for your Game Controller.
Not sure what version you have? Take a look at the image below:

Hmm, it seems like we need to connect that Controller PCBA to the Pico. Let's unplug our Pico while we build!
NOTE: The Controller wires may not be the same color as the picture below. Be sure to match the pin labels to the GPIO shown.
Double-check all of your connections. Once you are sure that they are correct, go ahead and plug your Pico into your computer. Then, click NEXT.

Ooh, I think this Controller uses a shift register!
Shift registers read inputs all at the same time, and then shift them out and to the Microcontroller:
And then the LATCH pulse gets the Pico to read all of the pressed buttons as 1s and 0s, each called a bit. 8 bits is a byte!
Hmm, that CLOCK pulse seems to send the bits to the Pico in order until the byte is complete.
Your Controller has 14 buttons, so there are two shift registers to capture all of them.

Click NEXT.

The first part of the program needs to setup your Controller. Just like we do with all programs, we need to begin by dragging out a start block from the Chip menu. Then, grab a setup controller block from the Sensing menu and connect it to the start block.

This block tells the Pico that a Controller is connected to it, and it tells the Pico which pins it's connected to.

Choose the following options for the pin variables on the setup controller block:
  • data pin: 2
  • latch pin: 3
  • clock pin: 4
Click NEXT.

Let's build some quick test code to ensure our Controller works.

Grab a repeat forever block from the Loops menu and connect it below the setup controller block. Then, grab an if do else block from the Logic menu and place it inside the repeat forever block.

Change the 0.5 on the repeat forever block to 0.1. Next, grab a read controller buttons block from the Sensing menu and place it into the repeat forever block.

Now, grab a was controller block from the Sensing menu and place it into the input of the if do else block. Drag out a turn pin block from the Chip menu and place it into the first do of the if do else block. Change the pin to 25 (LED).

Right-click the turn pin block and select duplicate. Move the new block into the else do part of the if do else block. Change it from ON to OFF:
Once your program is ready, click CONNECT and then click START. While your program is running, pressing the UP button turns the LED on the Pico on, and releasing it turns it off. If your test code works, click STOP.

Delete the repeat forever block and all the blocks inside it by dragging them to the trash can.
You just clicked STOP, and usually your Pico would stay connected. However, this time it disconnected itself. Why is that?

There are a few devices and pieces of code that require a full reset of the Controller when you are done running them. This clears some memory inside the Pico that the Controller was using so that it's ready to go again the next time you need it.

Because the Controller utilizes additional resources inside the Pico, you may need a few troubleshooting tips if your Pico stops connecting or running the programs properly on your computer.

Here are some things you can try if something doesn't work correctly:
  • Check your program and wiring to make sure they are correct.
  • Unplug your Pico, wait 5 seconds, and plug it back in.
  • Refresh your browser page.
Ready to code the rest of your Controller's program? Click NEXT.

We are going to make a game that we can play using the CONSOLE.

Our game will be simple - we will be able to move our character around using the Controller. The goal is to obtain the food while an enemy character chases our character. That's it, but it will be a lot of fun to play!

Our game will need to keep track of the positions for 3 things: our character, the food, and the enemy. Each character will have a horizontal position (left-right), which we will call x, and a vertical position (up-down), which we will call y. This means that we will need 6 variables to keep track of the positions of our 3 characters.

We will also need to keep track of the game's score. Our characters will gain one point every time they reach the food, and they will lose one point if the enemy catches them.

Let's create all 7 variables. Click the Variables menu, and then click the Create variable... button. You'll do this 7 times, and make each of the following variables:
  • player x
  • player y
  • food x
  • food y
  • enemy x
  • enemy y
  • score
Once you have created all of your variables, click NEXT.

We don't want our enemy to start on top of our player. So let's set the starting places.
Click the Variables menu and grab the set block. Connect it below the setup controller block. Ensure the variable in that block is set to score. Grab a 0 block from the Values menu and place it into the set score block.

Right-click the set score block and select duplicate. Place the new block right below the set score block. Change the variable in the new block to "player x". Do this again and change the new block's variable to "player y". Repeat this two more times and set the two new blocks to "enemy x" and "enemy y". Finally, change the value in the enemy x block from 0 to 15, and change the value in the enemy y block from 0 to 9:
Now, where should we place the food? We want it to change up so we can chase it. Drag out a set block from the Variables menu and connect it to the end of your program. Change the variable in the block to "food x". Do this again and change the variable on the new block to "food y".

Next, grab a random integer block from the Values menu and place it into the set food x block. Change the 1 to 0, and change the 100 to 15. Grab another random integer block and place it into the set food y block. Change the 1 to 0, and change the 100 to 9:
Click NEXT.

The code for almost every game uses a loop. Inside the loop, the game's code usually calls a few functions. Usually, the loop calls functions like this:
  • read the inputs
  • move the characters
  • check for collisions or hits
  • check for scoring
  • display the characters and score
  • repeat!
Makes sense, right? That means we need to start with a loop. Drag out a repeat forever block and connect it to the end of the program. Then, grab a to do something block from the Functions menu and place it off to the right side of your program. Change the name of your new function to "read controller":
Grab a read controller buttons block from the Sensing menu and place it into the to read controller block. Then, grab an if do block from the Logic menu and place it below the read controller buttons block. Next, grab a was controller block from the Sensing menu and place it in the input of the if do block:
Click NEXT.

Before adding more blocks, it's important to understand how the Console works. Usually, when you use a print block, it sends letters or numbers to the Console. But did you know that you can tell the Console where you want those letters and numbers printed?

Look carefully at the image below. The Console has columns and rows. We sometimes refer to the column as how far left or right a letter or number is in the Console, and we sometimes say that is the letter or number's x-position.

The Console also has rows, referring to how far up or down a letter or number is in the Console. This is also called the y-position.

When we write the position of a letter or number, we write it as two numbers like this: "(x,y)"
Console positions start in the top-left corner and increase as you move right and down.

Click NEXT.

Now, we want our Controller to control our player's movements. Let's set that up.

When the up button is pressed, we want the player character to move up 1 position, which means that we need the player y variable to decrease by 1. We also want to ensure that the player y variable never falls below 0 or exceeds 9, so that it stays inside the Console. We will do this by subtracting 1 from the variable and using a constrain block to keep the value from going out of bounds.

Drag out a set block from the Variables menu and place it into the if do block. Change the variable to player y. Next, grab a constrain block from the Logic menu and place it in the input of the set player y block. Then, grab a _ + _ block and place it into the first input of the constrain block. Grab a player y block from the Variables menu and place it into the left side of the _ + _ block. Grab a 0 block from the Values menu, place it into the right side of the _ + _ block, and change the 0 to a 1. Next, change the + (plus) to a - (minus). Finally, change the 50 in the constrain block to 9:
Now, we can copy our if do block for the other 3 buttons and make a few changes.

Right-click the if do block and select duplicate. Place the new if do block under the first one. Next, change the up button to down button, and change the - (minus) to a + (plus).

Then, duplicate the first if do block again and attach it below the last if do block. This time, change the up button to left button, change the two player y variables to player x, and change the 9 to 15.

Finally, duplicate the last if do block and attach the new block below the last if do block. Change the left button to right button and change the - (minus) to a + (plus):
Right-click the to read controller block and select Collapse Block. Then, grab a read controller block from the Functions menu and place it into the repeat forever block:
Click NEXT.

Right now, our enemy is just standing there. Not a great foe, right? Let's write a function to make it chase us!
Grab a to do something block from the Functions menu and place it to the right side of your program. Change the name of the function to "move enemy". Drag an if _ = _ do block out of the Logic menu and place it in the to move enemy block.

Click the blue gear icon. In the mini-workspace that opens up, grab the else if block and connect it below the if doblock to add an else if. Then, click the blue gear icon to close the mini-workspace.

Grab an enemy x block from the Variables and place it on the left side of the _ = _ block. Then, grab a player x block from the Variables and place it on the right side of the _ = _ block. Change the = (equals) to a < (less than).

Finally, grab a change block from the Variables menu and place it into the first do of the if do block. Change the variable to enemy x, and change the 1 to 0.5:
Why 0.5? We want the enemy to move more slowly than the player can. If you want to make the game more challenging, you could increase this to 0.75 or even 1, or make it slightly less than 0.5 to make the game easier.

Right-click the _ < _ block and select duplicate. Place the new block into the else if input. Change the < (less than) to > (greater than).

Right-click the change enemy x block and duplicate it. Place the new block into the other do of the if do else if block. Change the 0.5 to -0.5 (negative):
Right-click the if do else if block and duplicate it. Place the new block right after the first if do else if block. Change all of the enemy x variables in the new block to enemy y, and change all of the player x variables in the new block to player y:
Right-click the to move enemy block and select collapse block. Then, grab a move enemy block from the Functions menu and place it into the repeat forever block:
Click NEXT.

Our next function needs to check if the enemy has caught the player. If the enemy's position is the same as the player's position, then that means the player has been caught and loses a point. After the enemy catches the player, we also want to reset the game, so let's start there.

Grab a to do something block from the Functions menu and place it to the right side of your program. Change the name of the function to "set up game".

Grab the repeat forever block and disconnect it from the program by dragging it straight down a little. Next, grab the set player x right after the set score block near the beginning of your program and place it into the to set up game block:
Next, drag out a set up game block from the Functions menu and connect it below the setup controller block. Then, grab the repeat forever block you moved earlier and connect it below the set up game block. Finally, right-click the to set up game block and select collapse block:
Now, grab a to do something from the Functions menu and place it to the right side of your program. Change the name of the function to "check enemy". Then, grab a check enemy block from the Functions menu and place it into the repeat forever block:
Click NEXT.

Grab an if do block from the Logic menu and place it into the to check enemy block. Next, grab an and block and place it into the input of the if do block. Then, grab a _ = _ block from the Logic menu and place it into the left side of the and block.

Grab a player x block from the Variables menu and place it into one of the inputs of the _ = _ block. Then, grab a round block from the Values menu and place it into the other side of the _ = _ block. Change the round to round down. Finally, grab an enemy x block from the Variables menu and place it into the input of the round down block:
Right-click the _ = _ block and select duplicate. Place the new blocks into the other side of the and block. Change player x to player y and enemy x to enemy y in the new blocks.

Grab a change block from the Variables menu and place it into the if do block. Make sure it is set to the score variable and then change the 1 to a -1 (negative). Finally, grab a set up game block from the Functions menu and connect it below the change score block:
Right-click the to check enemy block and select collapse block. Then, click NEXT.

Now, our game needs a function that checks to see if the player has reached the food.

If the player's position is the same as the food's position, then the player has obtained the food and earns a point. After the player receives the food, we also want to reset the game.

Grab a to do something from the Functions menu and place it on the right side of your program. Change the name of the function to "check food". Then, grab a check food block from the Functions menu and place it into the repeat forever block:
Grab an if do block from the Logic menu and place it into the to check food block. Next, grab an and block and place it into the input of the if do block. Then, grab a _ = _ block from the Logic menu and place it into the left side of the and block.

Grab a player x block from the Variables menu and place it into one of the inputs of the _ = _ block. Then, grab a food x block from the Variables menu and place it into the other side of the _ = _ block.

Right-click the _ = _ block and select duplicate. Place the new blocks into the other side of the and block. Change player x to player y and food x to food y in the new blocks:
Now, grab a change block from the Variables menu and place it into the if do block. Make sure it is set to the score variable and then change the 1 to a -1 (negative). Finally, grab a set up game block from the Functions menu and connect it below the change score block:
Right-click the to check food block and select collapse block. Then, click NEXT.

We only have one function left, and that's the function that prints everything out to the CONSOLE.

Grab a to do something block from the Functions menu and place it to the right side of your program. Change the name of the function to "print game". Then, grab a print game block from the Functions menu and place it into the repeat forever block:
The first thing our function needs to do is clear the CONSOLE of anything printed in it before. Grab a console clear block from the Chip menu and place it in the to print game block.

Next, we need to print out each character. We can do this by setting the cursor position first and then printing a letter for each character.

Grab a console set cursor position block and connect it below the console clear block. Delete the two 0 blocks in the console set cursor position block by dragging them to the trash can.

Now, grab a player x block from the Variables menu and place it into the x input of the console set cursor block. Then, grab a player y block and place it into the y input. Next, grab a print without new line block from the Chip menu and connect it below the console set cursor block. Finally, type an O into the "_" block:
What's the difference between a print block and a print without new line block? The print block sends letters and numbers to the Console, and then it sends a Return/Enter right after. When we use the console set cursor position block, that Return/Enter can mess up the Console. That's why we want to use a print without new line block instead.

Let's print the food next. Right-click and duplicate the console set cursor block and connect it below the print without new line block. Change the player x and player y to food x and food y. Then, right-click and duplicate the print without new line block and connect it below the console set cursor block. Finally, change the O to a *:
Click NEXT.

All that is left is printing the enemy. Right-click and duplicate the console set cursor block, and connect it below the last print without new line block.

Delete the food x and food y by dragging them to the trash can. Then, right-click and duplicate the print without new line block and connect it below the last console set cursor block. Change the * to an X:
Grab a round block from the Values menu and place it into the first input of the last console set cursor block. Change the round to round down. Do this again for the other input.

Next, grab an enemy x block from the Variables menu and place it into the first round down block. Then, place an enemy y block into the second round down block:
And finally, let's print our score!

Grab a console set cursor block from the Chip menu and connect it below the last print without new line block. Change the first 0 to 18 and change the second 0 to 5. Then, grab a print without new line block from the Chip menu and place it below the last console set cursor block.

Now, type "Score: " into the "_" block. Notice the extra space at the end of "Score: ". Then, grab another print without new line block from the Chip menu and place it below the last print without new line block. Delete the "_" block. Then, grab a score block from the Variables menu and place it into the print without new line block:
Right-click the to print game block and select collapse block. Then, click NEXT.

Click CONNECT, and then click START. Look out! The Enemy is chasing your character! Use your Controller to stay away from the Enemy and get to the Food.

How high of a score can you get?

When you are finished playing your game, click STOP and then click NEXT.
When you're ready to try some new tutorials or projects, click EXIT to go back to the start screen.

Blueprint

OPEN IN BLUEPRINT VIEWER  DOWNLOAD

Have an different version? Look for more information on the Support Page.

Troubleshooting Tips

  • Is your Pico disconnecting after you test the Controller?
    • Check the program and wiring to make sure they are correct. The wires sent with your Game Controller might be different colors than depicted, which is fine. Follow the diagram to make the right connections.
    • Unplug your Pico, wait 5 seconds, and plug it back in.
    • Refresh your browser page.

Our customer support specialists are on hand to ensure your implementation of Piper runs seamlessly.
View Support Docs or Contact Support
EXPANDED RESOURCES

Term Glossary


Console Personal computers built for gaming (e.g., PlayStation, Xbox, Nintendo Switch).

Modding Making "modifications" to how a game sounds, plays, looks, or functions, usually by users.

Graphic User Interface (GUI) A way to display information on the computer screen where the user can use a mouse or touchscreen to click various buttons. (CAT)

Playtesting Playing through new inventions or builds to find bugs, test their ease of use, and get new ideas to build on.

Virtual Reality (VR) Computer technology that makes someone feel like they are elsewhere. It requires a player to wear a headset and use input devices to play games.

View Full Glossary

Standards Alignment