C语言小插曲——对图形库的初步探索(消除小球小游戏)

仅用于记录。

代码

#include <graphics.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#define ROW 9
#define COL 9
#define SIZE 25
#define EASY 10
#define SPEED 500
int main()
{
	//重新开始游戏
	again:
	int x,y,r = 0,g = 0,b = 0,count = 0,row,col,s = 0;
	int speed = SPEED;

	//建立一个用于存放小球的二维数组
	int arr[ROW][COL] = { 0 };
	//随机数
	srand((unsigned int)time(NULL));

	//建立窗口
	initgraph(ROW * (2 * SIZE) + 200, COL * (2 * SIZE));

	//设定背景颜色
	setbkcolor(WHITE);
	cleardevice();

	//初始化小球数组
	for (x = 0; x < ROW; x++)
	{
		for (y = 0; y < COL; y++)
		{
			arr[x][y] = 1;
		}
	}

	//建立字符数组,用于后面显示分数
	char ch[5];

	//显示游戏基本情况
	settextstyle(16, 0, "新宋体");
	settextcolor(RGB(248, 99, 82));
	outtextxy(ROW * (2 * SIZE) + 10, 50, "通过鼠标点击来消除小球");
	outtextxy(ROW * (2 * SIZE) + 10, 70, "当屏幕中小球达到10个时");
	outtextxy(ROW * (2 * SIZE) + 10, 90, "则游戏结束");
	outtextxy(ROW * (2 * SIZE) + 10, 110, "难度:普通");
	outtextxy(ROW * (2 * SIZE) + 10, 130, "按下空格键开始游戏");
	setlinecolor(BROWN);
	rectangle(1, 1, ROW * (2 * SIZE)-1, COL * (2 * SIZE)- 1);

	//键盘操作开始游戏
	char key = 0;
	key = _getch();
	while (1)
	{
		switch (key)
		{
		case 32:
            while (count < EASY)
			{
				for (row = 0; row < ROW; row++)
				{
					for (col = 0; col < COL; col++)
					{
						//使小球获得随机颜色
						r = rand() % 255 + 20;
						g = rand() % 255 + 20;
						b = rand() % 255 + 20;
						x = rand() % ROW;
						setfillcolor(RGB(r, g, b));
						
						//获取鼠标点击的信息
						if (MouseHit())
						{
							MOUSEMSG msg = GetMouseMsg();
							switch (msg.uMsg)
							{
							case WM_LBUTTONDOWN:
								//判断鼠标点击的坐标处是否有小球
								if (arr[msg.y / (SIZE*2)][msg.x / (SIZE*2)] == 2)
								{
									clearcircle(SIZE + ((msg.x / (SIZE*2)) * 2 * SIZE), SIZE + ((msg.y / (SIZE*2)) * 2 * SIZE), SIZE);
									arr[msg.y / (SIZE*2)][msg.x / (SIZE*2)] = 1;
									count--;
									s += 1;
									printf("sore = %d\n", s);
									sprintf_s(ch, "%d", s);
									outtextxy(ROW * (2 * SIZE) + 10, 150, "score:");
									outtextxy(ROW * (2 * SIZE) + 80, 150, ch);
								}

							}
						}

						//鼠标无操作,则立即开始随机生成小球
						//避免重复生成在同一个位置,加多判断条件
						else if (x == 5 && arr[row][col] != 2)
						{
							arr[row][col] = 2;
							solidcircle(SIZE + ((col) * 2 * SIZE), SIZE + ((row) * 2 * SIZE), SIZE);
							count++;
							Sleep(speed);
						}
						else
							continue;
					}
				}

				//使得生成小球的速度随着鼠标点击次数越多而越快
				if (speed >= 350)
					speed -= 10;
			}

			//显示游戏结束情况
			outtextxy(ROW * (2 * SIZE) + 10, 200, "游戏结束");
			outtextxy(ROW * (2 * SIZE) + 10, 220, "您最终的游戏分数为");
			settextstyle(18, 0, "宋体");
			settextcolor(RGB(51, 89, 24));
			sprintf_s(ch, "%d", s);
			outtextxy(ROW * (2 * SIZE) + 40, 250, ch);

			//重新开始游戏,跳到again
			outtextxy(ROW * (2 * SIZE) + 10, 300, "按下空格键重新开始");
			key = _getch();
			while (1)
			{
				switch (key)
				{
				case 32:
					goto again;
					break;
				default:
					break;
				}
			}
		    break;
		default:
			break;
		}
	}
	getchar();
	closegraph();
}

运行结果 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值