December 09, 2018, 02:01:19 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
   Home   Blogs Help Search Tags Login Register  
Pages: [1]   Go Down
Author Topic: ASK Tutorial 06 - Player Lives  (Read 2778 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks

Respect: 3425
Offline Offline

Posts: 1027


« on: March 30, 2010, 04:01:06 PM »

ASK Tutorial 06 - Player Lives

Guess what? Time for yet another Allegro Starter Kit tutorial! Cool

The concept: Give the player life tracking and regeneration code.

It is assumed that you are using Visual C++ 2008 Express (MSVC9)
If you're not using MSVC9, then you will need to adjust some steps to suit your IDE.

This tutorial builds off the previous tutorial's code, so don't forget to add the code from the previous tutorial.

Step #1 - Create a new Empty Win32 Project (not a Console Application) in your IDE.

File -> New Project
Choose Win32 Project
Enter "ASKPlayerLivesDemo" for the Name
Click OK
Click Application Settings
Check the Empty Project check-box
Click Finish

Step #2 - Save the Empty Project

File -> Save All

Step #3 - Copy the code files from Tutorial 05 into your project's folder.

Copy all the *.cpp and *.h files from the Tutorial 05 project into the folder that holds the ASKPlayerLivesDemo.vcproj file.

Step #4 - Add the files to the Project

Project -> Add Existing Item
Choose all the .h and .cpp files you just copied and click Add.

Step #5 - Add the code to the PrimaryWindow files to implement the demo concept.

Our player object is getting upgraded a bit.
We are going to add a re-spawn timer to it, as well as some lives counters.
This program will not do anything new visibly, but we are setting up for later tutorials.
I was going to just stick all the new stuff in a single tutorial, but I thought that smaller increments would be easier to understand. Smiley

So, on to the code!
Add the new variables to PrimaryWindow.h
bool playerAlive_;
int startingPlayerLives_;
int playerLives_;
int playerRespawnTimeDelay_;
int playerRespawnTimeCounter_;

The LoadContentForPlayer() function in PrimaryWindow.cpp needs to be updated to initialize our new variables.
// the player starts the game in a dead state
// this gives the player a few seconds before the action begins.
playerAlive_ = false;

// the player will start with 5 lives
startingPlayerLives_ = 5;

// set the player with the right number of lives
playerLives_ = startingPlayerLives_;

// the player will take roughly 3 seconds to re-spawn
playerRespawnTimeDelay_ = 90;
playerRespawnTimeCounter_ = 0;

We need to move the player's starting Y coordinate up 2 pixels above the new ground level.
If you were paying attention in the previous tutorial, you will know that the ground level is 18 pixels from the bottom of the screen now.

Modify the Y starting coordinate as follows:
playerY_ = (SCREEN_H - 20) - playerHeight_;

The UpdatePlayer() function is now going to get upgraded to handle ending the game and re-spawning the player when it dies.
The new code goes before the key handling code, as a dead player doesn't get to have keyboard input. Grin
// if the player is dead
if (!playerAlive_)
// if the player has no lives remaining
if (!playerLives_)
// end the program for now.
// We will add game over screens and menus in later tutorials.

// exit the function

// increment the respawn counter

// if the respawn counter has reached the respawn delay
if (playerRespawnTimeCounter_ == playerRespawnTimeDelay_)
// reset the respawn counter
playerRespawnTimeCounter_ = 0;

// respawn the player:
playerX_ = SCREEN_W / 2 - playerWidth_ / 2;
playerY_ = (SCREEN_H - 20) - playerHeight_;

// set the player to be alive
playerAlive_ = true;

// exit the function

That should be pretty simple to understand, as its mostly code we've used before to re-spawn the ball.
The only new stuff is the player lives part, which isn't difficult.
The Close() function call is part of the A.S.K. ASKWindow, it will end the game abruptly when called.

Since we made our player start off "dead", there will be a delay before you can control the player.
You might think its broken if we don't somehow tell the user about the delay.
So, let's add something cool. Cool

We will modify the RenderPlayer() function to draw the player in a wire-frame when dead, but also we will make the player flash from visible to invisible during the time that it is dead. I call this time the re-spawning state.

First thing is we will calculate the bottom of the player's triangle.
We already have this, we're going to just move it to be the first line in the function.
Next we add a few local array variables to hold the X/Y coordinates for each of the player's triangle's points.
int vx[] = { playerX_, playerX_ + (playerWidth_ / 2), playerX_ + playerWidth_ };
int vy[] = { playerBottom, playerY_, playerBottom };

Now we need to test to see if the player is dead.
If the player is dead, we need to draw the player's triangle in wire-frame.
To do this, we will use 3 lines. We draw from V1 to V2 from V2 to V3 and from V3 to V1.
The vertices (points) are stored in our new arrays vx and vy, and the vertices are indexed starting from zero.
So, the vertex V1 is stored at vx[0], vy[0]

The other thing we need to do is make the wire-frame flicker on and off.
An easy way to do this is to test if the re-spawn time counter value is even or odd.
When it is even, you draw the wire-frame, and when odd, you will draw nothing.
You test if a value is even by checking if the modulus of 2 is zero.
such as
// don't add this code to the program! Its an explanation!
int value = 5;

if ((value % 2) == 0)
// value is even
// value is odd (since we set value to 5, this code block will execute)

OK, that should be enough explanation for you to understand the following code. Roll Eyes
if (!playerAlive_)
// wireframe display flashes
if ((playerRespawnTimeCounter_ % 2) == 0)
line(_backBuffer, vx[0], vy[0], vx[1], vy[1], playerColor_);
line(_backBuffer, vx[1], vy[1], vx[2], vy[2], playerColor_);
line(_backBuffer, vx[2], vy[2], vx[0], vy[0], playerColor_);

// exit the function

Lastly, we change the way that the player is drawn to use the vx and vy arrays.
// we draw the player as a filled triangle
triangle(_backBuffer, vx[0], vy[0], vx[1], vy[1], vx[2], vy[2], playerColor_);

Before we call this tutorial finished, lets tell the ball not to attack a dead player! Cheesy

In UpdateBall() right before the condition test to see if the ball has ammo we add a new condition to be met.
// is the player even alive?
if (!playerAlive_)
// exit the function

Now, the player still cannot be killed by the ball's shots, but that is a topic for another tutorial.
The code we added will not do anything visibly yet, except for the initial 3 seconds that the player flashes. Cool

OK, that is all for this tutorial.

When you compile and run the project, you should get:
1 - A window with a black background
2 - The bouncing blue ball from the first tutorial
3 - The green triangle from the second tutorial that you can move left and right using the arrow keys.
4 - A single reloading cyan bullet that you can fire using the space bar.
5 - The ball should disappear if the bullet collides with it.
6 - The ball should re-appear (re-spawn) after roughly 1 second has passed after it dies.
7 - The ball should attack the player when it is moving downwards, and above the Y center line.
8 - Three second delay before you can move the player as it is in a "re-spawning" state.
9 - The player should flash on and off while it is in the re-spawning state.

Much more to come.
Thanks for reading! See you in the next tutorial!
Let me know if you were able to follow this tutorial without any trouble.

(Updated April 2, 2010 - Changed usage of underscores in regards to Redslash's notice.)

Tags: ask code  Allegro tutorial allegro starter kit tutorial c++ 
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!