C++小游戏分享1 - 2048

这是一个使用C++和EasyX图形库实现的2048小游戏。代码中定义了游戏界面的尺寸、格子布局、颜色样式以及数字和颜色的对应关系。游戏初始化时会随机生成数字,玩家可以通过键盘控制上下左右移动格子,当相同数字的格子相遇时会合并。程序还包括了判断游戏状态和生成新数字的逻辑。
摘要由CSDN通过智能技术生成

小游戏基于EasyX库

界面数据

//每行四个格子 100 每个格子间隔15
//100 + 3*15相邻格子相邻 + 2*15两边边框距窗口 = 475
#define MAX_GRID 4		//每行格子
#define GRID_WIDTH 100  //格子宽度
#define INTERVAL 15     //间隔

//main中
//initgraph(475, 475, 1);

 

 颜色数据 

enum Color  //枚举
{
	zero = RGB(205, 193, 180),//0
	twoTo1 = RGB(238, 228, 218),//2
	twoTo2 = RGB(237, 224, 200),//4
	twoTo3 = RGB(242, 177, 121),//8
	twoTo4 = RGB(245, 149, 99),//16
	twoTo5 = RGB(246, 124, 95),//32
	twoTo6 = RGB(246, 94, 59),//64
	twoTo7 = RGB(242, 177, 121),//128
	twoTo8 = RGB(237, 204, 97),//256
	twoTo9 = RGB(255, 0, 128),//512
	twoTo10 = RGB(145, 0, 72),//1024
	twoTo11 = RGB(242, 17, 158),//2048
	back = RGB(187, 173, 160),//背景
};

 颜色数组及数字数组 

Color arr[13] = {
	zero,twoTo1,twoTo2,twoTo3,twoTo4,twoTo5,twoTo6,
	twoTo7,twoTo8,twoTo9,twoTo10,twoTo11,back
};
int num[12] = { 0,2,4,8,16,32,64,128,256,512,1024,2048 };

存储数字与坐标的数组

int map[4][4] = {};
POINT pos[4][4] = {};

源码分享

#include<iostream>
#include<easyx.h>
#include<conio.h>
using namespace std;

//每行四个格子 100 间隔15
#define MAX_GRID 4		//每行格子
#define GRID_WIDTH 100  //格子宽度
#define INTERVAL 15     //间隔

enum Color  //枚举
{
	zero = RGB(205, 193, 180),//0
	twoTo1 = RGB(238, 228, 218),//2
	twoTo2 = RGB(237, 224, 200),//4
	twoTo3 = RGB(242, 177, 121),//8
	twoTo4 = RGB(245, 149, 99),//16
	twoTo5 = RGB(246, 124, 95),//32
	twoTo6 = RGB(246, 94, 59),//64
	twoTo7 = RGB(242, 177, 121),//128
	twoTo8 = RGB(237, 204, 97),//256
	twoTo9 = RGB(255, 0, 128),//512
	twoTo10 = RGB(145, 0, 72),//1024
	twoTo11 = RGB(242, 17, 158),//2048
	back = RGB(187, 173, 160),//背景
};
Color arr[13] = {
	zero,twoTo1,twoTo2,twoTo3,twoTo4,twoTo5,twoTo6,
	twoTo7,twoTo8,twoTo9,twoTo10,twoTo11,back
};
int num[12] = { 0,2,4,8,16,32,64,128,256,512,1024,2048 };
int map[4][4] = {};
POINT pos[4][4] = {};
bool flag = 0;

//随机开局生成
int twoORfour()
{
	if (rand() % 10 == 1) // 1/10概率
	{
		return 4;
	}
	else
		return 2;
}
//放到数组里面
void CreateNumber()
{
	while (1)
	{
		int x = rand() % MAX_GRID;
		int y = rand() % 4;
		if (map[x][y] == 0)
		{
			map[x][y] = twoORfour();
			break;
		}
	}
}

//初始化坐标
void GameInit()
{
	srand(GetTickCount());
	//计算格子坐标
	for (int i = 0; i < MAX_GRID; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			pos[i][j].x = j * GRID_WIDTH + (j + 1) * INTERVAL;
			pos[i][j].y = i * GRID_WIDTH + (i + 1) * INTERVAL;
		}
	}
	//初始化--产生两个
	CreateNumber();
	CreateNumber();
}

void GameDraw()
{
	setbkcolor(back);
	cleardevice();//与setbkcolor一起用
	for (int i = 0; i < MAX_GRID; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			for (int q = 0; q < 12; q++)
			{
				if (map[i][j] == num[q])
				{
					settextcolor(RGB(119, 110, 101));
					setfillcolor(arr[q]);
					solidrectangle(pos[i][j].x, pos[i][j].y, pos[i][j].x + GRID_WIDTH, pos[i][j].y + GRID_WIDTH);
					if (map[i][j] != 0)
					{
						char number[5] = " ";
						settextstyle(50, 0, "楷体");
						setbkmode(TRANSPARENT);//背景模式

						sprintf_s(number, "%d", num[q]);
						//用格子的一半减去字符串的一半
						int tempx = GRID_WIDTH / 2 - textwidth(number) / 2;
						int tempy = GRID_WIDTH / 2 - textheight(number) / 2;

						outtextxy(pos[i][j].x + tempx, pos[i][j].y + tempy, number);//多字节
					}
				}
			}
		}
	}
}

void GameJudge()
{
	if (flag)
	{
		CreateNumber();
		flag = 0;
	}
}

//上移
void moveup()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = 0;
		for (int begin = 1; begin < MAX_GRID; begin++)
		{
			if (map[begin][i] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[begin][i];
					map[begin][i] = 0;
				}
				else if (map[temp][i] == map[begin][i])
				{
					map[temp][i] += map[begin][i];//*=2
					map[begin][i] = 0;
				}
				else {
					map[temp + 1][i] = map[begin][i];
					if (temp + 1 != begin)
					{
						map[begin][i] = 0;
					}
				}
				temp++;
				flag = 1;
			}
		}
	}
	cout << "up" << endl;
}
//下移
void movedown()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = MAX_GRID - 1;
		for (int begin = MAX_GRID - 2; begin >= 0; begin--)
		{
			if (map[begin][i] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[begin][i];
					map[begin][i] = 0;
				}
				else if (map[temp][i] == map[begin][i])
				{
					map[temp][i] += map[begin][i];//*=2
					map[begin][i] = 0;
				}
				else {
					map[temp - 1][i] = map[begin][i];
					if (temp - 1 != begin)
					{
						map[begin][i] = 0;
					}
				}
				temp--;
				flag = 1;
			}
		}
	}
	cout << "down" << endl;
}
//左移
void moveleft()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = 0;
		for (int begin = 1; begin < MAX_GRID; begin++)
		{
			if (map[i][begin] != 0)
			{
				if (map[i][temp] == 0)
				{
					map[i][temp] = map[i][begin];
					map[i][begin] = 0;
				}
				else if (map[i][temp] == map[i][begin])
				{
					map[i][temp] += map[i][begin];//*=2
					map[i][begin] = 0;
				}
				else {
					map[i][temp + 1] = map[i][begin];
					if (temp + 1 != begin)
					{
						map[i][begin] = 0;
					}
				}
				temp++;
				flag = 1;
			}
		}
	}
	cout << "left" << endl;
}
//右移
void moveright()
{
	for (int i = 0; i < MAX_GRID; i++)
	{
		int temp = MAX_GRID - 1;
		for (int begin = MAX_GRID - 2; begin >= 0; begin--)
		{
			if (map[i][begin] != 0)
			{
				if (map[i][temp] == 0)
				{
					map[i][temp] = map[i][begin];
					map[i][begin] = 0;
				}
				else if (map[i][temp] == map[i][begin])
				{
					map[i][temp] += map[i][begin];//*=2
					map[i][begin] = 0;
				}
				else {
					map[i][temp - 1] = map[i][begin];
					if (temp - 1 != begin)
					{
						map[i][begin] = 0;
					}
				}
				temp--;
				flag = 1;
			}
		}
	}
	cout << "right"<<endl;
}
//键盘控制移动
void GameControl()
{
	char key = _getch();
	switch (key)
	{
	case 'w':
	case 'W':
	case 72:
		moveup();
		break;
	case 'S':
	case 's':
	case 80:
		movedown();
		break;
	case 'a':
	case 'A':
	case 75:
		moveleft();
		break;
	case 'd':
	case 'D':
	case 77:
		moveright();
		break;
	}
}

int main()
{
	initgraph(475, 475, 1);
	GameInit();
	while (1)
	{
		GameDraw();
		GameControl();
		GameJudge();
	}
	getchar();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值