今日小游戏:消除棋子

标题

怎么编写一个“消除棋子”的游戏?

#include <graphics.h>      // 引用图形库头文件
#include <conio.h>
#include <time.h>
int main1()
{
	initgraph(640, 480);   // 创建绘图窗口,大小为 640x480 像素
	MOUSEMSG m;
	int r;
	while (1)
	{
		m = GetMouseMsg();

		switch (m.uMsg)
		{
			//case WM_LBUTTONDOWN:
			//	setcolor(RGB(rand()%256, rand() % 256, rand() % 256));
			//	circle(m.x, m.y, rand()%91+10);
			//	break;
			//case WM_RBUTTONDOWN:
			//	setcolor(RGB(rand()%256, rand() % 256, rand() % 256));
			//	r = rand() % 91 + 10;
			//	rectangle(m.x - r, m.y-r, m.x+r, m.y+r);
			//	break;		

		case WM_MOUSEMOVE:
			setcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
			if (m.mkLButton)
			{
				circle(m.x, m.y, rand() % 91 + 10);
			}
			else if (m.mkRButton)
			{
				r = rand() % 91 + 10;
				rectangle(m.x - r, m.y - r, m.x + r, m.y + r);
			}

			break;
		}

	}


	_getch();              // 按任意键继续
	closegraph();          // 关闭绘图窗口
}

const int BoardW = 5;
const int BoardH = 5;
const int GridSz = 90;
const int Edge = 30;

void drawGrid()
{
	setbkcolor(RGB(214,167,140));
	cleardevice();

	setcolor(RGB(0, 0, 0));
	for (int i = 0; i <= BoardH; ++i)
	{
		line(Edge, Edge + i * GridSz,
			Edge + BoardW * GridSz, Edge + i * GridSz);
	}

	for (int i = 0; i <= BoardH; ++i)
	{
		line(Edge + i * GridSz, Edge,
			Edge + i * GridSz, Edge + BoardH * GridSz);
	}
}

void drawLayout(int layout[BoardH][BoardW])
{
	setcolor(RGB(32,27,27));
	setfillcolor(RGB(32, 27,27));

	int r = GridSz * 4 / 10;
	int x, y;

	for (int i = 0; i < BoardH; ++i)
	{
		for (int j = 0; j < BoardW; ++j)
		{
			if (1 == layout[i][j])
			{
				x = Edge + j*GridSz + GridSz / 2;
				y = Edge + i*GridSz + GridSz / 2;
				fillcircle(x, y, r);
			}
		}
	}
}

int pos2Index(int x, int y, int* pi, int* pj)
{
	if (x <= Edge || x >= Edge + BoardW*GridSz ||
		y <= Edge || y >= Edge + BoardH*GridSz)
		return 0;

	*pi = (y - Edge) / GridSz;
	*pj = (x - Edge) / GridSz;
	return 1;
}

void trunLayout(int layout[BoardH][BoardW], int i, int j)
{
	layout[i][j] = 1 - layout[i][j];
	if (i - 1 >= 0)
	{
		layout[i - 1][j] = 1 - layout[i - 1][j];
	}
	if (i + 1 < BoardH)
	{
		layout[i + 1][j] = 1 - layout[i + 1][j];
	}
	if (j - 1 >= 0)
	{
		layout[i][j - 1] = 1 - layout[i][j - 1];
	}
	if (j + 1 < BoardW)
	{
		layout[i][j + 1] = 1 - layout[i][j + 1];
	}
}

void updateLayout(int layout[BoardH][BoardW], int x, int y)
{
	int i, j;

	if (pos2Index(x, y, &i, &j))
	{
		trunLayout(layout, i, j);
	}
}

int getPiecesCount(int layout[BoardH][BoardW])
{
	int sum = 0;
	for (int i = 0; i < BoardH; ++i)
	{
		for (int j = 0; j < BoardW; ++j)
		{
			sum += layout[i][j];
		}
	}

	return sum;
}

void initLayout(int layout[BoardH][BoardW])
{
	for (int i = 0; i < 10; ++i)
	{
		trunLayout(layout, rand() % BoardH, rand() % BoardW);
	}

	//while (1)
	//{
	//	trunLayout(layout, rand() % BoardH, rand() % BoardW);
	//	if (getPiecesCount(layout) < 7 && getPiecesCount(layout) > 0)
	//		break;
	//}
}

int main()
{
	//int layout[3][BoardH][BoardW] =
	//{
	//	{
	//		1,1,0,1,1,
	//		1,0,0,0,1,
	//		0,0,0,0,0,
	//		1,0,0,0,1,
	//		1,1,0,1,1},
	//	{
	//		0,0,0,0,0,
	//		0,0,1,0,0,
	//		0,1,1,1,0,
	//		0,0,1,0,0,
	//		0,0,0,0,0
	//	},
	//	{
	//		1,0,0,0,1,
	//		0,0,0,0,0,
	//		0,0,0,0,0,
	//		0,0,0,0,0,
	//		1,0,0,0,1
	//	}
	//};

	srand(time(NULL));
	int layout[BoardH][BoardW] = { 0 };

	HWND hwnd;
	hwnd = initgraph(BoardW * GridSz + 2 * Edge, BoardH * GridSz + 2 * Edge);

	//for(int i=0; i<3; ++i)
	while (1)
	{
		initLayout(layout);

		BeginBatchDraw();
		drawGrid();
		drawLayout(layout);
		EndBatchDraw();

		MOUSEMSG m;

		while (getPiecesCount(layout))
		{
			m = GetMouseMsg();

			switch (m.uMsg)
			{
			case WM_LBUTTONDOWN:
				updateLayout(layout, m.x, m.y);
				break;
			}
			BeginBatchDraw();
			drawGrid();
			drawLayout(layout);
			EndBatchDraw();
		}

		MessageBox(hwnd, _T("恭喜过关!"), _T("提示"), MB_OK);
	}

	_getch();              // 按任意键继续
	closegraph();          // 关闭绘图窗口
}


#if 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	srand(time(NULL));//1970.1.1 00:00:00


	for (int i = 0; i < 10; ++i)
	{
		printf("%d,", rand());
	}

	return 0;
}
/*
伪随机
*/

#endif
  •                                                                **作者:邹氏情歌cium**
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值