ASK Tutorial 01 - Bouncing Ball

(1/2) > >>

Richard Marks:
ASK Tutorial 01 - Bouncing Ball
Watch the terribly shitty video version of this tutorial

Alright here is a super small tutorial on making a project with my Allegro Starter Kit. 8)

The concept is simple: Bounce a ball around the screen.

I'm assuming you are using Visual C++ 2008 Express (MSVC9), as that is one of the most popular IDEs for Windows.
If you're not using MSVC9, then you will need to adjust some steps to suit your IDE.

OK, Here is what to do.

Step #1 - Download A.S.K.

Check this forum thread to make sure you get the latest version of A.S.K.

Step #2 - Extract the kit files to some place you can easily get to.

You should know how to extract a .zip file already! ::) Put the files somewhere logical.

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

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

Step #4 - Save the Empty Project

File -> Save All

Step #5 - Copy the kit files to your project's folder.

Copy all the kit files into the folder that holds the ASKBouncingBallDemo.vcproj file.

Step #6 - Add the kit files to the Project

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

Step #7 - 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:

float ballX_;
float ballY_;
float ballDX_;
float ballDY_;
int ballRadius_;
int ballColor_;

Yeah, its messy to not class up the ball, but I'm being brief just to get you started. :D
Now, where it says your game methods here add this code:

void UpdateBall();
void RenderBall();

OK, File -> Save to save your changes, and open PrimaryWindow.cpp and add this code near the end of the file:

void PrimaryWindow::UpdateBall()
ballX_ += ballDX_;
ballY_ += ballDY_;

if (ballX_ < ballRadius_ || ballX_ > (SCREEN_W - ballRadius_))
ballDX_ = ballDX_ * -1.0f;
ballX_ += ballDX_;

if (ballY_ < ballRadius_ || ballY_ > (SCREEN_H - ballRadius_))
ballDY_ = ballDY_ * -1.0f;
ballY_ += ballDY_;

void PrimaryWindow::RenderBall()
circlefill(_backBuffer, (int)ballX_, (int)ballY_, ballRadius_, ballColor_);

The update function for the ball just adds the ball velocity to the position to get a new position, and then tests to see if the ball has hit any of the sides, and if so, the velocity is inverted and re-applied to make sure the ball never goes off screen.

The rendering function is just a simple filled circle using the Allegro function circlefill()
The _backBuffer is an Allegro BITMAP pointer from the ASKWindow class which is where the black-box stuff comes from.

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, add this code:

// a blue ball
ballColor_ = makecol(0, 0, 255);

// the ball has a radius of 8 pixels
ballRadius_ = 8;

// the ball starts in the center of the screen
ballX_ = SCREEN_W / 2;
ballY_ = SCREEN_H / 2;

// the ball starts on a random trajectory
ballDX_ = (float)4+(rand()%8);
ballDY_ = (float)4+(rand()%8);

And that is it.
Steps 3 - 6 you will repeat for any project you want to make, but thats a small price to pay since I'm implementing a lot of stuff for you already! 8)

Let me know if you were able to follow this tutorial without any trouble.

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

I left you a rating!   ;D

Richard Marks:
Thanks :D

I've noticed that you've once again changed your naming conventions, and are declaring your member variables as _var instead var_. I think var_ is much better for coding and for the reasons as follows. Users using _var may get mixed up with the C++ restricted identifier rules regarding identifiers beginning with an underscore. The C++ rules states that 1) _var is reserved in global space, 2) _Var (underscore+capital letter) is reserved anytime. Although your code does not violate any of these rules, but by using the var_ form, you will never mix up these rules and is I believe the reason why people placed the _ at the end in the first place.

Richard Marks:
Doh! ::) can blame my recent usage of C# for that.

When I get home tonight, I'll go reverse the usage of underscores in the series.
-_- I'm so tired right now, and I have an 8 hour shift in 2 hours. ttyl.


[0] Message Index

[#] Next page