C语言实现的2048小游戏

给大一新生写的一个小游戏。

缺点:函数名称和功能略微不对应,函数功能写得比较乱,时间记录有误差,可扩展性弱。

优点:通过几个配置数组,将单位方块移动的函数缩短到30行以内。

#include 
#include 
#include 
#include 
#include 

//坐标常量 
const int squareSize = 4;
const int locationX[squareSize] = { 4, 6, 8, 10 };
const int locationY[squareSize] = { 2, 8, 14, 20 };
const int locationTime[2] = { 13, 12};
const int directionS[2][3] = { {1, 4, 1}, {2, -1, -1} };
//左 右 上 下
//0  1  2  3
const int directionXY[4][2] = { {0,-1}, {0, 1}, {-1, 0}, {1, 0} };

//矩阵内容 
int square[squareSize][squareSize];

//时间相关全局变量 
int t_hour = 0;
int t_min = 0;
int t_sec = 0;
int t_ms = 0;

//得分相关全局变量
int score = 0;

//步数相关全局变量
int step = 0;

//存活全局变量
int alive = 1;

//函数声明 
void gotoxy(int x, int y);
void sleep(int n);
void gotoxy(int x, int y);
void move(int direction);
void play();
int judge(int x, int y, int direction);
void printSquare();
void init();
void randSquare();
void printScore();


//打印当前时间
void printTime()
{
	gotoxy(locationTime[0], locationTime[1]);
	printf("%02d:%02d:%02d", t_hour, t_min, t_sec);
}

//睡眠函数 不是很准确 推荐用系统时间换算 
void sleep(int n)
{
	Sleep(n);
	t_ms += n;
	if (t_ms >= 1000)
	{
		t_ms -= 1000;
		t_sec += 1;
	}
	if (t_sec >= 60)
	{
		t_sec -= 60;
		t_min += 1;
	}
	if (t_min >= 60)
	{
		t_sec -= 60;
		t_hour += 1;
	}
	gotoxy(locationTime[0], locationTime[1]);
	printTime();
}

//光标移动函数
void gotoxy(int x, int y)
{
	COORD pos;
	pos.X = y;
	pos.Y = x;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}



//左 右 上 下
//0  1  2  3
//游戏单元 移动函数
void move(int direction)
{
	//const int directionS[2][3] = { {1, 4, 1}, {2, -1, -1} };
	//向左移动  i = directionS[0][0]; i != directionS[0][1] ; i += directionS[0][2]; 
	//向右移动  i = directionS[1][0]; i != directionS[1][1] ; i += directionS[1][2]; 
	//向上移动  i = 1; i != 4 ; i++
	//向下移动  i = 2; i != -1; i--
	int mydirection = direction % 2;
	for(int k = 0;k < 3;k++)
	{
		for (int i = directionS[mydirection][0]; i != directionS[mydirection][1]; i += directionS[mydirection][2])
		{
			for (int j = 0; j < 4; j++)
			{
				if (direction / 2 == 0)
				{
					//横向移动
					if (square[j][i] != 0)
					{
						judge(j, i, direction);
						//getch();
					}
				}
				else
				{
					//纵向移动
					if (square[i][j] != 0)
					{
						judge(i, j, direction);
						//getch();
					}
				}
			}	
		}
		sleep(80);
	}
	//生成一个新的方块
	randSquare();
	step += 1;
	printScore();
}

int judge(int x, int y, int direction)
{
	int toX = x + directionXY[direction][0];
	int toY = y + directionXY[direction][1];
	if (square[toX][toY] == 0 || square[toX][toY] == square[x][y])
	{
		if(square[toX][toY] != 0)
		{
			score += square[x][y] * square[x][y];
		}
		square[toX][toY] += square[x][y];
		square[x][y] = 0;
		gotoxy(locationX[toX],locationY[toY]);
		printf("%4d", square[toX][toY]);
		gotoxy(locationX[x],locationY[y]);
		printf("%4d", square[x][y]);
		return 1;
	}
	else
	{
		return 0;
	}
}

//游戏函数
//左 右 上 下
//0  1  2  3
void play()
{
	char ch;
	for (;alive;)
	{
		ch = 'o';
		if (kbhit())
		{
			ch = getch() | 32;
		}
		switch (ch)
		{
		case 'w': //向上移动
			move(2);
			break;
		case 's'://向下移动
			move(3);
			break;
		case 'a'://向左移动
			move(0);
			break;
		case 'd'://向右移动
			move(1);
			break;
		default://不进行移动
			break;
		}
		sleep(20);
		gotoxy(15, 20);
	}
}


void printScore()
{
	gotoxy(6, 28);
	printf("%10d", step);
	gotoxy(10, 28);
	printf("%10d", score);
}


//打印棋盘
void printSquare()
{
	printf("┏━━━━━━━━━━━━━━━━━━┓\n");
	printf("┃            2048 小游戏             ┃\n");
	printf("┗━━━━━━━━━━━━━━━━━━┛\n");
	printf("┏━━┳━━┳━━┳━━┓┏━━━━━┓\n");
	printf("┃   0┃   0┃   0┃   0┃┃   步数   ┃\n");
	printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n");
	printf("┃   0┃   0┃   0┃   0┃┃         0┃\n");
	printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n");
	printf("┃   0┃   0┃   0┃   0┃┃   得分   ┃\n");
	printf("┣━━╋━━╋━━╋━━┫┣━━━━━┫\n");
	printf("┃   0┃   0┃   0┃   0┃┃         0┃\n");
	printf("┗━━┻━━┻━━┻━━┛┗━━━━━┛\n");
	printf("┏━━━━━━━━━━━┓┏━━━━━┓\n");
	printf("┃    时长:00:00:00    ┃┃  徐络络  ┃\n");
	printf("┗━━━━━━━━━━━┛┗━━━━━┛\n");
}

//初始化函数
void init()
{
	srand(time(NULL));
	printSquare();
	//时间相关全局变量 
	t_hour = 0;
	t_min = 0;
	t_sec = 0;
	t_ms = 0;
	//得分相关全局变量
	score = 0;
	//步数相关全局变量
	step = 0;
	for (int i = 0; i < squareSize; i++)
	{
		for(int j = 0;j < squareSize;j++)
		{
			square[i][j] = 0;
		}
	}
	//随机生成一个2或者4
	randSquare();	
}

//随机生成一个方块
void randSquare()
{
	int x = rand() % 4;
	int y = rand() % 4;
	int number = rand() % 2 + 1;
	int legal = 0;
	number *= 2;
	if (square[x][y] != 0)
	{
		for (int i = 0; i < 4 && !legal; i++)
		{
			for (int j = 0; j < 4 && !legal; j++)
			{
				if (square[i][j] == 0)
				{
					gotoxy(locationX[i], locationY[j]);
					printf("%4d", number);
					square[i][j] = number;
					legal = 1;
				}
			}
		}
	}
	else
	{
		gotoxy(locationX[x], locationY[y]);
		printf("%4d", number);
		square[x][y] = number;
		legal = 1;
	}
	if (legal == 0)
	{
		alive = 0;
	}
	gotoxy(15, 20);
}

int main()
{
	//先初始化
	init();
	//开始游戏
	play();
	//游戏结束 光标移动至游戏界面最下方
	gotoxy(15, 20);
	//暂停
	printf("游戏结束!");
	getch();
	return 0;
}


你可能感兴趣的:(C/C++课程设计代码,c语言,游戏)