answersLogoWhite

0

#include <iostream.h>

#include <conio.h>

# include <process.h> //exit(0)

# include <dos.h>

# include <stdlib.h>

# include <graphics.h>

# include <stdio.h>

int MaxX, MaxY, MidX, MidY ;

int bri[5][20] ;

bricks(); //to display bricks and paddle and bulb rounded form

delbrick(int,int);

int graphmode= CGAHI, graphdriver = CGA; //CGAHI = 640 x 200 (col x row)

void MousePos(int* x)

{ union REGS in,out;

in.x.ax=3;

int86(0x33,&in,&out);

*x=(int)out.x.cx;

}

main()

{

int BallX, BallY, Base1, Base2, dx = 1, dy = -1, OldX, OldY ;

int totallayer[5] = { 10, 20, 30, 40, 50 }, max = 50, layer = 4 ;

int i, flag = 0, speed = 10, score = 0, chance = 4, areareq ;

char *m1, *m2 ; //pointers to memory

initgraph ( &graphdriver, &graphmode, "c:\\tc\\bgi" ) ;

MaxX = getmaxx() ;

MaxY = getmaxy() ;

MidX = MaxX / 2 ;

MidY = MaxY / 2 ;

/* draw the four layer of bricks, the paddle and the ball */

rectangle ( 0, 0, MaxX, MaxY - 12 ) ;

bricks() ;

rectangle ( MidX - 25, MaxY - 19, MidX + 25, MaxY - 12 ) ;

circle ( MidX, MaxY -25, 12 ) ;

/* memory allocation for storing the image of the ball */

areareq = imagesize ( MidX - 12, MaxY - 18, MidX + 12, MaxY - 8 ) ;

m1 =((char*) malloc ( areareq )) ;

/* memory allocation for storing the image of the paddle */

areareq = imagesize ( MidX - 25, MaxY - 7, MidX + 25, MaxY - 1 ) ;

m2 =((char *) malloc ( areareq ) );

/* image of the paddle and the ball is stored into allocated memory */

getimage ( MidX - 12, MaxY - 7 - 12 - 12 + 1, MidX + 12, MaxY - 8 -12,m1 ) ;

getimage ( MidX - 25, MaxY - 7 - 12, MidX + 25, MaxY - 1 - 12, m2 ) ;

/* store current position of the paddle and ball */

Base1 = MidX - 25 ;

Base2 = MaxY - 19 ;

BallX = MidX - 12 ;

BallY = MaxY - 7 - 12 + 1 - 12 ;

/* display balls remaining ( initially 3 ) */

gotoxy ( 45, 25 ) ;

cout<< "Balls :" ;

for ( i = 0 ; i < 3 ; i++ )

{

circle ( 515 + i * 35, MaxY - 5, 12 ) ;

}

/* display starting score */

gotoxy ( 1, 25 ) ;

cout<< "Score: ";

gotoxy(16,25);

cout<<score;

while ( !kbhit() )

{

flag = 0 ;

/* saving current x and y coordinates of the ball */

OldX = BallX ;

OldY = BallY ;

/* update ballx and bally to move the ball in correct direction */

BallX = BallX + dx ;

BallY = BallY + dy ;

/* according to the position of ball the layer of bricks isdetermined*/

if ( BallY > 40 )

{

max = 50 ;

layer = 4 ;

}

else

{

if ( BallY > 30 )

{

max = 40 ;

layer = 3 ;

}

else

{

if ( BallY > 20 )

{

max = 30 ;

layer = 2 ;

}

else

{

if ( BallY > 10 )

{

max = 20 ;

layer = 1 ;

}

else

{

max = 10 ;

layer = 0 ;

}

}

}

}

/* if the ball hits the right boundary, move it to the left */

if ( BallX > ( MaxX - 24 - 1 ) )

{

BallX = MaxX - 24 - 1 ;

dx = -dx ;

}

/* if the ball hits the left boundary, move it to the right */

if ( BallX < 1 )

{

BallX = 1 ;

dx = -dx ;

}

/* if the ball hits the top boundary, move it down */

if ( BallY < 1 )

{

BallY = 1 ;

dy = -dy ;

}

/* if the ball is in the area of the bricks */

if ( BallY < max )

{

/* if there is no brick at the top of the ball */

if ( bri[layer][ ( BallX + 10 ) / 32 ] 800 - ( ( 4 - chance ) * 10 ) )

{

outtextxy ( MidX, MidY, "Winner !!" ) ;

if ( score < 800 )

outtextxy ( MidX, MidY + 30, "Try to score 800" ) ;

else

outtextxy ( MidX, MidY + 30, " GREAT!" ) ;

closegraph() ;

restorecrtmode() ;

exit ( 0 ) ;

}

/* introduce delay for few seconds */

delay ( speed ) ;

/* put the image of the paddle at the old coordinates */

putimage ( Base1, Base2, m2, OR_PUT ) ;

/* erase the image of the paddle at the old coordinates */

putimage ( Base1, Base2, m2, XOR_PUT ) ;

MousePos(&Base1);

/* if paddle goes beyond left boundary */

if ( Base1 < 1 )

Base1 = 1 ;

/* if paddle goes beyond right boundary */

if ( Base1 > 588 )

Base1 = 588 ;

/* put the image of the paddle at the proper position */

putimage ( Base1, Base2, m2, XOR_PUT ) ;

}

closegraph(); /* Return the system to text mode */

return(0);

}

bricks()

{

int i, j, lx = 0, ly = 0 ;

for ( i = 0 ; i < 5 ; i++ ) /* 5 rows */

{

for ( j = 0 ; j < 20 ; j++ ) /* 20 columns */

{

rectangle ( lx, ly, lx + 20, ly + 7 ) ;

floodfill ( lx + 1, ly + 1, 2 ) ;

lx = lx + 32 ;

}

lx = 0 ;

ly = ly + 10 ;

}

}

delbrick ( int b, int l )

{

/* b - brick number, l - layer */

setcolor ( BLACK ) ;

rectangle ( b * 32, l * 10, ( b * 32 ) + 20 , ( l * 10 ) + 7 ) ;

rectangle ( b * 32 + 1, l * 10, ( b * 32 ) + 20 - 1, ( l * 10 ) + 7 -1 );

rectangle ( b * 32 + 2, l * 10, ( b * 32 ) + 20 - 2, ( l * 10 ) + 7 -2 );

rectangle ( b * 32 + 3, l * 10, ( b * 32 ) + 20 - 3, ( l * 10 ) + 7 -3 );

rectangle ( b * 32 + 4, l * 10, ( b * 32 ) + 20 - 4, ( l * 10 ) + 7 -4 );

rectangle ( b * 32 + 5, l * 10, ( b * 32 ) + 20 - 5, ( l * 10 ) + 7 -5 );

rectangle ( b * 32 + 6, l * 10, ( b * 32 ) + 20 - 6, ( l * 10 ) + 7 -6 );

setcolor ( CGA_YELLOW ) ;

}

User Avatar

Wiki User

13y ago

What else can I help you with?