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.
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.
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.
So that makes the sprite move to one location, but we want it to hop all over the screen.
This means your sprite will never move off the screen.
Check the phone! Your Sprite will be randomly hopping all over the screen!
Program the phone to play the sound and vibrate when the Sprite is touched.
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!
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.
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.
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.
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!