Let's create an MPAGD game - Part 11 - Collectibles and our first code
Updated: Oct 30, 2021
I'm going to create a Bone, and Stinky Dog will try to collect them, each time he does, he'll score some points. To create the bone, we'll use the Block editor...
Design your collectable block and click in the block type box to cycle through the available blocktypes until you reach COLLECTABLE
Next, open up the screen editor and add a few collectable blocks to your screen
Now build and test your game (File > Build )
Notice that Stinky Dog now collects the Bones when he touches them.
But the score isn't increasing.
This is it...it's time to write some code.
Open the Events menu, at the bottom is an event called Collect Block, click on it and your text editor should open up, hopefully it should just have one line of code on it:
If it doesn't , just type it in and hit the ENTER key (ALWAYS HOT THE ENTER KEY AT THE END OF EACH LINE OF CODE!)
Next, add the following line of code beneath it:
and hit the return key.
Now, whenever the Collect Block is called, it will add 100 to the score.
Save your Collect Block script (if you are using Notepad you'll need to close it)
But before we test it we need to write a line of code into the Player Event that will call the Collect Block event.
From the Events Menu select Player. The player script will now open in you text editor. This should have about 30 lines of code in it.
At the top of the script but beneath Event Player add the command:
... and hit the Enter key
The GETBLOCKS command removes any collectable blocks with which the current sprite is in contact and calls the Collect Block event for each one removed.
As we have put it in the Player Event, this means when Stinky Dog touches the collectable block it will be removed and the Collect Block event will be called.
Save your player event file and close it.
Now build and test your game.
Did you notice the score increase by 100 for each collectable collected?
It didn't work.
Well, actually it did work. Your score went up by 100 for each one. The problem is the score is not refreshing.
And to understand why, were going to need to look at another event script.
From the Events Menu select Game Initialisation
It should look something like this:
EVENT GAMEINIT LET LIVES = 3 AT 0 16 DISPLAY LIVES AT 0 6 SHOWSCORE AT 0 25 SHOWHIGH
The Game Initialisation script is only run when the game starts. If you read through this script its pretty obvious what this does:
Start the game with three lives
At row 0 column 16 display the number of lives
At row 0 column 6 show the score
At row 0 column 25 show the high score
So the only time the score will refresh at the moment is when we start the game.
We're going to need to add the SHOWSCORE code to the Collect Block event, so that every time a collectable is collected as well as adding 100 to the score, we refresh the score on the screen. Open up your Collect Block event, and add the code (including the AT 0 6). Now it should look like this:
EVENT COLLECTBLOCK SCORE 100 AT O 6 SHOWSCORE
Don't forget to hit ENTER after the last line!
Save and close your Collect Block event script and Build your game again.
Huzzah! Stinky Dog can collect bones...and score points!