Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

game bricks

This topic is 2523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well I am still working on my breakout game.I have 2 sticking points.first of all I want to move the lower paddle with the arrow keys.Secondly I want to turn off the bricks when the ball hits them.here is some of my code so far.please go easy I am really trying to finish this game so I can move on to bigger and better things. I am using c++ and direct x 9.I have also been studying c++ alot so I am not totally ignorant.
here is my code
[source lang="c++"]
[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] RenderFrame( )

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"] (!g_pD3DDevice && !g_pD3DXSprite && !g_pTexture)

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"];

?

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]// Clear the frame & depth buffer ready for drawing (Black color)

[/font][/font][/font][font="Consolas"][font="Consolas"]g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

g_pD3DDevice->BeginScene();

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] index=0;

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//-------------------------

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]// Render the sprite

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//

[/font][/font][/font][font="Consolas"][font="Consolas"]SPRITE sprite1;

SPRITE sprite2;

sprite1.x=posX;

sprite1.y=posY;

sprite1.width = 10.0f;

sprite1.height = 10.0f;

sprite2.x=0.0f;

sprite2.y=200.0f;

sprite2.width = 160.0f;

sprite2.height = 40.0f;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](Sprite_Collide(&sprite1,&sprite2)==1)

{

yVel=0.25f;

index=1;

}

sprite2.x=160.0f;

sprite2.y=200.0f;

sprite2.width = 160.0f;

sprite2.height = 40.0f;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](Sprite_Collide(&sprite1,&sprite2)==1)

{

yVel=0.25f;

index=2;

}



sprite2.x=320.0f;

sprite2.y=200.0f;

sprite2.width = 160.0f;

sprite2.height = 40.0f;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](Sprite_Collide(&sprite1,&sprite2)==1)

{

yVel=0.25f;

index=3;

}

sprite2.x=480.0f;

sprite2.y=200.0f;

sprite2.width = 160.0f;

sprite2.height = 40.0f;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](Sprite_Collide(&sprite1,&sprite2)==1)

{

yVel=0.25f;

index=4;

}

sprite2.x=640.0f;

sprite2.y=200.0f;

sprite2.width = 160.0f;

sprite2.height = 40.0f;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](Sprite_Collide(&sprite1,&sprite2)==1)

{

yVel=0.25f;

index=5;

}

D3DXVECTOR3 vecPos_ball = D3DXVECTOR3(posX,posY,0);

posX += xVel;

posY += yVel;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](posX <= 10.0f)

{

posX=10.0f;

xVel = 0.25f;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](posX >= 790.0f)

{

posX=790.0f;

xVel =-0.25f;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](posY <= 10.0f)

{

posY=10.0f;

yVel=0.25f;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](posY >= 590.0f)

{

posY=590.0f;

yVel=-0.25f;

}

D3DXVECTOR3 vecPos_upper = D3DXVECTOR3(j,0,0);

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](bAdd)

{

j+=0.25f;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else

[/font][/font][/font][font="Consolas"][font="Consolas"]{

j-=0.25f;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](j>=640)

{

bAdd = [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]false[/font][/font][/font][font="Consolas"][font="Consolas"];

}



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](j<=0)

{

bAdd = [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][font="Consolas"];

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][font="Consolas"]([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]float[/font][/font][/font][font="Consolas"][font="Consolas"] i=0; i<=640; i+=160)

{



D3DXVECTOR3 vecPos_brickone = D3DXVECTOR3(i,120,0);

D3DXVECTOR3 vecPos_bricktwo = D3DXVECTOR3(i,160,0);

D3DXVECTOR3 vecPos_brickthree = D3DXVECTOR3(i,200,0);

D3DXVECTOR3 vecPos_lower = D3DXVECTOR3(320,560,0);

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"] (g_pD3DXSprite && g_pTexture)

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](index==1)

{

yVel=-0.25f;

D3DXVECTOR3 vecPos_brickfour = D3DXVECTOR3(0,200,0);

}

g_pD3DXSprite->Begin( D3DXSPRITE_ALPHABLEND );

g_pD3DXSprite->Draw(g_pTexture, NULL, NULL, &vecPos_upper, 0xffffffff);

g_pD3DXSprite->Draw(g_pTexture, NULL, NULL, &vecPos_lower, 0xffffffff);

g_pD3DXSprite->Draw(g_pTexture_two, NULL, NULL, &vecPos_brickone, 0xffffffff);

g_pD3DXSprite->Draw(g_pTexture_three, NULL, NULL, &vecPos_bricktwo, 0xffffffff);

g_pD3DXSprite->Draw(g_pTexture_four, NULL, NULL, &vecPos_brickthree, 0xffffffff);

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]// g_pD3DXSprite->Draw(g_pTexture_five, NULL, NULL, &vecPos_brickfour, 0xffffffff);

[/font][/font][/font][font="Consolas"][font="Consolas"]g_pD3DXSprite->Draw(g_pTexture_ball, NULL, NULL, &vecPos_ball, 0xffffffff);

g_pD3DXSprite->End();

}



}

}

g_pD3DDevice->EndScene();



[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]// Frame buffer to Front buffer

[/font][/font][/font][font="Consolas"][font="Consolas"]g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] Paddle_Left()

{

D3DXVECTOR3 vecPos_lower = D3DXVECTOR3(320+m,560,0);

m--;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] Paddle_Right()

{

D3DXVECTOR3 vecPos_lower = D3DXVECTOR3(320+m,560,0);

m++;

}

[/source]


[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
What is your question?

The code is an excerpt and poorly formatted, making it difficult to read. However it appears you are recreating much game state inside the render function. Instead, create a set of persistent data structures that describe the state of the game that lasts between render calls.

For example, the grid can be represented by a two dimensional array of booleans, initially set to "true". When a brick is hit, you negate it's entry in the array. When drawing, you omit bricks with a value of false. You can also tell if the player has completed the level once all bricks are false.

Consider splitting the game into (at least) three distinct parts: update, render and input. The "update" step should handle collisions and movement. The render step should just paint to the screen. The input step applies the latest input to the player avatar object.

Something like the following:


const int MAX_BRICK_X = 40;
const int MAX_BRICK_Y = 20;

// This is just the *mutable* game state
// Constant state, such as the dimensions of the paddle, bricks
// and the paddle Y position, could be stored as constants for the moment
struct Breakout
{
int paddleX;
bool bricks[MAX_BRICK_Y][MAX_BRICK_X];
};

void update(Breakout &breakout);
void render(const Breakout &breakout);

/*
* Returns false if user indicates they wish to quit
*/
bool input(Breakout &breakout);

/*
* Returns false when no more levels left
*/
bool loadLevel(Breakout &breakout, int level);

/*
* Returns false when all bricks are destroyed
*/
bool isLevelFinished(const Breakout &breakout);

int main()
{
// Miscellaneous initialisation

Breakout breakout;

// Initialise breakout game state

int level = 1;
bool error = loadLevel(breakout, level);
if(error)
{
// Whine and exit
}

bool running = true;
while(running)
{
running = input(breakout);

update(breakout);

render(breakout);

if(running && isLevelFinished(breakout))
{
++level;
running = loadLevel(breakout, level);
}
}

// Cleanup

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!