Code: Select all
void Game::ComposeFrame()
{
int speed = 3;
int r = 0;
int g = 255;
int b = 255;
int q = 6;
int dr = 0;
int dg = 255;
int db = 0;
dx += dxspeed;
dy += dyspeed;
if ( kbd.SpaceIsPressed() )
{
speed = 1;
}
if ( kbd.EnterIsPressed() )
{
speed = 8;
}
if ( kbd.RightIsPressed() )
{
x += speed;
}
if ( kbd.LeftIsPressed() )
{
x -= speed;
}
if ( kbd.UpIsPressed() )
{
y -= speed;
}
if ( kbd.DownIsPressed() )
{
y += speed;
}
if ( x < 5 )
{
x = 5;
}
if ( y < 5 )
{
y = 5;
}
if ( x > 795 )
{
x = 795;
}
if ( y > 595 )
{
y = 595;
}
//next
if ( dx < 5 )
{
dxspeed = -dxspeed;
}
if ( dy < 5 )
{
dyspeed = -dyspeed;
}
if ( dx > 795 )
{
dxspeed = -dxspeed;
}
if ( dy > 595 )
{
dyspeed = -dyspeed;
}
if ( x > dx - q)
{
if ( x < dx + q)
{
if ( y > dy - q)
{
if ( y < dy + q)
{
dx = 300;
dy = 400;
}
}
}
}
gfx.PutPixel ( -1 + x, 3 + y,r,g,b );
gfx.PutPixel ( x, 3 + y,r,g,b );
gfx.PutPixel ( 1 + x, 3 + y,r,g,b );
gfx.PutPixel ( -1 + x,-3 + y,r,g,b );
gfx.PutPixel ( x,-3 + y,r,g,b );
gfx.PutPixel ( 1 + x,-3 + y,r,g,b );
gfx.PutPixel ( 3 + x, -1 + y,r,g,b );
gfx.PutPixel ( 3 + x, y,r,g,b );
gfx.PutPixel ( 3 + x, 1 + y,r,g,b );
gfx.PutPixel ( -3 + x, -1 + y,r,g,b );
gfx.PutPixel ( -3 + x, y,r,g,b );
gfx.PutPixel ( -3 + x, 1 + y,r,g,b );
gfx.PutPixel ( -1 + dx, 3 + dy,dr,dg,db );
gfx.PutPixel ( dx, 3 + dy,dr,dg,db );
gfx.PutPixel ( 1 + dx, 3 + dy,dr,dg,db );
gfx.PutPixel ( -1 + dx,-3 + dy,dr,dg,db );
gfx.PutPixel ( dx,-3 + dy,dr,dg,db );
gfx.PutPixel ( 1 + dx,-3 + dy,dr,dg,db );
gfx.PutPixel ( 3 + dx, -1 + dy,dr,dg,db );
gfx.PutPixel ( 3 + dx, dy,dr,dg,db );
gfx.PutPixel ( 3 + dx, 1 + dy,dr,dg,db );
gfx.PutPixel ( -3 + dx, -1 + dy,dr,dg,db );
gfx.PutPixel ( -3 + dx, dy,dr,dg,db );
gfx.PutPixel ( -3 + dx, 1 + dy,dr,dg,db );
And this is the code in my Game.h file at the bottom.
As you can see, I did the homework, but my ball bounced around the screen. I also tested for collision by testing if the reticle that was movable by the arrow keys (cyan colored) was inside and invisible box surrounding the bouncing reticle (green colored). If it was, then the bouncing ball was set back to its original position.