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

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!

What You Need & Getting Ready
- SPACEBLOCK Standard Package [SPACEBLOCK board, USB cable, battery box]
- Infrared motion sensor module (motion sensor)
- 8×8 RGB LED matrix panel (LED panel)
- 180° micro servo motor (servo motor)
- Jumper wires [3 pin header–pin socket wires × the number of wires you want to extend]
- A computer for programming

Connect the Expansion Parts to SPACEBLOCK

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.

Glue green construction paper on to make it look nice, and put the battery box and the SPACEBLOCK board 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.

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


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.

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

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.

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.

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.

That's it for the program. Write the program to SPACEBLOCK in standalone mode, put it back inside the crocodile, and start playing!
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%.



The Crocodile Game Is Finished!


