December 09, 2018, 03:08:41 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 02 - Keyboard Control  (Read 2942 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks

Respect: 3425
Offline Offline

Posts: 1027


« on: March 28, 2010, 05:12:08 PM »

ASK Tutorial 02 - Keyboard Control

Welcome to yet another tiny tutorial for my Allegro Starter Kit.

The concept: Add a player controlled object to the previous bouncing ball demo.

Once again I am assuming 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 "ASKKeyboardControlDemo" 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 01 into your project's folder.

Copy all the *.cpp and *.h files that you added to the Tutorial 01 project into the folder that holds the ASKKeyboardControlDemo.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:
int playerX_;
int playerY_;
int playerWidth_;
int playerHeight_;
int playerColor_;
int playerSpeed_;

I'm still not bothering with classing up the code yet, but rest assured that will be done in a later tutorial.
Now, where it says your game methods here add this code:
void UpdatePlayer();
void RenderPlayer();

OK, File -> Save to save your changes, and open PrimaryWindow.cpp and add this code near the end of the file:
void PrimaryWindow::UpdatePlayer()
// if the left arrow key is down
if (key[KEY_LEFT])
// move left
playerX_ -= playerSpeed_;
// if the right arrow key is down
else if (key[KEY_RIGHT])
// move right
playerX_ += playerSpeed_;

// ensure that the player hasn't gone off screen
int xMax = SCREEN_W - playerWidth_;
if (playerX_ < 0)
playerX_ = 0;
else if (playerX_ > xMax)
playerX_ = xMax;

The player's update function simply tests for the Left and Right arrow keys and moves the player accordingly.
There is a simple test to keep the player from moving off the screen at the end.

At the end of the PrimaryWindow.cpp file, add this code next:
void PrimaryWindow::RenderPlayer()
// we draw the player as a filled triangle

The player is defined by a position and a size:

The triangle's 3 points are

|   / \   |
|  /   \  |
| /     \ |
|/       \|

So, point 1 is at x, y+h
point 2 is at x+(w/2),y and
point 3 is at x+w,y+h

// we calculate the bottom once and save the value
// because it will be used twice. a tiny optimization
// that reduces the number of calculations needed.
int playerBottom = playerY_ + playerHeight_;

playerX_, playerBottom,
playerX_ + (playerWidth_ / 2), playerY_,
playerX_ + playerWidth_, playerBottom, playerColor_);

The rendering function is just a simple filled triangle using the Allegro function triangle()

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:
// the player is 32 pixels wide by 64 pixels tall
playerWidth_ = 32;
playerHeight_ = 64;

// the player is at the bottom center of the screen
playerX_ = SCREEN_W / 2 - playerWidth_ / 2;
playerY_ = SCREEN_H - playerHeight_;

// the player is green
playerColor_ = makecol(0, 255, 0);

// the player moves at 1/3 of its width
playerSpeed_ = playerWidth_ / 3;

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 - A green triangle that you can move left and right using the arrow keys.

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

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!