Experiments In Game Programming
Main
 Home
XNA - C#
 Coming Soon!
 Level Editing - GTK
DirectX 9 - C++
 Downloads
 Disclaimer
 Introduction
Part 1 - DirectX
 1 - Breakout
 2 - Create DX
 3 - 2d Images
 4 - 3d Models
 5 - Cameras & Lights
 6 - Animation Timing
 7 - Keyboard/Mouse
 8 - Sound
Part 2 - Breakout
 1 - Art and Sounds
 2 - The Menu
 3 - Starting Breakout
 4 - The Level
 5 - The Paddle
 6 - The Ball
 7 - Finishing Touches

Untitled Document

Chapter 5

 

Creating the Paddle

 

 

In this chapter we are going to create the PaddleController. The PaddleController will handle getting player input, and positioning and rendering the paddle.

Topics Covered in this Chapter:
  • Creating The Paddle
  • Adding the Paddle to Breakout

The PaddleController:

The PaddleController class does exactly what you would expect it to do, it controls the paddle. Take a look at the PaddleController from the part 2, chapter 5 code. Like the Level class the PaddleController also has a number of defines at the top. PADDLEY is the y position of the paddle which stays constant, the paddle is only allowed to move left and right. The PADDLE_MIN_X, and PADDLE_MAX_X control how far left and how far right the paddle can move.
const float PADDLEY = -15.0f;
const float PADDLE_MAX_X = 20;
const float PADDLE_MIN_X = -20;
const float PADDLE_WIDTH = 3.0f;

After the top, the first function of interest is init. Init loads the paddle model, and keeps a local copy of the InputManager.
bool PaddleController::init(InputManager* input, LPDIRECT3DDEVICE9 device){
     //Local pointer to the input manager
     myInput = input;
     //set the paddle to start at 0
     paddleX = 0;
     paddle = new Model();
     //load the paddle
     paddle->loadModel(device, "paddle.x");
     //set the scale of the model
     paddle->setScale(D3DXVECTOR3(0.25f,0.25f,0.25f));

     return true;
}

The render function sets the position of the paddle model and renders it.
void PaddleController::render(LPDIRECT3DDEVICE9 device){
     paddle->setPosition(D3DXVECTOR3(paddleX,PADDLEY, 0.0f));
     paddle->render(device);
}

Update gets the user input, and moves the paddle based on the X velosity of the mouse. The paddle is stopped from going past the PADDLE_MAX_X, and PADDLE_MIN_X.
svoid PaddleController::update(){
     myInput->getInput();
     //updates the paddleX
     paddleX+= myInput->getMouseMovingX()/10;

     //stop the paddle from going past the Max and Min paddle positions
     if (paddleX > PADDLE_MAX_X){
         paddleX = PADDLE_MAX_X;
     } else if (paddleX < PADDLE_MIN_X){
         paddleX = PADDLE_MIN_X;
     }
}

The last 2 functions are getPaddleXY, and getWidth. getPaddleXY fills the passed pointers with the current paddle x and y positions. getWidth returns the width of the paddle. Both of these functions are used for collision deteciton purposes.
void PaddleController::getPaddleXY(float* x, float* y){
     *x = paddleX;
     *y = PADDLEY;
}

float PaddleController::getWidth(){
     return PADDLE_WIDTH;
}

That's all there is to the PaddleController class, adding it to the Breakout class requires adding commands to init, update, and render.

Changes made to Breakout init:
bool Breakout::init(InputManager* input, SoundManager* sounds, LPDIRECT3DDEVICE9 device){
     .....
     //create the paddle controller
     paddle = new PaddleController();
     paddle->init(input, device);
     //create the ball controller


     ....

     return true;
}

Changes made to Breakout update:
void Breakout::updateFrames(int numberOfFrames){
     //update the paddle
     paddle->update();

     //update the ball controller
}

Changes made to Breakout render:
void Breakout::render(LPDIRECT3DDEVICE9 device){
     //render the background
     background->render(device);
     //render the level
     level->render(device);
     //render the ball
     //render the paddle
     paddle->render(device);

}

Now that the PaddleController class has been added to the Breakout class, we can compile and run the project. Starting a new game, you should now have a paddle controlled by the mouse at the bottom of the level.

.

Summing Up - Chapter 5

The PaddleController is a fairly simple class, the paddle size is controlled by the constants and the scale. Changing these would allow you to scale the paddle. Often breakout games allow powerups to adjust the size of the paddle, with a few changes our paddle could be made to have a dynamic size as well.
 ©2008 David Whittaker