Step 12: Making the ducks move down
We currently have it so the ducks move up and then stop above the crate. To get them to move down and hide, we'll use another message system just like we have for making the ducks move up. After the duck starts moving up, we'll send a randomly timed message to move down and hide.
Grab "100 millseconds have passed" (from Control Flow), "random integer from 1 to 100" (from Operators). Drag the time delay block into the bottom of the "When message 'move upwards' is retrieved", and drag the random integer block inside the time delay block. Change the numbers to 500 and 1500.
Then grab "send message 'message' to instance other" (from Events), and "myself" (from Sensing). Then drag "send message 'message' to instance other" into the time delay block, and replace "instance other" with "myself". Change the message to "move downwards".
To get the "move downwards" message actually make the ducks move down, grab the "When message 'message' is retrieved" (from Events), and "set velocity" (from Physics). Set velocity y to 2000 to make it move down.
Now play the game. The ducks will pop up, and then go back down. But they'll keep going down! So we'll fix that, too.
Step 13: Making the ducks stop again
We'll make the ducks stop moving down in the same way that we did before. We'll constantly check if the y position of the duck is greater than 550 (this is near the bottom of the game), and if it is, we'll set its y velocity to 0.
Since we already have the blocks we need for this, duplicate these blocks: "if y position of myself < 150, set velocity y to 0".
Change the duplicate blocks to "if y position of myself > 550, set velocity y 0".
When you play the game now, the ducks will stop when they reach 550 y. This allows them enough time to move back up again before they start moving down. But they get stuck down there!
Lastly, we'll make the ducks send themselves a message to move back up again. This will keep them bobbing up and down forever. Duplicate the blocks "random integer from 1000 to 3000 milliseconds have passed, send message move upwards to myself", and drag these duplicated blocks into the bottom of the "send message move downwards" block.
Playing the game now, you can see the ducks bob up and down. Now we just need to be able to whack them.
Step 14: Targeting the ducks
We'll make it so that when a duck is whacked it goes back down behind the crate. Each time the bat hits left, or middle, or right, we'll have the bat send a hit message to that specific duck. The ducks will tag themselves so that the bat knows which duck to target.
To get the ducks to tag themselves, we need to edit the duck script.
First we'll get the ducks to figure out if they are the left duck. When they're created, we'll get them to check if their x position is smaller than 200, and if it is, then that duck will give itself the tag "left".
Grab "if do" (from Control Flow), "x position of myself" (from Sensing), "<" (starts out as "=" from Operators), "0" (from Operators), and "add tag 'tag name' on myself" (from Sensing). Drag the "if" block to the bottom of the "When created" block, and drag the "<" into the "if" block. Then drag the "x position of myself" block into the left side of the ">" block, and drag the "0" block into the right side. Change 0 to 200.
Then drag the "add tag tag name on myself" block into the "if" block. Set the tag to "left".
Now we'll do the same thing to tag the middle duck. Duplicate the "if" block and all the blocks inside it, and drag them underneath the first "if" block.
We'll use these blocks to check if the duck's x position is larger than 200 and smaller than 400, and if it is, then that duck will give itself the tag "middle".
Change the "< 200" to "> 200", and change the tag "left" to "middle".
Then grab " and " from Operators, and drag it into the if block to replace the "x position of myself > 200" block. Duplicate that block, and drag the original and the duplicate into the lieft and right sides of the " and " block. Then change the right block to "x position of myself < 400"
Lastly, we'll use this same system to tag the duck on the right. Duplicate the first "if" block, and all the blocks inside it, and drag the duplicates underneath the second "if" block. Change these blocks to "x position of myself > 400".
Now all the ducks will tag themselves correctly so that the bat can target them for whacking!
Step 15: Sending the hit message to the ducks
Now we'll make the bat send a message to the ducks when it hits them.
Edit the bat script, and grab "Send message 'message' to instance other". Drag this block into the "When the player presses key left arrow" event, and snap it above the "100 milliseconds have passed" block.
Change the message to "hit". Drag "instance other" into the trash, and replace it with "first instance by tag 'tag name'". Change the tag to "left".
Duplicate this block twice, and drag the duplicates into the "When the player presses key down arrow" and "When the player presses key right arrow". Make sure they send the hit message to the correctly tagged instance, so that the down arrow sends it to the instance tagged "middle", and the right arrow sends it to the instance tagged "right".
Now we'll go and add the hit receiver block to the duck script.
Edit the duck script, and grab "When message of 'message' is retrieved". Drag this block into the workspace, and change message to "hit".
This hit message will make the duck go back down if it is above the crate. Make it so that when the "hit" message is retrieved, the duck will check if its y position is smaller than 300, and if it is, it will set its y position to 500 and set its y velocity to 0. We've use all those blocks before, so you should know where to find them. Arrange them as shown below.
Play the game and whack the ducks!
Step 16: Displaying the score
Now that we've got the game fully playable, we'll add in a score system so that the player can see how well they are doing.
Edit the bat script. We'll add a variable which will keep track of the score number. Go to Variables, and from the Global section, grab "set true/false i to". Drag it to the bottom of the "When created" block.
Click on "true/false" and change this to "number". Then click on "i" and rename it to "score". Drag a "0" into the slot at the end.
Now to display this number variable, we'll create a textfield. Grab "Create new textfield with text" from Draw, and drag it underneath the variable block. Then grab "create text with" from Operators, and grab "number score" from Variables. Drag those blocks into the end of the "create new textfield" block.
We'll set the textfield as a variable so that we can keep changing it. From the Properties section of the Variables tab, grab "set true/false i to". Change "true/false" to "instance" and change the name to "score textfield".
Then grab "instance textfield" from the Local section of Variables, and drag it into the end of the variable block.
Lastly, we'll get the textfield to constantly update to reflect what the score is.
Grab "Constantly" from Events, and drag it under the other blocks. Then grab "Set text of instance textfield to" from Draw, and drag it into the "Constantly" block.
Drag "instance textfield" into the trash, and replace it with "instance score textfield". Then grab "create text with" from Operators, and then grab "number score" from Variables. Connect them together to read "Set text of instance score textfield to create text with number score".
Play the game now to see the score in your game. It doesn't increase! We'll fix that.
Step 17: Increasing the score
To get the score to increase each time a duck is whacked, we'll edit the duck script and make the hit receiver increase the score by 1.
Edit the duck script. Grab "set number score" from Variables, and drag it into the "When message hit is retrieved" block, inside the "if" block.
To increase the score by 1, grab "+" from Operators, "number score" from Variables, and "0" from Operators. Drag the blocks together to read "set number score to number score + 1".
Play the game to see the score increase.
Next, we'll make the text look nicer. Edit the bat script.
Grab two copies of "set x position" from Transform, and drag them to the bottom of the "When created" block. Change one of them to "set y position". Drag "instance score textfield" to replace the "myself" blocks in both these blocks, and set the x and y position to 20.
Now grab "Set font colour of instance textfield" from Draw, and drag it under the position blocks. Then drag "instance textfield" into the trash, and replace it with "instance score textfield". Then choose a colour (yellow looks good).
Now play your game and see the score increase nicely.
Step 18: Displaying the time limit
You could keep playing this game and it would never end. We'll add a time limit so that you can see how many ducks you can whack in limited time.
Edit the bat script. We'll create a new variable for the time limit, and we'll set it to 60 seconds when the bat is created.
From the Global section of Variables, grab "set true/false i to" and drag it underneath the font colour block inside the "When created" block. Change "true/false" to "number" and rename "i" to "timer". Set it to 60.
Grab "create new textfield with text" (from Draw), "create text with" (from Operators), and "number timer" (from Variables). Arrange those blocks to read "Create new textfield with text create text with number timer".
Then grab "set true/false i" from the Properties section of Variables and drag it under the textfield block. Change "true/false" to "instance", and rename it to "timer textfield". Drag "instance textfield" (from the Local section of Variables) into the empty slot.
Duplicate the "set x position" and "set y position" blocks above, and drag the copies underneath the "set instance timer textfield" block. Change these to set the position of "timer textfield", and change the x position to 700.
Lastly, set the font colour of "timer textfield".
Now play the game to see the time limit displayed. But it stays at 60! We'll fix that in the next step.
Step 19: Decreasing the time limit
We'll make it so the time limit decreases every second. We'll do this by sending a message to the bat which will decrease the time limit, and then this message will send itself the same message every 1000 milliseconds.
Grab the "100 milliseconds have passed" block (from Control Flow), and drag it to the bottom of the blocks in "When created".
Change 100 to 1000, and then grab "send message message to instance other", and drag it into the time delay block. Change the message to "timer", and drag "instance other" into the trash and replace it with "myself" (from Sensing).
Now grab "When message of 'message' is retrieved" (from Events) and drag it into the workspace. Change the message to "timer".
Grab "set number timer to" (from Variables), and drag it into the message retriever block. Also grab "+" (from Operators), "number timer" (from Variables), and "0" (from Operators). Change the + to -, and arrange these blocks to read: "set number timer to number timer - 1".
Then grab "100 milliseconds have passed" (from Control Flow), and drag it under the "set number timer" block. Change 100 to 1000, and then grab "send message message to instance other", and drag that inside the time delay block. Change message to "timer", and replace "instance other" with "myself".
This will start a loop that continuously decreases the time limit every second. The last thing we need to do here is to make sure the textfield is updating itself to reflect the number timer.
In the "Constantly" block, find the "set text instance score textfield" block and duplicate it. Change "instance score textfield" to "instance timer textfield", and change "number score" to "number timer".
Now the game is almost complete! Play it and see how many ducks you can whack in 60 seconds!
One last thing: the game doesn't actually end when the timer hits 0! We'll fix that in the next step.
Step 20: End the game when the timer runs out
To make the game end when the timer runs down to 0, we'll send the player to the next level, which will act as a game over screen. This is where we'll tell them how well they did, and allow them to restart the game.
Edit the bat script again. We'll constantly check if the timer is smaller than 0. Grab "if do" (from Control Flow), "number timer" (from Variables), "<" (starts as "=" from Operators), "0" (from Operators), and "go to next level" (from Control Flow). Drag these blocks into the bottom of the "Constantly" block, and arrange them to read: "Constantly if number timer < 0, go to next level."
To actually make the game go to the next level we will need another level. Click on the + button next to Level 1 to create Level 2.
Open the Game Properties sidebar, and scroll down to Level Properties. Make the level size 768 x 512, and make the background coloured blue.
Place a duck in the level, and add a script to the duck. We'll make it so that when the duck is created, we'll create a textfield that says "Good work! You scored (score here)".
Grab "When created" (from Events), "create new textfield with text" (from Draw), "create text with" (from Operators), and "number score" (from Variables). Arrange the blocks to read: "When created, create new textfield with text, create text with 'Good work! You scored ' 'number score'".
Then grab two copies of "set x position" (from Transform), and "instance texfield" (from Variables). Change one of the x positions to "y position", and make the blocks read: "set x position of instance textfield to 200, set y position of textfield to 100".
Then set the font colour to yellow with "set font colour of instance textfield" (from Draw).
Finally, we'll allow the player to play again when they reach the game over screen. Grab three "When player presses key" blocks (from Events), and set them to detect when the left arrow, down arrow, and right arrow keys are pressed. Make it so these three blocks send the player back to the first level with "go to first level" (from Control Flow).
Now we're finished, so play the game and test it out. Congratulations!
If you'd like to remix the finished version of this game to compare it to your own work, visit http://gamefroot.com/game/whack-a-duck/ and click Remix under the game.