This project is the result of an assignment to recreate a simple level of the classic game Arkanoid. A small snippet of the code I wrote for the game, which initializes the game field, is pasted below:
bool ExampleGame::gameInit ( )
{
bool result = myKeyboard.init( getHWnd() );
// number of columns and rows
int columns = 10;
int rows = 4;
{
bool result = myKeyboard.init( getHWnd() );
// number of columns and rows
int columns = 10;
int rows = 4;
// padding between bricks
const int xPad = 10, yPad = 5;
const int xPad = 10, yPad = 5;
// set the array of bricks
for( int bricks = 0; bricks < numBricks; bricks++ )
{
// figure out how much space each brick takes up
int totalWidth = 74 + xPad;
int totalHeight = 46 + yPad;
for( int bricks = 0; bricks < numBricks; bricks++ )
{
// figure out how much space each brick takes up
int totalWidth = 74 + xPad;
int totalHeight = 46 + yPad;
// offset values
int xOffset = (dxWinWidth() - (totalWidth * columns )) /2;
int yOffset = (dxWinHeight() - (totalHeight * rows)) /10;
int xOffset = (dxWinWidth() - (totalWidth * columns )) /2;
int yOffset = (dxWinHeight() - (totalHeight * rows)) /10;
// x and y values
int xPos = (bricks % columns * totalWidth ) + xOffset;
int yPos = (bricks / columns * totalHeight ) + yOffset;
int xPos = (bricks % columns * totalWidth ) + xOffset;
int yPos = (bricks / columns * totalHeight ) + yOffset;
myBricks[bricks].create( "Block.png", dxDev(), xPos, yPos );
myBricks[bricks].setNumFrames( 0 );
myBricks[bricks].setFrameHeight( 46 );
myBricks[bricks].setFrameWidth( 74 );
}
myBricks[bricks].setNumFrames( 0 );
myBricks[bricks].setFrameHeight( 46 );
myBricks[bricks].setFrameWidth( 74 );
}
int midScreenPos = (dxWinWidth()) /2;
int botScreenPos = (dxWinHeight()) - 50;
myPaddle.create( "Block.png", dxDev(), midScreenPos, botScreenPos);
int botScreenPos = (dxWinHeight()) - 50;
myPaddle.create( "Block.png", dxDev(), midScreenPos, botScreenPos);
myPaddle.setNumFrames( 0 );
myPaddle.setFrameHeight( 23 );
myPaddle.setFrameWidth( 95 );
myPaddle.setFrameHeight( 23 );
myPaddle.setFrameWidth( 95 );
myBall.create( "BallRotate.png", dxDev(), midScreenPos, (botScreenPos - 60));
myBall.setFps( 0 );
myBall.setNumFrames( 2 );
myBall.setFrameHeight( 31 );
myBall.setFrameWidth( 30 );
myBall.setNumFrames( 2 );
myBall.setFrameHeight( 31 );
myBall.setFrameWidth( 30 );
return true;
}
}
This is another snippet of code from the same game, which provides the primary game loop:
bool ExampleGame::gameRun( )
{
dxDev()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0);
myKeyboard.update();
//start rendering
if (dxDev()->BeginScene() == D3D_OK )
{
for( int bricks = 0; bricks < numBricks; bricks++ )
{
if(bricks % 3 == 0)
{
myBricks[bricks].setRandColor();
}
myBricks[bricks].draw( dxSprite() );
//test for collisions
if(Collision(myBall, myBricks[bricks]))
{
myBricks[bricks].setXPos( 2000 );
myBall.setDeltaY( (myBall.getDeltaY() * -1.0f) );
}
}
if(!((myPaddle.getXPos() <= 10)))
{
if (myKeyboard.keyDown(DIK_LEFT)) myPaddle.setXPos( myPaddle.getXPos() - 6.0f );
}
if(!(myPaddle.getXPos() >= (dxWinWidth()- 100)))
{
if (myKeyboard.keyDown(DIK_RIGHT)) myPaddle.setXPos( myPaddle.getXPos() + 6.0f );
}
if(Collision(myBall, myPaddle))
{
myBall.setDeltaY( (myBall.getDeltaY() * -1.0f) );
}
myPaddle.draw( dxSprite() );
myBall.draw( dxSprite() );
myBall.update( dxSprite() );
//stop drawing
dxSprite()->End();
//stop rendering
dxDev()->EndScene();
//display the back buffer on the screen
dxDev()->Present( NULL, NULL, NULL, NULL);
}
return true;
}