C语言实现-图形化编程小游戏-反弹小球消砖块(多注释)

编译器:Visual Studio 2019,末尾附有图形库插件安装包
注意:

  1. 需在编译器中安装图形库插件,可查看图形库函数说明文档
  2. 要把项目属性------高级-------字符集 改为 使用多字节字符集,否则编译器会报错

代码如下:

# 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,操作更方便哦

如果有问题可以留言交流,我一定及时回复;
如果采纳麻烦您给俺点个赞

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值