[No.16] Test Your Luck with the Crocodile Game!

Programming and Making

Summer vacation is here! Let's make a fun game to liven up your playtime! Put your hand in the crocodile's mouth and... it might snap shut on you! It's a slightly thrilling game. It's also great as a summer vacation programming project!

Elementary School ~ Intermed. 80 min
Here's what the crocodile game looks like! You could also remake it as another animal or a monster

What You Need & Getting Ready

Top row, from left: SPACEBLOCK Standard Package, 8×8 RGB LED matrix panel, jumper wires. Bottom row, from left: 180° micro servo motor, motion sensor module

Connect the Expansion Parts to SPACEBLOCK

First, let's connect the LED panel, the motion sensor and the servo motor to SPACEBLOCK.
The whole setup

Check the details of the connections in the next picture.

Design a Crocodile Mouth That Closes When You Put Your Hand In!

What we want to build this time is a crocodile mouth that snaps shut when you put your hand in it. For the input device, we'll use a motion sensor that can detect movement. For the output device, we'll use a servo motor that can turn from 0° to 180°. The motion sensor detects a hand in the crocodile's mouth, and the servo motor's movement opens and closes the mouth.

The crocodile's body is a milk carton, and the mouth is made from thick paper. The upper jaw is attached to the body with a split pin, so it closes naturally under its own weight. Attach a straw to the servo motor so it's long enough to push the upper jaw up.

Make the basic shape from a milk carton and thick paper

Glue green construction paper on to make it look nice, and put the battery box and the SPACEBLOCK board inside the milk carton body.

Decorated in green. The battery box and board go inside the milk carton body

Next, add teeth, eyes, a tail, arms and legs. Attach the motion sensor to the upper jaw and the servo motor to the lower jaw.

Eyes and teeth make it look like a real crocodile

Once the upper jaw is attached with the split pin, the crafting part is done.

The servo's angle makes the upper jaw close and open. Tuck the wires firmly inside the body so they don't get in the way of the servo or the upper jaw
Finished. The motion sensor checks whether a hand has been put in the crocodile's mouth. It's handy to make a hole where you can reach the battery box switch

Make a Program Where You Never Know If It Will Close!

The program we want to make this time is one where "sometimes the crocodile's mouth closes, and sometimes it doesn't". We need a program where you can't tell ahead of time whether the mouth will close or not.

What are random numbers?

When a program needs a value you can't predict, like rolling a die, it uses "random numbers". When you can't tell what will come up, we call it random.

[random integer from (0) to (10)] Use this block to get a random integer within the range you choose. Just like rolling a die gives you a number from 1 to 6, every number in the range has the same chance of coming up.

The random number blocks are in the "Math" category

Create a variable

Create a variable named "random" to hold the random number.

Create the variable "random"

A program that sets the servo's starting position

First, as shown in the next picture, make a program that sets the servo to 0° as soon as the program starts.

Set the servo position first, and keep the program running

A program that runs when movement is detected

Now make the program that runs when you put your hand in the crocodile's mouth. Let's start with the overall flow.

As shown in the next picture, the [When the voltage of (A0) (rises)] block keeps checking the motion sensor. When you put your hand in the crocodile's mouth, the sensor detects movement, the voltage goes up, and the program starts running. First, a random number is made, and an integer from 1 to 10 goes into the variable "random." Then, if the value is 3 or less, the crocodile's mouth closes. Otherwise, the mouth stays open.

The overall flow of the program that runs when you put your hand in the crocodile's mouth

Next, add LED effects to this program. As shown in the next picture, when a hand is detected in the crocodile's mouth, a countdown appears. If you get eaten, an "×" appears, and if you escape, an "○" appears. Also, after the result, add a 5-second wait so the motion sensor doesn't detect the mouth opening or your hand pulling out.

Add the LED effects and the program is finished

That's it for the program. Write the program to SPACEBLOCK in standalone mode, put it back inside the crocodile, and start playing!

Point: What are the chances of getting eaten?
In this program, we make a random integer from 1 to 10, and if it's 3 or less, the crocodile eats you. If the random number is 1 to 3, you get eaten, and if it's 4 to 10, you escape.The chance of getting eaten is 30%, and the chance of not getting eaten is 70%.
So, what should you do to raise the chance of getting eaten to 50%? Change the condition in the [if ( ) then] block so the variable "random" is 5 or less. Then you get eaten when it's 1 to 5, so the chance becomes 50%.
To make the chance of getting eaten 50%, you could also make a random integer from 1 to 2 and get eaten when it's 1.
QR code for project

The Crocodile Game Is Finished!

Put your hand in the crocodile's mouth and...
The countdown starts and...
Are you safe? Or will it snap you up?
Watch the video!
You can use the crocodile game instead of rock-paper-scissors to decide who goes first or who picks a snack first. It's also fun to play every morning to test your luck for the day! If you secretly change the program, you can make the chance of getting eaten higher or lower!!