top of page
Writer's pictureBruce

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:



EVENT COLLECTBLOCK

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:


SCORE 100

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:


GETBLOCKS

... 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?


No.


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!




Comments


Want to support my work?....Buy my games!

aboutME

Hello, I'm Bruce and I write games for old 8bit computers using Jonathan Cauldwell's excellent Multi-Platform Arcade Games Designer (MPAGD)

I've written a few successful* games for the Sinclair ZX Spectrum and MSX platforms that have been (largely) well received including Twenty Four Hour Parsley People scoring a 10 out of 10 on Planeta Sinclair.

In my blog I am sharing lots of the code that I wrote for my games, in a way that you can use in your own games.   I've commented it so that you'll learn some of the techniques I use to create interesting new mechanics and help your games stand out from the pack.

MPAGD includes lots of standard scripts, they're great to get you started, but if you're new (or just rusty) when it comes to writing code, hopefully my tutorials will help you get started and  turn your imagination into awesome 8 bit games!

All my code is free to use and do with as you please, but if you find them useful please feel free to buy me a coffee ...or better still - buy or download my games :)

*successful is a very relative term in 8bit computer games

bottom of page