Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Fun4 Eat Apple
In this project, we use an infrared obstacle avoidance module to guide the Beetle sprite to an apple.
After clicking the green flag, swiftly pass your hand over the left obstacle avoidance module (block and quickly remove your hand from in front of the module) to initiate Beetle’s rotation. Once it aligns properly, sweep your hand over the module again to move Beetle forward, ensuring it avoids the black lines on the map. If adjustments or turns are necessary, swipe your hand over the module again to rotate Beetle to the desired angle. Repeat these steps until the Beetle sprite reaches the apple.
Below are the steps for implementing the project. It’s recommended to follow these initially, and once familiar, you may modify the effects as you wish.
1. Draw the Maze Backdrop
Here, we will draw a map backdrop with a red apple on it.
First, select a blank backdrop.
Click Backdrops to start drawing the map backdrop. First, rename the backdrop to Maze.
Use the Line tool, set the color to black, width to 4, and start drawing the map. You can design your map based on your ideas; it doesn’t have to be like mine.
Now, draw an apple. Using the Circle tool, draw an ellipse or circle in red with no outline.
Note
You can remove the outline by clicking the Outline window and then using the Remove tool.
Select the Brush tool and choose a color and width to finish drawing the apple.
2. Draw the Win Backdrop
Now start drawing; refer to the steps below, or draw your backdrop based on your creativity, ensuring it represents a victory.
3. Scripting for the Maze Backdrop
Ensure the backdrop is switched to Maze every time the game starts.
4. Select the Beetle Sprite
Delete the default sprite and select the Beetle sprite.
Place the Beetle sprite at the entrance of the Maze backdrop, noting the x, y coordinate values at this point, and resize the sprite to 40%.
5. Scripting for the Beetle Sprite
Now, write a script for the Beetle sprite to move forward and change direction under the control of the left obstacle avoidance module.
When the green flag is clicked, set the Beetle angle to 90, and the position to (-124, -113), or use the coordinate values of your placement.
Create the variable flag and set its initial value to -1.
Next, within the [forever] block, use four [if] blocks to manage different scenarios.
If the left infrared is blocked, use the [mod] block to toggle the variable flag between 0 and 1 (alternating between 0 for this press and 1 for the next).
If the flag is 0 (left infrared is blocked), let the Beetle sprite turn clockwise. If the flag is 1 (left infrared is blocked again), the Beetle moves forward. Otherwise, it continues turning clockwise.
If the Beetle sprite touches black (the black line on the Maze backdrop), the game ends, and the script stops running.
Note
Click on the color area in the [Touch color] block and select the eyedropper tool to pick up the color of the black line on the stage. If you choose a random black, this [Touch color] block will not function correctly.
If the Beetle touches red (also use the straw tool to pick up the red color of the apple), the backdrop switches to Win, indicating the game is successful, and the script stops running.
Programming complete, you can now click the green flag to run the script and see if it achieves the desired effect.