📄 brick_v1.cpp
字号:
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Brick_V1.cpp :
// This is a example brick game. The program was modified from the previous PingPing.CPP
// code by adding the two dimensional array of BRICK data structure. The pingpong racket
// is directed by the arrow Key and mouse cursor.
// The CONSOLA.LIB library was used to support the obsolete CONIO functions.
// See the README.TXT file for details.
//
// AUTHOR: TA-TE LIN
// DEPARTMENT OF BIO-INDUSTRIAL MECHATRONICS ENGINEERING
// NATIONAL TAIWAN UNIVERSITY
//
// DATE: 2004/07/04
//
//////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "consola.h"
#define WINDOW_TOP 1 // Top side y coordinate of the game window
#define WINDOW_LEFT 1 // Left side x coordinate of the game window
#define WINDOW_BOTTOM 22 // Bottom side y coordinate of the game window
#define WINDOW_RIGHT 60 // Right side x coordinate of the game window
#define LEFT 75 // Code for the LEFT arrow key
#define RIGHT 77 // Code for the RIGHT arrow key
#define UP 72 // Code for the UP arrow key
#define DOWN 80 // Code for the DOWN arrow key
#define ROW 6 // Number of rows of bricks
#define COL 8 // Number of column of bricks
void DrawBox(char x, char y, char width, char height, char color); // Draw the boundary box
void DrawBoard(char x, char y, char width, char color); // Draw the racket
void DrawBrick(char x, char y, char width, char color); // Draw the brick
void InitBrick(void); // Draw the initial brick array
void DisplayStatus(int score, int time, char life); // Display the game status
void EndingScreen(void); // Ending screen
char GetKey(void); // Get the keyboard input (get the arrow keys)
struct BRICK
{
char x; // X coordinate of the brick
char y; // Y coordinate of the brick
char color; // Color of the brick
char score; // Score of the brick
char exist; // Status of the brick
}Brick[ROW][COL];
void main(void)
{
char Ball_X = 30; // X coordinate of the the bouncing ball
char Ball_Y = 19; // Y coordinate of the the bouncing ball
char Delta_X = 1; // The incremental vector in X direction
char Delta_Y = -1; // The incremental vector in Y direction
char Board_X = 25; // X coordinate of the pingpong racket
char Board_Y = 21; // Y coordinate of the pingpong racket
char BoardWidth = 10; // Width of the pingpong racket
char BoardDelta_X = 5; // Incremental movement of the pingpong racket
char BrickRow; // Row index of the brick array
char BrickCol; // Column index of the brick array
char Life = 5; // Allowing 5 miss per game
char Key; // Input Key
int Score = 0; // Score of the game
time_t timeStart, timeNow; // Timer variables for the game
// Set the title and default size of this program on the window
settitle("THE BRICK GAME <BIME NTU> Ver. 1.0");
setsizedefault();
// Generate the seed for random numbers
srand( (unsigned)time( NULL ) );
// Draw the window in which the pingpong ball is confined
DrawBox(WINDOW_LEFT,WINDOW_TOP,WINDOW_RIGHT,WINDOW_BOTTOM,YELLOW);
// Draw the window in which the game status is shown
DrawBox(WINDOW_RIGHT,WINDOW_TOP,21,WINDOW_BOTTOM,YELLOW);
// Draw the bricks
InitBrick();
// Set the text color for the bouncing ball
textcolor(CYAN);
// Generate the position for the initial bouncing ball
Ball_X = 30;
Ball_Y = 19;
gotoxy(Ball_X, Ball_Y);
putch('O');
// Draw the pingpong racket
DrawBoard( Board_X, Board_Y, BoardWidth, GREEN );
// User instruction
gotoxy(2,23);
puts("[<- Key] Move left, [-> Key] Move right, [Q] Quit");
// Get the time at the start
time(&timeStart);
while( toupper(Key) != 'Q' && Life > 0 ) // Press 'Q' or 'q' to exit the program
{
gotoxy(Ball_X, Ball_Y);
putch(' '); // Erase the ball at the old position
Ball_X = Ball_X + Delta_X;
Ball_Y = Ball_Y + Delta_Y;
gotoxy(Ball_X, Ball_Y); // Draw the ball at the new position
putch('O');
// Check the user input for racket movement
if( Key == LEFT )
{
DrawBoard( Board_X, Board_Y, BoardWidth, BLACK ); // Erase the previous racket
Board_X = Board_X - BoardDelta_X;
}
if( Key == RIGHT )
{
DrawBoard( Board_X, Board_Y, BoardWidth, BLACK ); // Erase the previous racket
Board_X = Board_X + BoardDelta_X;
}
// Check the valid position of the racket
if( Board_X <= WINDOW_LEFT + 1)
Board_X = WINDOW_LEFT + 1;
if( Board_X >= WINDOW_RIGHT - BoardWidth)
Board_X = WINDOW_RIGHT - BoardWidth;
// Draw the pingpong racket
DrawBoard( Board_X, Board_Y, BoardWidth, GREEN );
// Check the condition when the ball hit the wall
if( Ball_X >= 59 || Ball_X <= 2 )
{
Delta_X = -1*Delta_X; // Reverse direction
std::cout << "\a"; // Sound the speaker when the ball hit the wall
}
if( Ball_Y <= 2 )
{
Delta_Y = 1; // Reverse direction
std::cout << "\a"; // Sound the speaker when the ball hit the wall
}
// Check if the ball hit the racket
if( (Ball_Y == Board_Y-1) && Delta_Y == 1)
{
if( (Ball_X >= Board_X) && (Ball_X <= Board_X + BoardWidth) )
{
Delta_Y = -1; // Reverse direction
std::cout << "\a"; // Sound the speaker when the ball hit the racket
}
}
// Check if the ball hit any brick
BrickRow = (Ball_Y-4)/2;
BrickCol = (Ball_X-3)/7;
if( (BrickRow >=0 && BrickRow <=5) && (BrickCol >=0 && BrickCol <=7) )
{
if( Brick[BrickRow][BrickCol].exist )
{
Brick[BrickRow][BrickCol].exist = 0;
Score = Score + Brick[BrickRow][BrickCol].score;
Delta_Y = -1*Delta_Y; // Reverse direction
std::cout << "\a"; // Sound the speaker when the ball hit the racket
// Erase the brick hit by the ball
DrawBrick(Brick[BrickRow][BrickCol].x, Brick[BrickRow][BrickCol].y, 7, BLACK);
}
}
// Check if the ball was not catched by the racket (out of bottom boundary)
if( Ball_Y >= 21 )
{
Life = Life - 1;
gotoxy(Ball_X, Ball_Y);
putch(' '); // Erase the ball at the old position
// Generate the position for the initial bouncing ball
Ball_X = Board_X;
Ball_Y = Board_Y-1;
Delta_X = 1;
Delta_Y = -1;
std::cout << "\a\a\a"; // Sound the speaker
}
delay(100); // Delay the speed a little bit (adjustable)
// Reset the input
Key = 0;
if(kbhit()) // Check if any Key is pressed and get the Key
Key = GetKey();
// Get the current time
time(&timeNow);
// Show the game status
DisplayStatus(Score, (int) (timeNow - timeStart), Life );
}
EndingScreen();
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Draw the boundary box
//
//////////////////////////////////////////////////////////////////////////////////////////////
void DrawBox(char x, char y, char width, char height, char color)
{
// get and save the current text background
int old_color = gettextbackground();
// Set text background color
textbackground(color);
// Draw the two horizontal bars
for(int i = x; i< x+width; i++)
{
gotoxy(i, y);
putch(' ');
gotoxy(i, y+height-1);
putch(' ');
}
// Draw the two vertical bars
for(i = y; i< y+height; i++)
{
gotoxy(x, i);
putch(' ');
gotoxy(x+width-1, i);
putch(' ');
}
// Reset the text background to the original color
textbackground(old_color);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Draw the pingpong racket
//
//////////////////////////////////////////////////////////////////////////////////////////////
void DrawBoard(char x, char y, char width, char color)
{
// get and save the current text background
int old_color = gettextbackground();
// Set text background color
textbackground(color);
// Draw the two horizontal bars
for(int i = x; i< x+width; i++)
{
gotoxy(i, y);
putch(' ');
}
// Reset the text background to the original color
textbackground(old_color);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Draw the a brick
//
//////////////////////////////////////////////////////////////////////////////////////////////
void DrawBrick(char x, char y, char width, char color)
{
// get and save the current text background
int old_color = gettextbackground();
// Set text background color
textbackground(color);
// Draw the two horizontal bars
for(int i = x; i< x+width; i++)
{
gotoxy(i, y);
putch(' ');
}
// Reset the text background to the original color
textbackground(old_color);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Generate and draw the bricks at the beginning of the game
//
//////////////////////////////////////////////////////////////////////////////////////////////
void InitBrick(void)
{
// get and save the current text background
int old_color = gettextcolor();
// Draw the two horizontal bars
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
Brick[i][j].x = 3+7*j;
Brick[i][j].y = 4+2*i;
Brick[i][j].color = 1+i;
Brick[i][j].score = 10*(ROW - i);
Brick[i][j].exist = 1;
textcolor(Brick[i][j].color);
DrawBrick(Brick[i][j].x, Brick[i][j].y, 6, Brick[i][j].color);
}
}
// Reset the text background to the original color
textcolor(old_color);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Display the game status
//
//////////////////////////////////////////////////////////////////////////////////////////////
void DisplayStatus(int score, int time, char life)
{
gotoxy(63,4);
printf("SCORE:%d",score);
gotoxy(63,6);
printf("TIME: %d s",time);
gotoxy(63,8);
printf("BALL: %d",life);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Get the key input
//
//////////////////////////////////////////////////////////////////////////////////////////////
char GetKey(void)
{
char a;
a = getch();
if( a == 0 ) // The first byte of the special function key is 0
{
a = getch(); // Get the second byte for the special function keys such as arrow keys
}
return a;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// Ending screen
//
//////////////////////////////////////////////////////////////////////////////////////////////
void EndingScreen(void)
{
for(int i=0; i<=(WINDOW_BOTTOM-WINDOW_TOP)/2; i++)
{
DrawBox( WINDOW_LEFT+i, WINDOW_TOP+i,
WINDOW_RIGHT-WINDOW_LEFT-2*i+1, WINDOW_BOTTOM-WINDOW_TOP-2*i+1,
(char) (1.0*rand()/RAND_MAX*14.0+1) );
delay(200);
}
gotoxy(WINDOW_LEFT+1,WINDOW_BOTTOM+1);
// Erase the user instruction at the bottom of the console
puts(" ");
textcolor(WHITE);
// Prompt the user for another run
gotoxy((WINDOW_RIGHT-WINDOW_LEFT)/2-3, (WINDOW_BOTTOM-WINDOW_TOP)/2+1);
printf("<GAME OVER>");
getch();
clrscr();
}
//////// END OF THE PROGRAM ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -