Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help with C LANGUAGE Basic Functionality (15 Marks) 1,The display screen

ID: 3742280 • Letter: P

Question

please help with C LANGUAGE

Basic Functionality (15 Marks)

1,The display screen is 5 characters high, located at the top of the screen, is entirely visible at all times (i.e. never occluded in any way) and displays:

Student number,

Lives remaining,

Playing time in minutes and seconds format as mm:ss,

Score,

Border that separates the display screen from the game area.

2,The player initially appears on a 'starting block' in the top row. The player’s sprite is at least 3 characters high and 3 characters wide.

3,All blocks are:

2 characters high,

At least 7 characters wide (unless Advanced Functionality is implemented) and are clearly distinguished from each other (through characters in bitmap or spacing between sprites),

Always at least sprite height + 2 characters vertically away from other blocks.

4,Columns are calculated as visible screen width / max block width + horizontal spacing (if used to distinguish between sprites) and there are:

The number of blocks on screen is proportional to the number of columns, to a maximum of 160 safe blocks and 40 forbidden blocks,

At least 1 safe block per column denoted by '=' on the screen at all times,

At least 2 forbidden block on the screen denoted by 'x' on the screen at all times.

5,Blocks have no consistent observable pattern and each block:

Appears in a randomly selected row,

Appears in a randomly selected column (after there is 1 safe block per column),

Does not overlap other blocks or the display screen.

6,Player movement behaves such that:

Pressing the ‘a’ or ‘d’ key moves the character left and right respectively,

The player can not overlap the display screen.

When off a block, the player free falls downwards at an appropriate speed,

No lateral motion occurs during downward motion (unless Advanced Functionality is implemented).

7,There is a treasure sprite that:

Is no larger than the player's sprite and has a clearly distinct image,

Spawns at the bottom of the screen and moves back and forward along the bottom, changing horizontal direction when it reaches the edges of the screen,

Stops moving when 't' is pressed and starts moving again if 't' is pressed again (i.e. motion toggles on and off when 't' is pressed),

Disappears when the player collides with it and gives the player 2 more lives and returns the player to the 'starting block'.

8,The following game mechanics are present:

The player starts with 10 lives.

A point is scored every time the player lands on a safe block.

The player dies when they fall off the bottom of the screen or hit a forbidden block. On death, the player starts from the ‘starting block’ (unless Advanced Functionality is implemented).

When the player loses all their lives, the game over screen is displayed which displays a game over message, total score, and game play time.

The game over screen allows the player to restart by pressing ‘r’ (score, lives, time, and screen all reset) or quit with the ‘q’ key which ends the game gracefully.

Advanced Functionality (7 Marks)

9,Each block has a random width between 5 and 10 and:

The top and bottom rows of blocks remain stationery, but the blocks in other rows move at appropriate speeds (different rows may have different speeds),

Blocks in the same row move at the same speed and direction,

Blocks reappear on the other side of the screen as they go off the screen,

Each row of blocks moves in opposite directions or speeds to the row directly above (e.g. if row 2 moves left, row 3 could move right),

10,Player motion is controlled as follows:

An 'a' or 'd' key press gives the player a horizontal velocity so that if 'a' or 'd' is pressed on a moving block, the player moves along the block accordingly,

When on a block, a 'w' key press gives the player a vertical velocity upwards (pressing 'w' again does not do anything until player lands again),

When the player lands on a block their vertical velocity changes to zero and they adopt the same horizontal velocity as the block,

If the player is on a block and any part of the block goes off screen, the player loses a life and respawns.

11,A constant vertical acceleration (i.e. gravity) acts downwards at all times, generating a reasonably realistic ballistic motion effect when the player is in flight after stepping or jumping from a block. Gravity generates the following visually observable behaviour:

When on a block, the effects of gravity are eliminated – the player does not fall through the block,

When jumping, the player’s vertical velocity is slowest at the peak jump height,

When jumping with a horizontal velocity, the player traces a parabolic path. The horizontal velocity is constant, while the vertical velocity increases at a constant rate with passage of time,

When falling with a non-zero horizontal velocity, the player also traces a parabolic path but starts with zero vertical velocity,

When hitting the side of a block, the player’s horizontal velocity becomes zero.

12,Player randomly respawns on any of the top row safe blocks after dying (doesn't just reappear on the 'starting block').

Advanced Graphics (4 Marks)

13,The treasure has some sort of animation.

14,The player's image changes when:

The player falls or when they jump,

The the player is moving left,

The the player is moving right,

The player is stationery or only moving with the blocks.

15,The player has some sort of animation every time the player respawns.

Advanced Coding Practices (4 marks)

16,Your program has minimal code duplication and is cleanly structured for a human reader.

17,You have used arrays or pointers where appropriate.

18,Your program is composed of functions. A function should only perform one task and not exceed 20 lines of code.

19,You have used parameters where appropriate and minimised the number of global variables.

For this assignment, you will be writing a computer program that emulates a player moving through a series of static and moving blocks. The aim of the player is to move/jump between blocks while gaining points and lives Lives: 10 Time: 00:10 Score: 1 c PsE XXXXXXX 0

Explanation / Answer

PROGRAM IN C LANGUAGE:

#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*/

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: cgi " ) ;

mainscreen();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

/* 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<< "Balls :" ;

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

{

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

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

}

/* display starting score */

gotoxy ( 1, 25 ) ;

cout<< "Score: ";

gotoxy(16,25);

cout<<score;

/* select font and alignment for displaying text */

settextjustify ( CENTER_TEXT, CENTER_TEXT ) ;

changetextstyle ( GOTHIC_FONT, HORIZ_DIR, 4 ) ;

while ( 1 )

{

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 is determined

*/

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

{

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 <= 6 ; i++ )

{

/* check whether there is a brick to the right of the ball */

if ( bri[layer][ ( BallX + i + 10 ) / 32 ] == 0 )

{

/* if there is a brick */

BallX = BallX + i ;

flag = 1 ;

break ;

}

/* check whether there is a brick to the left of the ball */

if ( bri[layer][ ( BallX - i + 10 ) / 32 ] == 0 )

{

BallX = BallX - i ;

flag = 1 ;

break ;

}

}

/* if the ball does not touch a brick at the top, left or right */

if ( !flag )

{

/* check if the ball has moved above the current layer */

if ( BallY < totallayer[layer - 1] )

{

/* if so, change current layer appropriately */

layer-- ;

max = totallayer[layer] ;

}

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

putimage ( OldX, OldY, m1, OR_PUT ) ;

/* erase the image at the old coordinates */

putimage ( OldX, OldY, m1, XOR_PUT ) ;

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

putimage ( BallX, BallY, m1, XOR_PUT ) ;

/* delay for fewseconds*/

delay ( speed ) ;

/* carry on moving the ball */

continue ;

}

}

/* when ball touch the brick control comes to this point */

bell ( 5 ) ; /* play music */

/* erase the touched brick */

delbrick ( ( BallX + 10 ) / 32, layer ) ;

/* if the brick hit is on the extreme right */

if ( ( BallX + 10 ) / 32 == 19 )

line ( MaxX, 0, MaxX, 50 ) ; /* redraw right boundary */

/* if the brick hit is on the extreme left */

if ( ( BallX + 10 ) / 32 == 0 )

line ( 0, 0, 0, 50 ) ; /* redraw left boundary */

/* if the brick hit is in the topmost layer */

if ( layer == 0 )

line ( 0, 0, MaxX, 0 ) ; /* redraw top boundary */

/* set appropriate array element to 1 to indicate absence of brick */

bri[layer][ ( BallX + 10 ) / 32 ] = 1 ;

BallY = BallY + 1 ; /* update the y coordinate */

dy = -dy ; /* change the current direction of the ball */

score += 10 ; /* increment the score by 10 */

gotoxy ( 16, 25 ) ;

cout<< score; /* display latest score */

/* if the first brick is hit during a throw*/

}

/* if ball reached the bottom */

if ( BallY > 180 - 12 )

{

/* if paddle missed the ball */

if ( BallX < Base1 - 20 || BallX > 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<< score; /* display latest score */

bell ( 1 ) ;

/* erase one of the available balls */

if ( chance )

putimage ( 515 + ( chance - 1 ) * 35 - 12 , MaxY - 10, m1, XOR_PUT )

;

/* if the last ball is being played */

if ( chance == 1 )

{

gotoxy ( 45, 25 ) ;

cout<< "Last ball... careful!";

}

/* if all the balls are lost */

if ( !chance )

{

gotoxy ( 45, 25 ) ;

cout<<"Press any key to continue ... " ;

outtextxy ( MidX, MidY, "Better luck next time" ) ;

bell ( 2 ) ;

closegraph() ;

restorecrtmode() ;

closegraph(); /* Close the graphics mode */

Initialize(); /* Intialize the graphics mode */

setbkcolor(4);

SayGoodbye(); /* Display the That's all Folks message */

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

exit ( 0 ) ;

}

}

/* if ball is collected on paddle */

bell ( 4 ) ;

BallY = 180 - 12 ; /* restore the y coordinate of ball */

dy = -dy ; /* move the ball upwards */

}

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

putimage ( OldX, OldY, m1, OR_PUT ) ;

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

putimage ( OldX, OldY, m1, XOR_PUT ) ;

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

putimage ( BallX, BallY, m1, XOR_PUT ) ;

/* if all the bricks have been knockout */

if ( score == 500 - ( ( 4 - chance ) * 20 ) )

{

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

if ( score < 600 )

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

else

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

bell ( 2 ) ;

closegraph() ;

restorecrtmode() ;

exit ( 0 ) ;

}

/* introduce delay for few seconds */

delay ( speed ) ;

/* if the key is pressed to move the paddle */

if ( kbhit() )

{

/* interrupt issue to obtain the ascii and scan codes of key hit */

ii.h.ah = 0 ;

int86 ( 22, &ii, &oo ) ;

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

/* if Esc key has been pressed */

if ( oo.h.ah == 1 )

exit ( 0 ) ;

/* right arrow key */

if ( oo.h.ah == 75 )

Base1 = Base1 - 25 ;

/* left arrow key */

if ( oo.h.ah == 77 )

Base1= Base1 + 25 ;

/* if paddle goes beyond left boundary */

if ( Base1 < 0 )

Base1 = 0 ;

/* if paddle goes beyond right boundary */

if ( Base1 > 589 )

Base1 = 589 ;

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

putimage ( Base1, Base2, m2, XOR_PUT ) ;

}

}

closegraph();

Initialize();

SayGoodbye(); /* Give user the closing screen */

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

return(0);

}

/* This function creates the opening screen and

displyed instruction related to the game */

screen()

{

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

rectangle(1,1,600,195);

setbkcolor(4);

/* set the textstyle for displaying instruction */

changetextstyle ( DEFAULT_FONT, HORIZ_DIR, 0 ) ;

outtextxy ( 150, 55, " Instructions " ) ;

changetextstyle (4, HORIZ_DIR, 5 );

outtextxy ( 130, 0, " B R I C K S" ) ;

changetextstyle ( DEFAULT_FONT, HORIZ_DIR, 0 ) ;

outtextxy ( 30, 68, "Use left and right arrow keys to move paddle." ) ;

outtextxy ( 30, 88, "If you don't collect the ball on the paddle, you

lose the ball." ) ;

outtextxy ( 30, 108, "On loosing a ball you loose 20 points." ) ;

outtextxy ( 30, 128, "On taking a brick you gain 5 points." ) ;

changetextstyle(7, HORIZ_DIR, 3);

outtextxy ( 100, 148, "Press any key to continue ..." ) ;

bell ( 3 ) ; /* ring music */

fflush ( stdin ) ;

if ( getch() == 0 )

getch() ;

closegraph();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

rectangle(2,2,620,195);

setbkcolor(4);

/* display the main menu */

while ( 1 )

{

changetextstyle(4,HORIZ_DIR,5);

outtextxy ( 60, 8, "Options Available:" ) ;

outtextxy ( 150, 55, "Play ( P )" ) ;

outtextxy ( 150, 125, "Exit ( E )" ) ;

ch = 0 ;

/* continue untill you select the correct choice */

while ( ! ( ch == 'E' || ch == 'P' ) )

{

fflush ( stdin ) ;

/* if a special key is hit, flush the keyboard buffer */

if ( ( ch = getch() ) == 0 )

getch() ;

else

ch = toupper ( ch ) ; /* store the uppercase of the choice made*/

}

if ( ch == 'P' )

break ;

if (ch == 'E')

exit ( 0 ) ;

}

setviewport ( 1, 125 - 12, MaxX - 1, MaxY - 1, 1 ) ;

clearviewport() ;

closegraph();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

rectangle(2,2,620,195);

setbkcolor(4);

/* display menu for the diffrent levels */

changetextstyle(7,HORIZ_DIR,3);

outtextxy ( 60, 8, "Select the level for play:" ) ;

outtextxy ( 150,50, "Slow ( S )" ) ;

outtextxy ( 150, 100, "Medium ( M )" ) ;

outtextxy ( 150, 150, "Fast ( F )" ) ;

/* accept user's choice */

fflush ( stdin ) ;

if ( ( ch = getch() ) == 0 )

getch() ;

clearviewport() ;

/* return the choice selected by the user */

return ( ch ) ;

}

/* This function draws bricks at the start of the game.There are four

layers of

the bricks */

bricks()

{

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

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

{

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

{

/* draw a brick at appropriate coordinates */

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

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

lx = lx + 32 ;

}

lx = 0 ;

ly = ly + 10 ;

}

}

/* This function erases the brick which is knock by the ball */

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

}

/* plays different types of music */

bell ( int m_no )

{

/* natural frequencies of 7 notes */

float wave[6] = { 120.81, 136.83, 144.81, 154.61, 216, 240 } ;

int n, i ;

switch ( m_no )

{

case 1 :

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

{

sound ( wave[i] * 1 ) ;

delay ( 30 ) ;

}

nosound() ;

break ;

case 2 :

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

{

n = random ( 6 ) ;

sound ( wave[n] * 2 ) ;

delay ( 100 ) ;

}

nosound() ;

break ;

case 3 :

while ( !kbhit() )

{

n = random ( 6 ) ;

sound ( wave[n] * 2 ) ;

delay ( 100 ) ;

}

nosound() ;

/* flush the keyboard buffer */

if ( getch() == 0 )

getch() ;

break ;

case 4 :

for ( i = 5 ; i >= 0 ; i-- )

{

sound ( wave[i] * 4 ) ;

delay ( 15 ) ;

}

nosound() ;

break ;

case 5 :

sound ( wave[2] * 5 ) ;

delay ( 50 ) ;

nosound() ;

}

}

/* */

/* INITIALIZE: Initializes the graphics system and reports */

/* any errors which occured. */

/* */

void Initialize(void)

{

int xasp, yasp; /* Used to read the aspect ratio*/

GraphDriver = DETECT; /* Request auto-detection */

initgraph( &GraphDriver, &GraphMode, "c: cgi" );

ErrorCode = graphresult(); /* Read result of initialization*/

if( ErrorCode != grOk ){ /* Error occured during init */

printf(" Graphics System Error: %s

", grapherrormsg( ErrorCode ) );

exit( 1 );

}

getpalette( &palette ); /* Read the palette from board */

MaxColors = getmaxcolor() + 1; /* Read maximum number of colors*/

MaxXX = getmaxx();

MaxYY = getmaxy(); /* Read size of screen */

getaspectratio( &xasp, &yasp ); /* read the hardware aspect */

AspectRatio = (double)xasp / (double)yasp; /* Get correction factor */

}

/* */

/* SAYGOODBYE: Give a closing screen to the user before leaving. */

/* */

void SayGoodbye(void)

{

struct viewporttype viewinfo; /* Structure to read viewport */

int h, w;

MainWindow( "== Finale ==" );

getviewsettings( &viewinfo ); /* Read viewport settings */

changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );

settextjustify( CENTER_TEXT, CENTER_TEXT );

h = viewinfo.bottom - viewinfo.top;

w = viewinfo.right - viewinfo.left;

outtextxy( w/2, h/2, "That's all, folks!" );

StatusLine( "Press any key to EXIT" );

getch();

cleardevice(); /* Clear the graphics screen */

}

/* */

/* MAINWINDOW: Establish the main window for the demo and set */

/* a viewport for the demo code. */

/* */

void MainWindow( char *header )

{

int height;

cleardevice(); /* Clear graphics screen */

setcolor( MaxColors - 1 ); /* Set current color to white */

setviewport( 0, 0, MaxXX, MaxYY, 1 ); /* Open port to full screen */

height = textheight( "H" ); /* Get basic text height */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );

settextjustify( CENTER_TEXT, TOP_TEXT );

outtextxy( MaxXX/2, 2, header );

setviewport( 0, height+4, MaxXX, MaxYY-(height+4), 1 );

DrawBorder();

setviewport( 1, height+5, MaxXX-1, MaxYY-(height+5), 1 );

}

/* */

/* STATUSLINE: Display a status line at the bottom of the screen. */

/* */

void StatusLine( char *msg )

{

int height;

setviewport( 0, 0, MaxXX, MaxYY, 1 ); /* Open port to full screen */

setcolor( MaxColors - 1 ); /* Set current color to white */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );

settextjustify( CENTER_TEXT, TOP_TEXT );

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

setfillstyle( EMPTY_FILL, 0 );

height = textheight( "H" ); /* Detemine current height */

bar( 0, MaxYY-(height+4), MaxXX, MaxYY );

rectangle( 0, MaxYY-(height+4), MaxXX, MaxYY );

outtextxy( MaxXX/2, MaxYY-(height+2), msg );

setviewport( 1, height+5, MaxXX-1, MaxYY-(height+5), 1 );

}

/* */

/* DRAWBORDER: Draw a solid single line around the current */

/* viewport. */

/* */

void DrawBorder(void)

{

struct viewporttype vp;

setcolor( MaxColors - 1 ); /* Set current color to white */

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

getviewsettings( &vp );

rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );

}

/* */

/* CHANGETEXTSTYLE: similar to settextstyle, but checks for */

/* errors that might occur whil loading the font file. */

/* */

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

{

int ErrorCode;

graphresult(); /* clear error code */

settextstyle(font, direction, charsize);

ErrorCode = graphresult(); /* check result */

if( ErrorCode != grOk ){ /* if error occured */

closegraph();

printf(" Graphics System Error: %s

", grapherrormsg( ErrorCode ) );

exit( 1 );

}

}

/* The main function body of main screen which displays the main screen

creating the opening screen display */

mainscreen()

{

int maxx, maxy, in, area;

// get maximum x, y coordinates of the screen

maxx = getmaxx();

maxy = getmaxy();

// setbkcolor sets the current background color using the palette

setbkcolor(RED);

// Draws a rectangle (graphics mode)

rectangle(0, 0, maxx, maxy);

// sets the line style and text justification in screen

changetextstyle(1, HORIZ_DIR, 0);

// displaying the output text on main screen

outtextxy(220, 20, "WELCOME");

outtextxy(240,60," TO ");

outtextxy(220,100," BRICKS");

changetextstyle(7, HORIZ_DIR, 3);

bell(3);

outtextxy(110, 150, "Press any key to continue...");

// Flushes the standard input device

fflush(stdin);

getch();

closegraph();

}

PROGRAM IN JAVA LANGUAGE:

package net.obviam.droidz.model.components;

public class Speed {

public static final int DIRECTION_RIGHT = 1;

public static final int DIRECTION_LEFT = -1;

public static final int DIRECTION_UP = -1;

public static final int DIRECTION_DOWN = 1;

private float xv = 1; // velocity value on the X axis

private float yv = 1; // velocity value on the Y axis

private int xDirection = DIRECTION_RIGHT;

private int yDirection = DIRECTION_DOWN;

public Speed() {

this.xv = 1;

this.yv = 1;

}

public Speed(float xv, float yv) {

this.xv = xv;

this.yv = yv;

}

public float getXv() {

return xv;

}

public void setXv(float xv) {

this.xv = xv;

}

public float getYv() {

return yv;

}

public void setYv(float yv) {

this.yv = yv;

}

public int getxDirection() {

return xDirection;

}

public void setxDirection(int xDirection) {

this.xDirection = xDirection;

}

public int getyDirection() {

return yDirection;

}

public void setyDirection(int yDirection) {

this.yDirection = yDirection;

}

public void toggleXDirection() {

xDirection = xDirection * -1;

}

public void toggleYDirection() {

yDirection = yDirection * -1;

}

}

PROGRAM IN C LANGUAGE:

#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*/

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: cgi " ) ;

mainscreen();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

/* 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<< "Balls :" ;

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

{

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

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

}

/* display starting score */

gotoxy ( 1, 25 ) ;

cout<< "Score: ";

gotoxy(16,25);

cout<<score;

/* select font and alignment for displaying text */

settextjustify ( CENTER_TEXT, CENTER_TEXT ) ;

changetextstyle ( GOTHIC_FONT, HORIZ_DIR, 4 ) ;

while ( 1 )

{

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 is determined

*/

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

{

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 <= 6 ; i++ )

{

/* check whether there is a brick to the right of the ball */

if ( bri[layer][ ( BallX + i + 10 ) / 32 ] == 0 )

{

/* if there is a brick */

BallX = BallX + i ;

flag = 1 ;

break ;

}

/* check whether there is a brick to the left of the ball */

if ( bri[layer][ ( BallX - i + 10 ) / 32 ] == 0 )

{

BallX = BallX - i ;

flag = 1 ;

break ;

}

}

/* if the ball does not touch a brick at the top, left or right */

if ( !flag )

{

/* check if the ball has moved above the current layer */

if ( BallY < totallayer[layer - 1] )

{

/* if so, change current layer appropriately */

layer-- ;

max = totallayer[layer] ;

}

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

putimage ( OldX, OldY, m1, OR_PUT ) ;

/* erase the image at the old coordinates */

putimage ( OldX, OldY, m1, XOR_PUT ) ;

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

putimage ( BallX, BallY, m1, XOR_PUT ) ;

/* delay for fewseconds*/

delay ( speed ) ;

/* carry on moving the ball */

continue ;

}

}

/* when ball touch the brick control comes to this point */

bell ( 5 ) ; /* play music */

/* erase the touched brick */

delbrick ( ( BallX + 10 ) / 32, layer ) ;

/* if the brick hit is on the extreme right */

if ( ( BallX + 10 ) / 32 == 19 )

line ( MaxX, 0, MaxX, 50 ) ; /* redraw right boundary */

/* if the brick hit is on the extreme left */

if ( ( BallX + 10 ) / 32 == 0 )

line ( 0, 0, 0, 50 ) ; /* redraw left boundary */

/* if the brick hit is in the topmost layer */

if ( layer == 0 )

line ( 0, 0, MaxX, 0 ) ; /* redraw top boundary */

/* set appropriate array element to 1 to indicate absence of brick */

bri[layer][ ( BallX + 10 ) / 32 ] = 1 ;

BallY = BallY + 1 ; /* update the y coordinate */

dy = -dy ; /* change the current direction of the ball */

score += 10 ; /* increment the score by 10 */

gotoxy ( 16, 25 ) ;

cout<< score; /* display latest score */

/* if the first brick is hit during a throw*/

}

/* if ball reached the bottom */

if ( BallY > 180 - 12 )

{

/* if paddle missed the ball */

if ( BallX < Base1 - 20 || BallX > 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<< score; /* display latest score */

bell ( 1 ) ;

/* erase one of the available balls */

if ( chance )

putimage ( 515 + ( chance - 1 ) * 35 - 12 , MaxY - 10, m1, XOR_PUT )

;

/* if the last ball is being played */

if ( chance == 1 )

{

gotoxy ( 45, 25 ) ;

cout<< "Last ball... careful!";

}

/* if all the balls are lost */

if ( !chance )

{

gotoxy ( 45, 25 ) ;

cout<<"Press any key to continue ... " ;

outtextxy ( MidX, MidY, "Better luck next time" ) ;

bell ( 2 ) ;

closegraph() ;

restorecrtmode() ;

closegraph(); /* Close the graphics mode */

Initialize(); /* Intialize the graphics mode */

setbkcolor(4);

SayGoodbye(); /* Display the That's all Folks message */

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

exit ( 0 ) ;

}

}

/* if ball is collected on paddle */

bell ( 4 ) ;

BallY = 180 - 12 ; /* restore the y coordinate of ball */

dy = -dy ; /* move the ball upwards */

}

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

putimage ( OldX, OldY, m1, OR_PUT ) ;

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

putimage ( OldX, OldY, m1, XOR_PUT ) ;

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

putimage ( BallX, BallY, m1, XOR_PUT ) ;

/* if all the bricks have been knockout */

if ( score == 500 - ( ( 4 - chance ) * 20 ) )

{

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

if ( score < 600 )

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

else

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

bell ( 2 ) ;

closegraph() ;

restorecrtmode() ;

exit ( 0 ) ;

}

/* introduce delay for few seconds */

delay ( speed ) ;

/* if the key is pressed to move the paddle */

if ( kbhit() )

{

/* interrupt issue to obtain the ascii and scan codes of key hit */

ii.h.ah = 0 ;

int86 ( 22, &ii, &oo ) ;

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

/* if Esc key has been pressed */

if ( oo.h.ah == 1 )

exit ( 0 ) ;

/* right arrow key */

if ( oo.h.ah == 75 )

Base1 = Base1 - 25 ;

/* left arrow key */

if ( oo.h.ah == 77 )

Base1= Base1 + 25 ;

/* if paddle goes beyond left boundary */

if ( Base1 < 0 )

Base1 = 0 ;

/* if paddle goes beyond right boundary */

if ( Base1 > 589 )

Base1 = 589 ;

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

putimage ( Base1, Base2, m2, XOR_PUT ) ;

}

}

closegraph();

Initialize();

SayGoodbye(); /* Give user the closing screen */

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

return(0);

}

/* This function creates the opening screen and

displyed instruction related to the game */

screen()

{

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

rectangle(1,1,600,195);

setbkcolor(4);

/* set the textstyle for displaying instruction */

changetextstyle ( DEFAULT_FONT, HORIZ_DIR, 0 ) ;

outtextxy ( 150, 55, " Instructions " ) ;

changetextstyle (4, HORIZ_DIR, 5 );

outtextxy ( 130, 0, " B R I C K S" ) ;

changetextstyle ( DEFAULT_FONT, HORIZ_DIR, 0 ) ;

outtextxy ( 30, 68, "Use left and right arrow keys to move paddle." ) ;

outtextxy ( 30, 88, "If you don't collect the ball on the paddle, you

lose the ball." ) ;

outtextxy ( 30, 108, "On loosing a ball you loose 20 points." ) ;

outtextxy ( 30, 128, "On taking a brick you gain 5 points." ) ;

changetextstyle(7, HORIZ_DIR, 3);

outtextxy ( 100, 148, "Press any key to continue ..." ) ;

bell ( 3 ) ; /* ring music */

fflush ( stdin ) ;

if ( getch() == 0 )

getch() ;

closegraph();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

rectangle(2,2,620,195);

setbkcolor(4);

/* display the main menu */

while ( 1 )

{

changetextstyle(4,HORIZ_DIR,5);

outtextxy ( 60, 8, "Options Available:" ) ;

outtextxy ( 150, 55, "Play ( P )" ) ;

outtextxy ( 150, 125, "Exit ( E )" ) ;

ch = 0 ;

/* continue untill you select the correct choice */

while ( ! ( ch == 'E' || ch == 'P' ) )

{

fflush ( stdin ) ;

/* if a special key is hit, flush the keyboard buffer */

if ( ( ch = getch() ) == 0 )

getch() ;

else

ch = toupper ( ch ) ; /* store the uppercase of the choice made*/

}

if ( ch == 'P' )

break ;

if (ch == 'E')

exit ( 0 ) ;

}

setviewport ( 1, 125 - 12, MaxX - 1, MaxY - 1, 1 ) ;

clearviewport() ;

closegraph();

initgraph ( &graphdriver, &graphmode, "c: cgi " ) ;

rectangle(2,2,620,195);

setbkcolor(4);

/* display menu for the diffrent levels */

changetextstyle(7,HORIZ_DIR,3);

outtextxy ( 60, 8, "Select the level for play:" ) ;

outtextxy ( 150,50, "Slow ( S )" ) ;

outtextxy ( 150, 100, "Medium ( M )" ) ;

outtextxy ( 150, 150, "Fast ( F )" ) ;

/* accept user's choice */

fflush ( stdin ) ;

if ( ( ch = getch() ) == 0 )

getch() ;

clearviewport() ;

/* return the choice selected by the user */

return ( ch ) ;

}

/* This function draws bricks at the start of the game.There are four

layers of

the bricks */

bricks()

{

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

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

{

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

{

/* draw a brick at appropriate coordinates */

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

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

lx = lx + 32 ;

}

lx = 0 ;

ly = ly + 10 ;

}

}

/* This function erases the brick which is knock by the ball */

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

}

/* plays different types of music */

bell ( int m_no )

{

/* natural frequencies of 7 notes */

float wave[6] = { 120.81, 136.83, 144.81, 154.61, 216, 240 } ;

int n, i ;

switch ( m_no )

{

case 1 :

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

{

sound ( wave[i] * 1 ) ;

delay ( 30 ) ;

}

nosound() ;

break ;

case 2 :

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

{

n = random ( 6 ) ;

sound ( wave[n] * 2 ) ;

delay ( 100 ) ;

}

nosound() ;

break ;

case 3 :

while ( !kbhit() )

{

n = random ( 6 ) ;

sound ( wave[n] * 2 ) ;

delay ( 100 ) ;

}

nosound() ;

/* flush the keyboard buffer */

if ( getch() == 0 )

getch() ;

break ;

case 4 :

for ( i = 5 ; i >= 0 ; i-- )

{

sound ( wave[i] * 4 ) ;

delay ( 15 ) ;

}

nosound() ;

break ;

case 5 :

sound ( wave[2] * 5 ) ;

delay ( 50 ) ;

nosound() ;

}

}

/* */

/* INITIALIZE: Initializes the graphics system and reports */

/* any errors which occured. */

/* */

void Initialize(void)

{

int xasp, yasp; /* Used to read the aspect ratio*/

GraphDriver = DETECT; /* Request auto-detection */

initgraph( &GraphDriver, &GraphMode, "c: cgi" );

ErrorCode = graphresult(); /* Read result of initialization*/

if( ErrorCode != grOk ){ /* Error occured during init */

printf(" Graphics System Error: %s

", grapherrormsg( ErrorCode ) );

exit( 1 );

}

getpalette( &palette ); /* Read the palette from board */

MaxColors = getmaxcolor() + 1; /* Read maximum number of colors*/

MaxXX = getmaxx();

MaxYY = getmaxy(); /* Read size of screen */

getaspectratio( &xasp, &yasp ); /* read the hardware aspect */

AspectRatio = (double)xasp / (double)yasp; /* Get correction factor */

}

/* */

/* SAYGOODBYE: Give a closing screen to the user before leaving. */

/* */

void SayGoodbye(void)

{

struct viewporttype viewinfo; /* Structure to read viewport */

int h, w;

MainWindow( "== Finale ==" );

getviewsettings( &viewinfo ); /* Read viewport settings */

changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );

settextjustify( CENTER_TEXT, CENTER_TEXT );

h = viewinfo.bottom - viewinfo.top;

w = viewinfo.right - viewinfo.left;

outtextxy( w/2, h/2, "That's all, folks!" );

StatusLine( "Press any key to EXIT" );

getch();

cleardevice(); /* Clear the graphics screen */

}

/* */

/* MAINWINDOW: Establish the main window for the demo and set */

/* a viewport for the demo code. */

/* */

void MainWindow( char *header )

{

int height;

cleardevice(); /* Clear graphics screen */

setcolor( MaxColors - 1 ); /* Set current color to white */

setviewport( 0, 0, MaxXX, MaxYY, 1 ); /* Open port to full screen */

height = textheight( "H" ); /* Get basic text height */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );

settextjustify( CENTER_TEXT, TOP_TEXT );

outtextxy( MaxXX/2, 2, header );

setviewport( 0, height+4, MaxXX, MaxYY-(height+4), 1 );

DrawBorder();

setviewport( 1, height+5, MaxXX-1, MaxYY-(height+5), 1 );

}

/* */

/* STATUSLINE: Display a status line at the bottom of the screen. */

/* */

void StatusLine( char *msg )

{

int height;

setviewport( 0, 0, MaxXX, MaxYY, 1 ); /* Open port to full screen */

setcolor( MaxColors - 1 ); /* Set current color to white */

changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );

settextjustify( CENTER_TEXT, TOP_TEXT );

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

setfillstyle( EMPTY_FILL, 0 );

height = textheight( "H" ); /* Detemine current height */

bar( 0, MaxYY-(height+4), MaxXX, MaxYY );

rectangle( 0, MaxYY-(height+4), MaxXX, MaxYY );

outtextxy( MaxXX/2, MaxYY-(height+2), msg );

setviewport( 1, height+5, MaxXX-1, MaxYY-(height+5), 1 );

}

/* */

/* DRAWBORDER: Draw a solid single line around the current */

/* viewport. */

/* */

void DrawBorder(void)

{

struct viewporttype vp;

setcolor( MaxColors - 1 ); /* Set current color to white */

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );

getviewsettings( &vp );

rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );

}

/* */

/* CHANGETEXTSTYLE: similar to settextstyle, but checks for */

/* errors that might occur whil loading the font file. */

/* */

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

{

int ErrorCode;

graphresult(); /* clear error code */

settextstyle(font, direction, charsize);

ErrorCode = graphresult(); /* check result */

if( ErrorCode != grOk ){ /* if error occured */

closegraph();

printf(" Graphics System Error: %s

", grapherrormsg( ErrorCode ) );

exit( 1 );

}

}

/* The main function body of main screen which displays the main screen

creating the opening screen display */

mainscreen()

{

int maxx, maxy, in, area;

// get maximum x, y coordinates of the screen

maxx = getmaxx();

maxy = getmaxy();

// setbkcolor sets the current background color using the palette

setbkcolor(RED);

// Draws a rectangle (graphics mode)

rectangle(0, 0, maxx, maxy);

// sets the line style and text justification in screen

changetextstyle(1, HORIZ_DIR, 0);

// displaying the output text on main screen

outtextxy(220, 20, "WELCOME");

outtextxy(240,60," TO ");

outtextxy(220,100," BRICKS");

changetextstyle(7, HORIZ_DIR, 3);

bell(3);

outtextxy(110, 150, "Press any key to continue...");

// Flushes the standard input device

fflush(stdin);

getch();

closegraph();

}