Program the App Behaviour

Code the sprite to hop around the screen.

Step 1

To start programming the behaviour of the app, go to the Blocks editor. 

To move the sprite around randomly as time goes by, program a Clock based event.

From the Blocks palette, click the Clock1 drawer to open it. Drag and drop the when Clock1.Timer block onto the Viewer. In Designer, we set the TimerInterval to 500 milliseconds, which means this when Clock1.Timer block will be triggered every half-second.

Step 2

To make the Sprite move, go to the Sprite drawer and find the call Sprite.MoveTo block. Snap it to the when Clock1.Timer block.

It has two empty slots for x and y: these are the coordinates of the Sprite on the screen. Go to the Math drawer, pull out a number “0” block and change it to “150”. Copy this block and snap both to the empty slots of x and y.

Step 3

The Canvas is 300 x 300 pixels, so coordinates (150, 150) are in the middle of the screen. This code will make the Sprite move to the middle of the screen after half a second. 

Try it on the phone now! Change the coordinates to make the Sprite move to a new place.

Step 4

So that makes the sprite move to one location, but we want it to hop all over the screen. 

  • Go to the Math drawer
  • Pull out a random integer from “1” to “100” block
  • Throw the old coordinates in the Trash 
  • Snap the random integer block to the empty slots
  • Take the upper value of “100” for both x and y and put them in the Trash too
  • Open the Canvas1 drawer 
  • Place the green Canvas1.Width block in the empty slot for x 
  • Place Canvas1.Height in the empty slot for y

This means your sprite will never move off the screen.

Check the phone! Your Sprite will be randomly hopping all over the screen!

Step 5

Program the phone to play the sound and vibrate when the Sprite is touched.

  • Go to the Sprite drawer 
  • Pull out a when Sprite.Touched block
  • Open the Catch_Sound drawer
  • Drag out a call Catch_Sound.Play block 
  • Snap it to the when Sprite.Touched block
  • Again, open the Catch_Sound drawer
  • Drag out a call Catch_Sound.Vibrate block 
  • Snap it below the call Catch_Sound.Play block

Go to the Math drawer and pull out a number “0” block, snap it to the empty slot of the Vibrate block and set it to “500” milliseconds. 

Test it on the phone now! It will play the sound and vibrate if you catch the Sprite!

Step 6

Now you need to think about another part of the game. What happens when you first start playing? At the moment, every time we start the game the Sprite starts in the same place. So if a player plays the game more than once, they might notice this and get a free point. 

To prevent this kind of cheating, open the Screen1 drawer and drag out the when Screen1.Initialize block. To make the Sprite start in a random place every time the game begins, copy the call Sprite.MoveTo code and paste it here.

Step 7

If you have a set of complex command blocks that you need to use more than once in your program, you can save time by creating a procedure. A procedure is a new block that you create yourself: it can store a collection of complex blocks. 

Go to the Procedures drawer and pull out a to procedure do block. Click on the word “procedure” and change it to “MoveRandom”. 

Copy the call Sprite.MoveTo code and paste it in the new procedure. 

Step 8

Now you can simplify your code using this new block. Throw the call Sprite.MoveTo code from the Clock and Screen events in the Trash. It will ask you to confirm deletion – do it!

Go to the Procedures drawer and find your new MoveRandom block. Snap this block to the Clock and Screen events. 

Step 9

Now your code is a lot simpler and easier to read. For a big app, making new procedures is a great way to stop the code getting too complex and difficult to understand. 

Test the new code on the phone! The basic game is complete!