O'Reilly logo

Raspberry Pi For Dummies by Mike Cook, Sean McManus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making Sprites Move Automatically

If you’re wondering why we left the alien’s movement to the end, it’s because it makes it easier to test the game. We’ve now got a spaceship that the player can move, a working firing mechanism, and an alien that dies and then regenerates when shot. We can test all that at our leisure and fix any problems without worrying about having to keep up with the alien.

Our alien moves from left to right and then from right to left, and then back again. Each time it changes direction, it moves down the screen a little bit. This is quite sophisticated behavior, but you can create most of it using the blocks you’ve already used this chapter. The art of programming is partly about working out how you use the different blocks or commands at your disposal to achieve what you want to.

To start, we need to make a new variable, which we’ll call leapsize. Each time Scratch goes around the alien’s Forever loop, it moves the sprite and then checks whether it’s touching a fireball or the spaceship. The leapsize variable is used to store how much the alien’s X position should change by each time. If the alien is going right, the leapsize variable is 20, and if it’s going left, it’s -20.

warning_bomb.eps When you create the leapsize variable, you are asked whether this variable should apply to all sprites or to this sprite only (the alien). Make sure you click the button to make it apply ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required