Wednesday, 15 August 2012

Post 07 - Collect & Score

No platformer is complete without collectibles. In this post we will create a collectible object to be placed around the level, a HUD object which will count how many collectibles you have collected and a level exit which only becomes available after a set amount of collectibles have been collected.

THE COLLECTIBLE
We'll start by creating a new sprite and object voor the collectible. I've named mine obj_coin. This collectible will work slightly different then you might be used to. For it to be 'picked up' both players must have collision with it at the same time. Collision Events only allow collision with one object, so we make a Step Event in the collectible object and check for collision with code. If both players have collision with it, it will disappear. We use the following bit of code for this.

if collision_rectangle(x-8,y-8,x+8,y+8,obj_char,0,0) && collision_rectangle(x-8,y-8,x+8,y+8,obj_char2,0,0)
{
    instance_destroy();
}

The above collision code works like this. First you give the four corners of the collision rectangle, then the object you want to check collision with, and then if precise is true and if notme is true. We draw a collision rectangle as big as the coin (in relation to the center of the coin it  draws the collision rectangle from x-8 to y-8 to x+8 to y+8) and check for the player1 object. Precise and notme are false. We do the exact same for player2 object. Place some collectibles in your test level.

THE HUD
Next we want our amount of collected objects to be displayed somewhere. For this we make a new object which will serve as a hud. I made it 32x32 and made it look like the image below. It's basically my collectible object with an equal sign next to it. I called it obj_hud. Place the hud object in your test level. Preferably somewhere in one of the upper corners.


We are going to count the collected objects in the hud object. So let's start off by defining collect_counter in the Create Event of the hud object and settings it's value to 0.

collect_counter = 0;

Next we want the counter to go up by 1 every time both players collide with a collectible. To do this we edit the above piece of code like this.

if collision_rectangle(x-8,y-8,x+8,y+8,obj_char,0,0) && collision_rectangle(x-8,y-8,x+8,y+8,obj_char2,0,0)
{
    obj_hud.collect_counter += 1;
    instance_destroy();
}

Now it will add 1 to the collect_counter of the hud object before being destroyed.

Our next step is to be able to see how many collectibles we have collected. To do this we will use a draw function. Draw functions can only be used in Draw Events, so we'll make a draw event in the hud object. Once an object has a Draw Event, the object itself must be drawn within this event, or else it won't be visible in the game. So in the Draw Event we type the following code.

draw_sprite_ext(spr_hud,0,x,y,1,1,0,c_white,1);

draw_text(x+20,y-10,collect_counter);

The first bit of code draws the hud object. It is the same piece of code we have used before. The second piece draws how many collectibles we have collected at position x+20 and y-10 from the hud object position. This position worked best for the hud object I draw, it might work out differently with yours. Play around with it a bit until you have a nice position.

THE EXIT
Next up we'll create an exit object. Mine looks like a gate and is called obj_exit. Place the exit somewhere in the test level. We want this exit to appear whenever a set amount of collectibles have been gathered. In my case 5. To do this we make a Draw Event in the exit object and use the next piece of code.

if obj_hud.collect_counter >= 5
{
    draw_sprite_ext(spr_exit,0,x,y,1,1,0,c_white,1);
}

This code checks if the collect_counter has reached 5. If so, it draws the exit in the same way we have drawn objects before.

Lastly we want the exit to show a message and how many collectibles you have collected. We do this by creating a Step Event in the exit object and using the same collision check code as we used before with the collectibles. Except now the collision rectangles are bigger because my exit object is bigger.

if collision_rectangle(x-16,y-16,x+16,y+16,obj_char,0,0) && collision_rectangle(x-16,y-16,x+16,y+16,obj_char2,0,0)
{
    show_message("GOOD JOB! You have collected "+string(obj_hud.collect_counter)+" coins!");
    game_end(); 
}

So if there is collision with both players it will show the above message. What is typed in between quotes will be shown as text in the message. In the string we show the collectible counter. Afterwards the game will close.

Great! You now have the basics of a sneak/platform game!

ASSIGNMENT: COUNT PLAYER DEATHS
Find a way to count the death of both players and show this amount at the end message.

Take a look at how I did it (including assignment answers) here.