吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2016|回复: 3
收起左侧

[C&C++ 转载] 分享一款点灯游戏源码(游戏玩法可以网上找)

[复制链接]
涛行 发表于 2020-9-26 20:12
该程序需要编译器安装对应的EasyX才能包含graphics.h的头文件


[C] 纯文本查看 复制代码
#include<graphics.h>
#include<conio.h>

#define M 14
#define G 30

struct Grid
{
	int left;
	int right;
	int up;
	int down;
	int arr[M][M];
	int num;
}grid;

void Welcome();
void PaintGrid(int mx, int my, int num, int color);
void OnLButtonDown(MOUSEMSG m, int num);
void OnRButtonDown(int num);
int JudheFull(int num, int arr[M][M]);
void NextLevel(int num);
int Dispatch();

int main()
{
	int end = 0;
	grid.num = 4;
	Welcome();
	PaintGrid(320, 240, grid.num, RGB(0, 255, 0));
	while (end != 1)
	{
		end = Dispatch();
	}
	closegraph();
	return 0;
}
//欢迎界面
void Welcome()
{
	initgraph(640, 480);
	cleardevice();//刷新
	settextcolor(RGB(0, 255, 0));
	settextstyle(64, 0, "方正情不知所起");
	outtextxy(200, 50, "点灯游戏");

	settextcolor(WHITE);
	settextstyle(20, 0, "微软雅黑");
	outtextxy(100, 200, "每点击一个格子,上下左右的格子也会做出与现状相反的动作");
	outtextxy(100, 240, "总共11关,左键填色,右键重来");

	int c = 255;
	while (!_kbhit())     //_kbhit()->键盘录入  !是取反
	{
		settextcolor(RGB(0, c, 0));
		outtextxy(280, 400, "请按任意键继续");
		c -= 8;
		if (c < 0)
			c = 255;
		Sleep(20);
	}
	_getch();
	cleardevice();
}

/*游戏主界面*/

void PaintGrid(int mx, int my, int num, int color)
{
	int x, y, nx, ny;//游戏左右上下边界
	grid.left = mx - num * G / 2;
	grid.right = mx + num * G / 2;
	grid.up = my - num * G / 2;
	grid.down = my + num * G / 2;

	//绘制格子的方法
	setlinecolor(color);
	for (x = grid.left; x <= grid.right; x += G)
		line(x, grid.up, x, grid.down);
	for (y = grid.up; y <= grid.down; y += G)
		line(grid.left, y, grid.right, y);

	//外边框
	for (x = 20; x > 10; x--)
	{
		rectangle(grid.left - x, grid.up - x, grid.right + x, grid.down + x);
		Sleep(10);
	}
	//清空单元格
	for (x = 0; x < num; x++)
		for (y = 0; y < num; y++)
			grid.arr[x][y] = -1;
	for (nx = 0; nx < num; nx++)
		for (ny = 0; ny < num; ny++)
		{
			setfillcolor(BLACK);
			x = nx * G + grid.left;
			y = ny * G + grid.up;
			solidrectangle(x + 1, y + 1, x + G - 1, y + G - 1);
		}

}

//鼠标左键点击
void OnLButtonDown(MOUSEMSG m, int num)
{
	int nx, ny, x, y;
	if (m.x > grid.left && m.x<grid.right && m.y>grid.up && m.y < grid.down)
	{
		//坐标转换成下标
		nx = (int)(m.x - grid.left) / G;
		ny = (int)(m.y - grid.up) / G;
		grid.arr[nx][ny] = -grid.arr[nx][ny];
		if (nx >= 0 && nx < num - 1)
			grid.arr[nx + 1][ny] = -grid.arr[nx + 1][ny];
		if (nx > 0 && nx < num)
			grid.arr[nx - 1][ny] = -grid.arr[nx - 1][ny];
		if (ny >= 0 && ny < num - 1)
			grid.arr[nx][ny + 1] = -grid.arr[nx][ny + 1];
		if (ny > 0 && ny < num)
			grid.arr[nx][ny - 1] = -grid.arr[nx][ny - 1];

		for (nx = 0; nx < num; nx++)
			for (ny = 0; ny < num; ny++)
			{
				if (grid.arr[nx][ny] == 1)
					setfillcolor(GREEN);
				else
					setfillcolor(BLACK);
				x = nx * G + grid.left;
				y = ny * G + grid.up;
				solidrectangle(x + 1, y + 1, x + G - 1, y + G - 1);
			}
	}
}
//右键清空单元格
void OnRButtonDown(int num)
{
	int x, y, nx, ny;
	for (x = 0; x < num; x++)
		for (y = 0; y < num; y++)
			grid.arr[x][y] = -1;
	for (nx = 0; nx < num; nx++)
		for (ny = 0; ny < num; ny++)
		{
			setfillcolor(BLACK);
			x = nx * G + grid.left;
			y = ny * G + grid.up;
			solidrectangle(x + 1, y + 1, x + G - 1, y + G - 1);
		}
}

int JudheFull(int num, int arr[M][M])
{
	int c = -1;//格子状态
	int nx = 0, ny = 0;
	while (nx < num && ny < num)
	{
		for (nx = 0; nx < num; nx++)
			for (ny = 0; ny < num; ny++)
				if (arr[nx][ny] == 1)
					continue;
				else
					return c;
	}
	c = 1;
	return c;
}


void NextLevel(int num)
{
	BeginBatchDraw();
	for (int y = 0; y <= 480; y += 5)
	{
		setlinecolor(RGB(255, 0, 0));
		line(0, y, 640, y);
		line(0, 480 - y, 640, 480 - y);
		settextstyle(20, 0, "黑体");
		outtextxy(300, y - 8, "下一关");
		FlushBatchDraw();
		Sleep(16);
		setfillcolor(BLACK);
		solidrectangle(0, y, 640, y - 10);
		solidrectangle(0, 480 - y, 640, 480 - y + 1);
	}
	EndBatchDraw();
	PaintGrid(320, 240, num, RGB(0, 255, 0));
}

int Dispatch()
{
	MOUSEMSG m;
	while (1)
	{
		m = GetMouseMsg();
		switch (m.uMsg)
		{
		case WM_LBUTTONDOWN:
			OnLButtonDown(m, grid.num);
			if (JudheFull(grid.num, grid.arr) == 1)
			{
				grid.num++;
				if (grid.num > M)
				{
					return 1;
					break;
				}
				else
					NextLevel(grid.num);
			}
			break;
		case WM_RBUTTONDOWN:
			OnRButtonDown(grid.num);
			break;
		default:
			break;
		}

	}
}

免费评分

参与人数 3吾爱币 +1 热心值 +3 收起 理由
九道1 + 1 用心讨论,共获提升!
兰笙c + 1 热心回复!
yellowclock + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

寒冰流火 发表于 2020-9-28 07:53
谢谢楼主分享可以的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 23:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表