Introduction to Programming

Activity 2: Sensing the Environment

Lesson 9: Beetle in a Maze

The beetle can now move — but it can walk right through the maze walls! In this activity, we will use sensing blocks to detect when the beetle touches the walls and stop it from passing through them. We will also detect when the beetle reaches the exit!

Sensing Blocks in PictoBlox

The Sensing palette in PictoBlox contains blocks that allow sprites to detect their environment — including touching other sprites, colors, or the edges of the Stage.

Key Sensing Blocks

BlockWhat it Detects
Touching (sprite)?Whether the sprite is touching another specific sprite
Touching color ()?Whether the sprite is touching a specific color on the Stage
Color () touching ()?Whether a color on the sprite is touching another color on the Stage
Distance to (sprite)The distance between the sprite and another sprite

Detecting Wall Collisions

We will use the 'Touching color?' block to detect when the beetle touches the wall color of the maze. When it does, we will move it back to prevent it from passing through.

  • Identify the wall color of your maze (e.g. black or dark blue).
  • Add a 'When flag clicked' block with a Forever loop.
  • Inside the loop, add an 'If (touching color [wall color]?) Then' block.
  • Inside the If block, add a 'Move -5 steps' block to push the beetle back.
  • This will prevent the beetle from passing through walls.
Block coding script showing when flag clicked, forever loop with if touching color black then move -5 steps

Detecting the Exit

Now let's detect when the beetle reaches the exit. We will mark the exit with a distinct color (e.g. green) and use the 'Touching color?' block to detect it.

  • Color the exit of the maze with a distinct color (e.g. green).
  • Inside the Forever loop, add another 'If (touching color [exit color]?) Then' block.
  • Inside it, add a 'Say (You made it! Well done!) for 3 seconds' block.
  • Optionally, add a 'Stop all' block to end the program when the exit is reached.
Block coding script showing if touching color green then say You made it Well done for 3 seconds then stop all

Running the Game

  • Click the green flag to start.
  • Use the arrow keys to navigate the beetle through the maze.
  • Try to reach the exit without touching the walls.
  • The beetle will be pushed back if it touches a wall, and it will celebrate when it reaches the exit!
Save your file with the name Beetle in a Maze — Final. You have built your first game with collision detection!

Subscribe to our newsletter.

Get updates to news and events.