#include<iostream>
#include<graphics.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#define WIN_WIDHT 1100 //窗体宽度
#define WIN_HEIGHT 800 //窗体高度
#define WALL_WIDTH 90 //墙宽度
#define WALL_HEIGHT 25 //墙高度
#define BOARD_WIDTH 200 //挡板宽度
#define GAME_WIDTH 800 //游戏主界面宽度
#define GAME_HEIGHT (WIN_HEIGHT) //游戏主界面高度
using namespace std;//木板结构体
typedef struct Board
{
int x;
int y;
int width;
int height;
int speed;
COLORREF color;
}Board;//球的结构体
typedef struct Ball
{
int x;
int y;
int r;
int dx; //移动x坐标值
int dy; //移动y坐标值
COLORREF color;
}Ball;//墙的结构体
typedef struct Wall
{
int x;
int y;
int width;
int height;
COLORREF color;
}Wall;int map[10][10] = { 0 }; //墙集合
int row = 6, col = 6; //行数和列数
bool death = false; //是否死亡标记
int all_board_num = row * col;//砖块的总数
int kit_board_num = 0; //销毁砖块的总数
int score = 0;//分数
int game_num = 1;//关卡
//初始化
void init();
//创建木板
Board* createBoard();//创建球
Ball* createBall(Board* board);//创建墙
void createWall(int row, int col);
//画木板
void drawBoard(Board* board);//画球
void drawBall(Ball* ball);//画墙
void drawWall();
//键盘按键按下
void keyDown(Board* board, Ball* ball);//小球移动
void moveBall(Ball* ball, Board* board);
//小球移动定时器
bool timer_ball(int num);//小球碰到木板
bool kitboard(Board* board, Ball* ball);
//小球碰到砖块
bool kitwall(Ball* ball);
//画分割线
void drawcutline();
//显示信息
void outinfo(int x, int y, string info);
int main()
{
//1初始化
init();
Board* pboard; //墙指针
Ball* pball;//球指针
while (true)//这层循环控制关卡
{pboard = createBoard();
pball = createBall(pboard);
createWall(row, col);static char temp_game_num[10]{}; //临时变量,用于拼接数字和字母,方便输出到窗体
static char temp_score[10]{}; //临时变量,用于拼接数字和字母,方便输出到窗体
static char temp_all_board_num[10]{}; //临时变量,用于拼接数字和字母,方便输出到窗体BeginBatchDraw();//批量赋值标记
while (1) //这个循环是更新当前关卡信息
{
if (death)//判断小球是否死亡
break;
cleardevice(); //清屏
drawWall(); //画墙
drawcutline(); //画分割线outinfo(GAME_WIDTH + 20, 30, "游戏关卡: ");
outtextxy(GAME_WIDTH + 20, 50, "积分: ");
itoa(game_num, temp_game_num, 10);
itoa(score, temp_score, 10);
outtextxy(GAME_WIDTH + 100, 30, temp_game_num);
outtextxy(GAME_WIDTH + 100, 50, temp_score);drawBoard(pboard);
drawBall(pball);
if (timer_ball(10)) //用于球循环移动条件
moveBall(pball, pboard);
keyDown(pboard, pball);//键盘按键FlushBatchDraw();
//判断是否通关d
if (kit_board_num == all_board_num)
{
//
row = rand() % 6 + 1;
col = rand() % 6 + 1;
all_board_num = row * col;
kit_board_num = 0;game_num++;
Sleep(100);
break;
}}
EndBatchDraw();//跳出循环
if (death)//
break;
}outtextxy(GAME_WIDTH / 2, GAME_HEIGHT / 2, "Game Over");
outtextxy(GAME_WIDTH / 2, GAME_HEIGHT / 2 + 50, "Press any key to exit the game");
system("pause");
return 0;
}//初始化
void init()
{
initgraph(WIN_WIDHT, WIN_HEIGHT, true);srand((unsigned)time(NULL));
}
//创木板
Board* createBoard()
{
Board* pboard = (Board*)malloc(sizeof(Board));
pboard->width = BOARD_WIDTH;
pboard->height = 50;
pboard->speed = 2;
pboard->color = RED;
pboard->x = GAME_WIDTH / 2 - pboard->width / 2;
pboard->y = GAME_HEIGHT - 100;
return pboard;
}//创建球
Ball* createBall(Board* board)
{Ball* pball = (Ball*)malloc(sizeof(Ball));
pball->color = BLUE;
pball->r = 10;
//pball->x = board->x + board->width / 2;
//pball->y = board->y - pball->r * 2;pball->x = board->x + board->width + 30;
pball->y = board->y - pball->r * 2;
pball->dx = -3;
pball->dy = -3;return pball;
}//创建墙
void createWall(int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
map[i][j] = rand() % 3 + 1;
}
}
}
//画木板
void drawBoard(Board* board)
{
auto coler = getfillcolor();
setfillcolor(board->color);
fillrectangle(board->x, board->y, board->x + board->width, board->y + board->height);
setfillcolor(coler);
}//画球
void drawBall(Ball* ball)
{
auto coler = getfillcolor();
setfillcolor(ball->color);
fillcircle(ball->x, ball->y, ball->r);
setfillcolor(coler);
}//画墙
void drawWall()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{int x = j * 100;
int y = i * 30;
switch (map[i][j])
{
case 0:
break;
case 1:
setfillcolor(RED);
fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
break;
case 2:
setfillcolor(BLUE);
fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
break;
case 3:
setfillcolor(YELLOW);
fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
break;
default:
break;
}
}
}
}//键盘按键按下
void keyDown(Board* board, Ball* ball)
{if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && board->x > 0)
{
board->x -= board->speed;}
else if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && board->x + board->width < GAME_WIDTH - 2)
{
board->x += board->speed;}
else
{}
}//小球移动
void moveBall(Ball* ball, Board* board)
{
if (ball->x - ball->r <= 0 || ball->x + ball->r >= GAME_WIDTH)
{
ball->dx = -ball->dx;
}if (ball->y + ball->r > board->y && (ball->x < board->x || ball->x > board->x + BOARD_WIDTH))
{
death = true;
return;
}
else
{
if (ball->y - ball->r <= 0 || ball->y - ball->r == board->y || kitboard(board, ball) || kitwall(ball))
{
ball->dy = -ball->dy;
}
}ball->x += ball->dx;
ball->y += ball->dy;
}//小球移动定时器
bool timer_ball(int num)
{
static time_t start[1];
time_t end = clock();if (end - start[0] >= num)
{
start[0] = end;
return true;
}
return false;}
//小球碰到木板
bool kitboard(Board* board, Ball* ball)
{
if (ball->y + ball->r == board->y && (ball->x > board->x) && ball->x < board->x + BOARD_WIDTH)
{
ball->y -= ball->dy;
return true;
}
return false;
}
//小球碰到砖块
bool kitwall(Ball* ball)
{
int ballj = ball->x / WALL_WIDTH;
int balli = ball->y / WALL_HEIGHT;if (ballj < col && balli < row && map[ballj][balli] != 0)
{
map[ballj][balli] = 0;
++kit_board_num;
score += 50;
return true;
}return false;
}//画分割线
void drawcutline()
{
//画游戏区域和信息显示区域分割线
setfillcolor(BLUE);
line(GAME_WIDTH + 10, 0, GAME_WIDTH + 10, WIN_HEIGHT);
}//显示信息
void outinfo(int x, int y, string info)
{
cout << info << endl;
settextcolor(WHITE);
outtextxy(x, y, info.c_str());
}
c 打砖块,可以无限关卡 ,显示关卡信息和得分
最新推荐文章于 2022-12-11 14:40:06 发布