编译器:Visual Studio 2019,末尾附有图形库插件安装包
注意:
- 需在编译器中安装图形库插件,可查看图形库函数说明文档
- 要把项目属性------高级-------字符集 改为 使用多字节字符集,否则编译器会报错
代码如下:
# include <stdio.h>
# include <graphics.h>
# include <conio.h>
# include <time.h>
int map[3][5];//砖块的地图
int ballX, ballY;//球圆心的坐标
int ballRadius;//球半径
int boardX, boardY;//板左上角的坐标
int boardWidth, boardHigh;//板的宽和高
int brickWidth, brickHigh;//砖块的宽和高
int width, high;//画面的宽和高
int moveX, moveY;//球的移动速度
HWND hwnd;
//函数声明部分
void startup();
void show();
void updateWithoutInput();
void updateWithInput();
void drawBrickMap();
void gameOver();
//函数定义部分
//画砖块
void drawBrickMap()
{
int i, j;
int brickX, brickY;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 5; j++)
{
brickX = j * brickWidth;
brickY = i * brickHigh;
switch (map[i][j])
{
case 0:
setfillcolor(GREEN);
solidrectangle(brickX, brickY, brickX + brickWidth, brickY + brickHigh);
break;
case 1:
setfillcolor(LIGHTRED);
solidrectangle(brickX, brickY, brickX + brickWidth, brickY + brickHigh);
break;
case 2:
setfillcolor(BROWN);
solidrectangle(brickX, brickY, brickX + brickWidth, brickY + brickHigh);
break;
case 3:
setfillcolor(BLACK);
solidrectangle(brickX, brickY, brickX + brickWidth, brickY + brickHigh);
break;
case 4:
setfillcolor(YELLOW);
solidrectangle(brickX, brickY, brickX + brickWidth, brickY + brickHigh);
break;
case -1:
break;
}
}
}
}
//判断胜负,结束游戏
void gameOver()
{
//判断负
if (ballY - ballRadius > high)
{
moveY = -moveY;
//settextstyle(20,0,"楷体");
//outtextxy(width / 2, high / 2, "YOU LOSE!");//可能由于本程序用了批图像处理,无法显示文本打印内容
MessageBox(hwnd, "You Lose!", "GameOver!", MB_OK);
EndBatchDraw();
closegraph();
exit(-1);//结束游戏
}
//判断胜
int flag = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (map[i][j] >= 0)
{
flag = 0;
goto NEXT;
}
}
}
NEXT:
if (flag)
{
MessageBox(hwnd, "You Win!", "GameOver!", MB_OK);
EndBatchDraw();
closegraph();
exit(-1);//结束游戏
}
}
//初始化数据
void startup()
{
srand((unsigned)time(NULL));
//画面尺寸
width = 100 * 5;//500
high = 700;
//砖块的尺寸
brickWidth = 100;
brickHigh = 40;
//木板尺寸
boardWidth = 100;
boardHigh = 20;
boardX = width / 2 - boardWidth / 2;
boardY = high - boardHigh;
//球的位置
ballRadius = 10;
ballX = rand() % 47 * 10 + ballRadius;//20~480
ballY = brickHigh * 3 + ballRadius;
//球的移动速度
moveX = 10;
moveY = 10;
//使代表砖块的颜色的数字随机,即有5种颜色
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
map[i][j] = rand() % 5;
}
}
//初始化画面尺寸
hwnd = initgraph(width, high);
BeginBatchDraw();
//设置画面背景颜色
setbkcolor(WHITE);
}
//展示画面
void show()
{
//刷新画面
cleardevice();
//画砖块图
drawBrickMap();
//画木板
setfillcolor(BLACK);
solidrectangle(boardX, boardY, boardX + boardWidth, boardY + boardY + boardHigh);
//画圆
setfillcolor(RED);
solidcircle(ballX, ballY, ballRadius);
FlushBatchDraw();
Sleep(40);
}
//与用户输入无关的更新
void updateWithoutInput()
{
//球的移动
ballX += moveX;
ballY += moveY;
//球撞上方的砖块反弹
if (ballY - ballRadius == 3 * brickHigh ||
ballY - ballRadius == 2 * brickHigh ||
ballY - ballRadius == 1 * brickHigh)
{
//搜索小球上方砖块的下标
int brick_j = (ballX - ballRadius) / brickWidth;//列下标
int brick_i = (ballY - ballRadius) / brickHigh - 1;//行下标
//判断球的上方是否存在砖块
if (map[brick_i][brick_j] != -1)
{
moveY = -moveY;
map[brick_i][brick_j] = -1;//让砖块消失
}
}
//球碰旁边的砖块反弹
if (ballY - ballRadius < 3 * brickHigh)
{
//撞左边的砖块
if (ballX - ballRadius == brickWidth * 1 ||
ballX - ballRadius == brickWidth * 2 ||
ballX - ballRadius == brickWidth * 3 ||
ballX - ballRadius == brickWidth * 4 )
{
//搜索小球左边砖块的下标
int brick_j = (ballX - ballRadius) / brickWidth - 1;//列下标
int brick_i = (ballY - ballRadius) / brickHigh;//行下标
if (map[brick_i][brick_j] != -1)
{
moveX = -moveX;
map[brick_i][brick_j] = -1;//让砖块消失
}
}
//撞右边的砖块
if (ballX + ballRadius == brickWidth * 1 ||
ballX + ballRadius == brickWidth * 2 ||
ballX + ballRadius == brickWidth * 3 ||
ballX + ballRadius == brickWidth * 4 )
{
//搜索小球右边砖块的下标
int brick_j = (ballX - ballRadius) / brickWidth + 1;//列下标
int brick_i = (ballY - ballRadius) / brickHigh;//行下标
if (map[brick_i][brick_j] != -1)
{
moveX = -moveX;
map[brick_i][brick_j] = -1;//让砖块消失
}
}
}
//球碰到上墙壁反弹
if (ballY - ballRadius <= 0)
{
moveY = -moveY;
}
//球碰到左右墙壁反弹
if (ballX - ballRadius <= 0 || ballX + ballRadius >= width)
{
moveX = -moveX;
}
//球碰到木板反弹
if (ballY + ballRadius == boardY)
{
if (ballX >= boardX && ballX <= boardX + boardWidth)
{
moveY = -moveY;
}
}
//判断胜负
gameOver();
}
//与用户输入有关的更新
void updateWithInput()
{
//按键处理
if (_kbhit())
{
char hit = _getch();
switch (hit)
{
case 'a'://左移
case 'A':
case 75:
boardX -= 10;
if (boardX < 0)
{
boardX = 0;
}
break;
case 'd'://右移
case 'D':
case 77:
boardX += 10;
if (boardX + boardWidth > width)
{
boardX = width - boardWidth;
}
break;
case ' '://暂停
case '\r':
case 27:
while (_getch() != ' ' && _getch() != '\r' && _getch() != 27);
break;
}
}
}
//主函数
int main()
{
startup();
while (1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
实现效果:
链接:https://pan.baidu.com/s/1ouIRd5weEc6KS08GJW1Hng 提取码:9kmg
复制这段内容后打开百度网盘手机App,操作更方便哦
如果有问题可以留言交流,我一定及时回复;
如果采纳麻烦您给俺点个赞