Exploring the PicoBoard sensors

Keep Moving

After the sprite moves, the script uses four more if () blocks to check the position of the sprite on the stage. To understand these four conditionals, you need to remember that the Scratch stage has the following boundaries: the left edge is x=-240, right edge is x=240, bottom edge is y=-180, and top edge is y=180.

The position checks in the four if () blocks are straightforward. As soon as the sprite passes the boundary coordinate, it will be repositioned to the same place on the opposite side of the stage using the set x to () or set y to () blocks. Here's an example: If the sprite moves off the stage at the (x,y) coordinates of (240,10), the script will reposition the sprite to (-240,10), and it will continue to move.

In many Scratch projects, you'll see the if on edge, bounce block used to send the sprite back into the center of the stage. The script in Figure 4 provides an alternative way to handle the sprite on the edge of the stage and can be used in projects everywhere.

The overall movement shown in Figure 4 can be added to a variety of games, allowing the player to do things such as navigate a maze, dodge balls, or shoot other sprites.

But Wait, There's More

The PicoBoard includes additional sensors for sound and a push button, but I'll save those for a future project. If you're looking for some quick ideas to introduce those sensors, however, I like to use the sound sensor to "sync" my voice to a script's action on the stage, and the push button could be used to set up a hearing test. Happy Scratchin'.

The Author

Michael Badger authored the Scratch (1.4 and 2.0) Beginner's Guide series from Packt Publishing. Learn more and get this project source at http://scratchguide.com.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content