40
Bricks game in C #include <iostream.h> #include <conio.h> #include <ctype.h> # include <process.h> # include <dos.h> # include <stdlib.h> # include <graphics.h> # include <stdio.h> # define NULL 0 # define YES 1 # define NO 0 #define ESC 0x1b /* Define the escape key */ int MaxX, MaxY, MidX, MidY ; int bri[5][20] ; int GraphDriver; /* The Graphics device driver */ int GraphMode; /* The Graphics mode value */ double AspectRatio; /* Aspect ratio of a pixel on the screen*/

Bricks Game in C

Embed Size (px)

DESCRIPTION

Bricks Game in C

Citation preview

Bricks game in C

#include

#include

#include

# include

# include

# include

# include

# include

# define NULL 0

# define YES 1

# define NO 0

#define ESC0x1b /* Define the escape key */

int MaxX, MaxY, MidX, MidY ;

int bri[5][20] ;

int GraphDriver;/* The Graphics device driver

*/

int GraphMode;

/* The Graphics mode value

*/

double AspectRatio;/* Aspect ratio of a pixel on the screen*/

int MaxXX, MaxYY;/* The maximum resolution of the screen */

int MaxColors;

/* The maximum # of colors available

*/

int ErrorCode;

/* Reports any graphics errors

*/

struct palettetype palette; /* Used to read palette info*/

// Initialize the graphics mode

void Initialize(void);

// Display the last screen of bricks

void SayGoodbye(void);

// Establish the main window for the demo

void MainWindow(char *header);

// Display the message Press any key to continue at last screen

void StatusLine(char *msg);

// Draws a boarder line at last screen

void DrawBorder(void);

// Changes the text style

void changetextstyle(int font, int direction, int charsize);

// Welcome screen of bricks game

mainscreen();

// Instruction messages of bricks game

screen();

// To display the bricks (in square box) and paddles in rectangle form and

bulbs rounded form

bricks();

// Delete a bricks when bulb hit it

delbrick(int,int);

// Echoes different musics

bell(int);

int graphmode = CGAHI, graphdriver = CGA, level;

main

()

{

union REGS ii, oo ;

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 = 25, score = 0, chance = 4, areareq ;

char *m1, *m2 ;

/* Function to initialise the graphics mode */

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

mainscreen();

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

/* get the maximum value of x and y coordinates of the screen */

MaxX = getmaxx() ;

MaxY = getmaxy() ;

/* finding the center of the screen */

MidX = MaxX / 2 ;

MidY = MaxY / 2 ;

/* create opening screen and accept the level of the player's */

level = screen() ;

/* assign the speed to ball as per the level chosen */

switch ( level )

{

case 'M' :

case 'm' :

speed = 15 ;

break ;

case 'F' :

case 'f' :

speed = 10 ;

}

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

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

bricks() ;

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

floodfill ( MidX, MaxY - 1 - 12, 12 ) ;

circle ( MidX, MaxY - 13 - 12, 12 ) ;

floodfill ( MidX, MaxY - 10 - 12, 12 ) ;

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

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

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

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

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

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

/* if unable to alloacte the memory */

if ( m1 == NULL || m2 == NULL )

{

puts ( "Not Enough memory!!" ) ;

exit ( 1 ) ;

}

/* 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 - 7 - 12 ;

BallX = MidX - 12 ;

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

/* display balls remaining ( initially 3 ) */

gotoxy ( 45, 25 ) ;

cout 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 ) )

{

bell ( 5 ) ;

BallX = MaxX - 24 - 1 ;

dx = -dx ;

}

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

if ( BallX < 1 )

{

bell ( 5 ) ;

BallX = 1 ;

dx = -dx ;

}

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

if ( BallY < 1 )

{

bell ( 5 ) ;

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 ] == 1 )

{

/* if the ball touches a brick */

for ( i = 1 ; i Base1 + 50 )

{

/* continue the decrement of the ball */

while ( BallY < 177 )

{

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

putimage ( OldX, OldY, m1, XOR_PUT ) ;

/* put the image of the ball at the new coordinates */

putimage ( BallX, BallY, m1, XOR_PUT ) ;

/* introduce delay for fewseconds */

delay ( speed ) ;

/* saveing 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 ;

}

chance-- ; /* decrement the total number of chances */

score -= 10 ; /* decrement 10 points for each ball lost */

gotoxy ( 16, 25 ) ;

cout