December 09, 2018, 03:26:57 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 03 - Shooting Bullets  (Read 4925 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks

Respect: 3425
Offline Offline

Posts: 1027


« on: March 28, 2010, 05:56:38 PM »

ASK Tutorial 03 - Shooting Bullets

It is time for another Allegro Starter Kit tutorial. Cool

The concept: Give the player the ability to shoot a bullet.

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 "ASKShootingBulletsDemo" 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 02 into your project's folder.

Copy all the *.cpp and *.h files from the Tutorial 02 project into the folder that holds the ASKShootingBulletsDemo.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.

In the PrimaryWindow.h file, where it says your game variables here add this code:
bool playerShotAlive_;
float playerShotX_;
float playerShotY_;
float playerShotDY_;
int playerShotRadius_;
int playerShotColor_;

We use a boolean to track whether the shot object is alive.
When the shot object is alive, it will be rendered, and its position updated.
The other variables should be self-explanatory.

Now, where it says your game methods here add this code:
void UpdatePlayerShot();
void RenderPlayerShot();

OK, File -> Save to save your changes, and open PrimaryWindow.cpp and add this code near the end of the file:
void PrimaryWindow::UpdatePlayerShot()
// if the shot is not alive, then we move it to match
// the player's X center position
if (!playerShotAlive_)
playerShotX_ = playerX_ + (playerWidth_ / 2);
// if the shot is alive, we move it along the Y axis
playerShotY_ -= playerShotDY_;

// if the shot moves off the top of the screen
if (playerShotY_ <  -playerShotRadius_)
// kill the shot
playerShotAlive_ = false;

// reset its position to the top of the player
playerShotX_ = playerX_ + (playerWidth_ / 2);
playerShotY_ = playerY_ - playerShotRadius_;

The way that the shot works is, while the shot is dead, it is invisibly moved to match the position of the player.
When the shot is alive, it no longer is matched to the X position of the player, and is moved by applying the velocity
to the Y position of the shot object.
Note that the velocity is subtracted, because we want the shot to go UP and not DOWN.

A test is made after applying the velocity to the shot, to see if it has moved off the top of the screen.
If the shot has moved off the screen, the shot is then killed and its position gets reset to the player.

At the end of the PrimaryWindow.cpp file, add this code next:
void PrimaryWindow::RenderPlayerShot()
// exit the function if the shot is dead
if (!playerShotAlive_)

// draw the shot
(int)playerShotX_, (int)playerShotY_,
playerShotRadius_, playerShotColor_);

The shot object is not rendered if it is not alive, so we just test this condition first.
The rendering of the shot is done using a simple filled circle using the Allegro function circlefill()

OK, we need to call those new member functions from the core functions so the kit code can use them.
In the PrimaryWindow::UpdateFrame() function, add this code:

In the PrimaryWindow::RenderFrame() function, add this code:

In the PrimaryWindow::LoadContent() function, after you initialize the Player's variables, add this code:
// the shot is dead by default
playerShotAlive_ = false;

// the player's shots have a 4 pixel radius
playerShotRadius_ = 4;

// the player fires cyan shots
playerShotColor_ = makecol(0, 255, 255);

// position the shot at the top-center of the player's ship
playerShotX_ = playerX_ + (playerWidth_ / 2);

// reset its Y position to the top of the player
playerShotY_ = (float)(playerY_ - playerShotRadius_);

// shot velocity is set once
// all shots are fired at 20 pixels per frame
playerShotDY_ = 20.0f;

One more thing, we need to add some code to the UpdatePlayer() function.
Add this code after the Left/Right arrow checks, and before the test for the player moving off screen:
// NEW
// the player is limited to firing a single shot at a time.
// if the space bar is pressed and the shot is currently dead
if (key[KEY_SPACE] && (!playerShotAlive_))
// fire a shot by making it alive
playerShotAlive_ = true;

And that is it.

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.

More fun is to come as I continue this tutorial series. Cheesy
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!