【C++ 程序】 2048游戏(EasyX 图形界面)

这是用 EasyX 制作图形界面游戏的第三次试验。效果不错。

程序

//This is a simple 2048 game.

#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>
#include <random>
#include <ctime>
#include <conio.h>
#include <graphics.h>
using namespace std;

unsigned long score = 0;
int stop = 0;
string s0 = "    ";
string s2 = " 2  ";
string s4 = " 4  ";
string s8 = " 8  ";
string s16 = " 16 ";
string s32 = " 32 ";
string s64 = " 64 ";
string s128 = "128 ";
string s256 = "256 ";
string s512 = "512 ";
string s1024 = "1024";
string s2048 = "2048";
string s4096 = "4096";
string s8192 = "8192";

IMAGE i0, i2, i4, i8, i16, i32, i64, i128, i256, i512, i1024, i2048, i4096, i8192, imageOver;

vector<vector<string>> p = {
    
     {
    
    s0, s0, s0, s0}, {
    
    s0, s2, s0, s0}, {
    
    s0, s0, s0, s0}, {
    
    s0, s0, s0, s0} };

int equal_int(string str)
{
    
    
	if (str == s2) return 4;
	else if (str == s4) return 8;
	else if (str == s8) return 16;
	else if (str == s16) return 32;
	else if (str == s32) return 64;
	else if (str == s64) return 128;
	else if (str == s128) return 256;
	else if (str == s256) return 512;
	else if (str == s512) return 1024;
	else if (str == s1024) return 2048;
	else if (str == s2048) return 4096;
	else if (str == s4096) return 8192;
}

string equal_str(string str)
{
    
    
	int num = equal_int(str);
	switch (num)
	{
    
    
	case 4: return s4;
	case 8: return s8;
	case 16: return s16;
	case 32: return s32;
	case 64: return s64;
	case 128: return s128;
	case 256: return s256;
	case 512: return s512;
	case 1024: return s1024;
	case 2048: return s2048;
	case 4096: return s4096;
	case 8192: return s8192;
	}
}

void change(int n)
{
    
    
	switch (n)
	{
    
    
	case 3: // left
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[i][0] == s0)
			{
    
    
				p[i][0] = p[i][1];
				p[i][1] = p[i][2];
				p[i][2] = p[i][3];
				p[i][3] = s0;
			}
			if (p[i][0] == s0)
			{
    
    
				p[i][0] = p[i][1];
				p[i][1] = p[i][2];
				p[i][2] = s0;
			}
			if (p[i][0] == s0)
			{
    
    
				p[i][0] = p[i][1];
				p[i][1] = s0;
			}
			if (p[i][1] == s0)
			{
    
    
				p[i][1] = p[i][2];
				p[i][2] = p[i][3];
				p[i][3] = s0;
			}
			if (p[i][1] == s0)
			{
    
    
				p[i][1] = p[i][2];
				p[i][2] = s0;
			}
			if (p[i][2] == s0)
			{
    
    
				p[i][2] = p[i][3];
				p[i][3] = s0;
			}
		}
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[i][0] == p[i][1] && p[i][0] != s0)
			{
    
    
				score += equal_int(p[i][0]);
				p[i][0] = equal_str(p[i][0]);
				p[i][1] = p[i][2];
				p[i][2] = p[i][3];
				p[i][3] = s0;
				if (p[i][1] == p[i][2] && p[i][1] != s0)
				{
    
    
					score += equal_int(p[i][1]);
					p[i][1] = equal_str(p[i][1]);
					p[i][2] = p[i][3];
				}
			}
			else if (p[i][1] == p[i][2] && p[i][1] != s0)
			{
    
    
				score += equal_int(p[i][1]);
				p[i][1] = equal_str(p[i][1]);
				p[i][2] = p[i][3];
				p[i][3] = s0;
			}
			else if (p[i][2] == p[i][3] && p[i][2] != s0)
			{
    
    
				score += equal_int(p[i][2]);
				p[i][2] = equal_str(p[i][2]);
				p[i][3] = s0;
			}
		}
		break;
	case 4: // right
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[i][3] == s0)
			{
    
    
				p[i][3] = p[i][2];
				p[i][2] = p[i][1];
				p[i][1] = p[i][0];
				p[i][0] = s0;
			}
			if (p[i][3] == s0)
			{
    
    
				p[i][3] = p[i][2];
				p[i][2] = p[i][1];
				p[i][1] = s0;
			}
			if (p[i][3] == s0)
			{
    
    
				p[i][3] = p[i][2];
				p[i][2] = s0;
			}
			if (p[i][2] == s0)
			{
    
    
				p[i][2] = p[i][1];
				p[i][1] = p[i][0];
				p[i][0] = s0;
			}
			if (p[i][2] == s0)
			{
    
    
				p[i][2] = p[i][1];
				p[i][1] = s0;
			}
			if (p[i][1] == s0)
			{
    
    
				p[i][1] = p[i][0];
				p[i][0] = s0;
			}
		}
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[i][3] == p[i][2] && p[i][3] != s0)
			{
    
    
				score += equal_int(p[i][3]);
				p[i][3] = equal_str(p[i][3]);
				p[i][2] = p[i][1];
				p[i][1] = p[i][0];
				p[i][0] = s0;
				if (p[i][2] == p[i][1] && p[i][2] != s0)
				{
    
    
					score += equal_int(p[i][2]);
					p[i][2] = equal_str(p[i][2]);
					p[i][1] = p[i][0];
				}
			}
			else if (p[i][2] == p[i][1] && p[i][2] != s0)
			{
    
    
				score += equal_int(p[i][2]);
				p[i][2] = equal_str(p[i][2]);
				p[i][1] = p[i][0];
				p[i][0] = s0;
			}
			else if (p[i][1] == p[i][0] && p[i][1] != s0)
			{
    
    
				score += equal_int(p[i][1]);
				p[i][1] = equal_str(p[i][1]);
				p[i][0] = s0;
			}
		}
		break;
	case 1: // up
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[0][i] == s0)
			{
    
    
				p[0][i] = p[1][i];
				p[1][i] = p[2][i];
				p[2][i] = p[3][i];
				p[3][i] = s0;
			}
			if (p[0][i] == s0)
			{
    
    
				p[0][i] = p[1][i];
				p[1][i] = p[2][i];
				p[2][i] = s0;
			}
			if (p[0][i] == s0)
			{
    
    
				p[0][i] = p[1][i];
				p[1][i] = s0;
			}
			if (p[1][i] == s0)
			{
    
    
				p[1][i] = p[2][i];
				p[2][i] = p[3][i];
				p[3][i] = s0;
			}
			if (p[1][i] == s0)
			{
    
    
				p[1][i] = p[2][i];
				p[2][i] = s0;
			}
			if (p[2][i] == s0)
			{
    
    
				p[2][i] = p[3][i];
				p[3][i] = s0;
			}
		}
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[0][i] == p[1][i] && p[0][i] != s0)
			{
    
    
				score += equal_int(p[0][i]);
				p[0][i] = equal_str(p[0][i]);
				p[1][i] = p[2][i];
				p[2][i] = p[3][i];
				p[3][i] = s0;
				if (p[1][i] == p[2][i] && p[1][i] != s0)
				{
    
    
					score += equal_int(p[1][i]);
					p[1][i] = equal_str(p[1][i]);
					p[2][i] = p[3][i];
				}
			}
			else if (p[1][i] == p[2][i] && p[1][i] != s0)
			{
    
    
				score += equal_int(p[1][i]);
				p[1][i] = equal_str(p[1][i]);
				p[2][i] = p[3][i];
				p[3][i] = s0;
			}
			else if (p[2][i] == p[3][i] && p[2][i] != s0)
			{
    
    
				score += equal_int(p[2][i]);
				p[2][i] = equal_str(p[2][i]);
				p[3][i] = s0;
			}
		}
		break;
	case 2: // down
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[3][i] == s0)
			{
    
    
				p[3][i] = p[2][i];
				p[2][i] = p[1][i];
				p[1][i] = p[0][i];
				p[0][i] = s0;
			}
			if (p[3][i] == s0)
			{
    
    
				p[3][i] = p[2][i];
				p[2][i] = p[1][i];
				p[1][i] = s0;
			}
			if (p[3][i] == s0)
			{
    
    
				p[3][i] = p[2][i];
				p[2][i] = s0;
			}
			if (p[2][i] == s0)
			{
    
    
				p[2][i] = p[1][i];
				p[1][i] = p[0][i];
				p[0][i] = s0;
			}
			if (p[2][i] == s0)
			{
    
    
				p[2][i] = p[1][i];
				p[1][i] = s0;
			}
			if (p[1][i] == s0)
			{
    
    
				p[1][i] = p[0][i];
				p[0][i] = s0;
			}
		}
		for (int i = 0; i != 4; i++)
		{
    
    
			if (p[3][i] == p[2][i] && p[3][i] != s0)
			{
    
    
				score += equal_int(p[3][i]);
				p[3][i] = equal_str(p[3][i]);
				p[2][i] = p[1][i];
				p[1][i] = p[0][i];
				p[0][i] = s0;
				if (p[2][i] == p[1][i] && p[2][i] != s0)
				{
    
    
					score += equal_int(p[2][i]);
					p[2][i] = equal_str(p[2][i]);
					p[1][i] = p[0][i];
				}
			}
			else if (p[2][i] == p[1][i] && p[2][i] != s0)
			{
    
    
				score += equal_int(p[2][i]);
				p[2][i] = equal_str(p[2][i]);
				p[1][i] = p[0][i];
				p[0][i] = s0;
			}
			else if (p[1][i] == p[0][i] && p[1][i] != s0)
			{
    
    
				score += equal_int(p[1][i]);
				p[1][i] = equal_str(p[1][i]);
				p[0][i] = s0;
			}
		}
		break;
	default:
		break;
	}
}

void input(char c1, char c2)
{
    
    
	c1 = _getch();
	c2 = _getch();
	switch (c2)
	{
    
    
	case 72: // up
		change(1);
		break;
	case 80: // down
		change(2);
		break;
	case 75: // left
		change(3);
		break;
	case 77: // right
		change(4);
		break;
	default:
		break;
	}
}

void random_n(vector<vector<string>> q, unsigned n)
{
    
    
	unsigned x, y;
	vector<unsigned> xx, yy;
	char c = 'A';
	for (int i = 0; i != 4; i++)
	{
    
    
		for (int j = 0; j != 4; j++)
			if (q[i][j] == s0)
			{
    
    
				q[i][j] = "";
				(q[i][j]).push_back(c);
				xx.push_back(i);
				yy.push_back(j);
				++c; // move to next character
			}
	}
	if (c == 'A') stop = 1; // indicate the end of the game
	else
	{
    
    
		srand((unsigned)time(NULL));
		int ran = rand() % (c - 'A'); // generate a random number
		string random_number = (n % 7 == 6) ? s4 : s2;
		p[xx[ran]][yy[ran]] = random_number;
	}
}

void init()
{
    
    
	initgraph(1200, 800);
	loadimage(0, _T("Welcome.jpg"), 1200, 800);
	Sleep(3000);
	loadimage(0, _T("Instruction.jpg"), 1200, 800);
	Sleep(3000);
	loadimage(0, _T("Board.jpg"), 1200, 800);
}

void print1(size_t i, size_t j)
{
    
    
	if (p[i][j] == s0)
	{
    
    
		loadimage(&i0, _T("0.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i0);
	}
	else if (p[i][j] == s2)
	{
    
    
		loadimage(&i2, _T("2.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i2);
	}
	else if (p[i][j] == s4)
	{
    
    
		loadimage(&i4, _T("4.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i4);
	}
	else if (p[i][j] == s8)
	{
    
    
		loadimage(&i8, _T("8.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i8);
	}
	else if (p[i][j] == s16)
	{
    
    
		loadimage(&i16, _T("16.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i16);
	}
	else if (p[i][j] == s32)
	{
    
    
		loadimage(&i32, _T("32.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i32);
	}
	else if (p[i][j] == s64)
	{
    
    
		loadimage(&i64, _T("64.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i64);
	}
	else if (p[i][j] == s128)
	{
    
    
		loadimage(&i128, _T("128.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i128);
	}
	else if (p[i][j] == s256)
	{
    
    
		loadimage(&i256, _T("256.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i256);
	}
	else if (p[i][j] == s512)
	{
    
    
		loadimage(&i512, _T("512.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i512);
	}
	else if (p[i][j] == s1024)
	{
    
    
		loadimage(&i1024, _T("1024.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i1024);
	}
	else if (p[i][j] == s2048)
	{
    
    
		loadimage(&i2048, _T("2048.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i2048);
	}
	else if (p[i][j] == s4096)
	{
    
    
		loadimage(&i4096, _T("4096.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i4096);
	}
	else if (p[i][j] == s8192)
	{
    
    
		loadimage(&i8192, _T("8192.jpg"), 150, 150);
		putimage(50 + 155 * j, 130 + 155 * i, &i8192);
	}
}

void print()
{
    
    
	for (size_t i = 0; i != 4; i++)
	{
    
    
		for (size_t j = 0; j != 4; j++)
		{
    
    
			print1(i,j); // print a single one
		}
	}
}

void print_score(unsigned n)
{
    
    
	setbkcolor(RGB(253, 236, 166)); // set the background colour
	settextstyle(120, 50, _T("Times New Roman")); // set the sytle
	settextcolor(BLUE); // set the colour
	TCHAR s01[5], s02[5], s03[5], s04[5], s05[5];
	swprintf_s(s01, _T("%d"), score % 10);
	swprintf_s(s02, _T("%d"), (score / 10) % 10);
	swprintf_s(s03, _T("%d"), (score / 100) % 10);
	swprintf_s(s04, _T("%d"), (score / 1000) % 10);
	swprintf_s(s05, _T("%d"), (score / 10000) % 10); // change into string first
	outtextxy(780, 300, s05);
	outtextxy(844, 300, s04);
	outtextxy(908, 300, s03);
	outtextxy(972, 300, s02);
	outtextxy(1036, 300, s01); // print
}

int main()
{
    
    
	init();
	unsigned cnt = 0;
	print();
	while (stop == 0) // when it's unfinshed
	{
    
    
		char a1 = '\0', a2 = '\0';
		input(a1, a2);
		random_n(p, cnt);
		print();
		print_score(score);
		++cnt;
	}
	loadimage(&imageOver, _T("over.jpg"), 320, 100);
	putimage(780, 500, &imageOver);
	getchar();
	return 0;
}

//Copyright: 2020 Teddy van Jerry

输出示例

1
2

素材

  • Welcome.jpg
    Welcome

  • Instruction.jpg
    Instruction

  • Board.jpg
    Board

  • over.jpg
    在这里插入图片描述

  • 0.jpg - 8192.jpg
    0
    2
    4
    8
    16
    32
    64
    128
    256
    512
    1024
    2048
    4096
    8192

分析

这在【C++ 程序】 2048游戏逻辑的基础上增加 EasyX 的图形库,关于图形库的使用及分析详见我的博客 【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)


ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。


See also

【C++ 程序】 井字棋游戏(人 VS 人)
【C++ 程序】 井字棋游戏(人 VS Lv1电脑)
【C++ 程序】 井字棋游戏(人 VS Lv2电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)
【C++ 程序】 五子棋游戏(人 VS 人)
【C++ 程序】 五子棋游戏(人 VS Lv1电脑)(思路及框架,内容待填充)
【C++ 程序】 随机数
【C++ 程序】 移动迷宫游戏
【C++ 程序】 贪吃蛇游戏
【C++ 程序】 数字推盘游戏(15-puzzle)
【C++ 程序】 2048游戏
【C++ 程序】 井字棋游戏(人 VS Lv3电脑)(战绩统计版)(EasyX 图形界面)
【C++ 程序】 井字棋游戏(人 VS 人)(EasyX 图形界面)

猜你喜欢

转载自blog.csdn.net/weixin_50012998/article/details/108558274