C语言实现2048小游戏

本文档介绍如何使用C语言实现2048小游戏。文章详细讲解了游戏原理,包括4x4游戏板的初始化、数字生成、块的移动与合并规则,以及胜利条件。此外,还提供了游戏文档的获取途径和源代码,便于读者理解并参考进行项目实践。
摘要由CSDN通过智能技术生成

大部分同学学习C语言编程以后不知道能通过什么样的项目才可以锻炼自己的思维功力,2048相信大家都应该熟悉,不管是手机上还是网页版的相信大家都玩过,这个简单的控制台版本的游戏是我曾经在伟易达上班时一个嵌入式应用游戏部门的大佬设计的,适合于喜欢用C语言写一些简易的游戏的朋友,逻辑性很强。

一、2048游戏原理

在最初的游戏, 它始于一个空4 x 4游戏板。

1)在空位置的游戏板上,每一轮游戏产生一个“2”或“4”随机的数字。

2)接下来,玩家输入的上移,下移,左移或右移命令移动块。两个相邻块相同的号码,若是Q,可以组合成一个块数量2Q。

3)如果没有空间产生一个新的数字块,玩家则game over。

4)想赢得游戏,玩家需要产生一块2048数字块。

二、2048游戏文档

当然,这些游戏的逻辑不是大家闷着脑子就能空想出来的,它一定有很规范的说明文档,由专业的人来书写,最后软件工程师参考对应的文档编写自己的代码

篇幅有限,详细的下载链接:

链接:https://pan.baidu.com/s/1Daan58WN-A95BeYQcmSyDA
提取码:m6ie

当然也可以访问Github网站,这是一个开源的项目,后面各位牛逼的大佬经过移植后,运行在各个平台下,原版本链接如下:

http://gabrielecirulli.github.io/2048/

三、2048游戏源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>

//num
#define FALSE	0
#define TRUE	1

#define EMPTY_CELL     	0

#define GMAE_ROW	 	4
#define GMAE_COL		4

//GameState
#define STATE_SELECT   	0
#define STATE_PREPARE  	1
#define STATE_PALYING  	2
#define STATE_EXIT      3

//GameMode
#define MODE_NONE      	0
#define MODE_NORMAL    	1
#define MODE_DEBUG     	2

//Select Index
#define INDEX_MAXNUM    3
#define INDEX_NORMAL    0
#define INDEX_DEBUG     1
#define INDEX_EXIT      2

//Command
#define COM_LEFT		'a'
#define COM_RIGHT		'd'
#define COM_UP			'w'
#define COM_DOWN 		's'
#define COM_QUIT		'q'

//direction
#define DIR_HEAD        0xe0
#define KEY_UP			0xe048
#define KEY_DOWN 		0xe050
#define KEY_LEFT		0xe04b
#define KEY_RIGHT		0xe04d


#define ESC 			0x1B
#define ENTER			0x0D

//type
typedef unsigned int    Uint;
typedef unsigned short  Ushort;
typedef unsigned char   Uchar;

//declaration
static void GM_Init(void);
static void GM_End(void);

static Uint GM_SelectInit(void);
static Uint GM_SelectHandle(void);
static Uint GM_SelectEnd(void);

static Uint GM_PrepareInit(void);
static Uint GM_PrepareHandle(void);
static Uint GM_PrepareEnd(void);

static Uint GM_PlayingInit(void);
static Uint GM_PlayingHandle(void);
static Uint GM_PlayingEnd(void);


static Uint GM_SelectHandleEnter(void);
static Uint GM_SelectHandleEsc(void);
static void GM_PrintSelectMode(void);
static void GM_RandAddOneNum(void);
static Uchar GM_FromFileAddNum(void);
static Uchar GM_InputAddOneNum(void);
static Uchar GM_NotMoreMove(void);
static void GM_PrintBoard(void);

static Uchar GM_CheckWin2048(void);
static Uchar GM_PlayingPull(void);
static Uchar GM_CombineRight(Uint *array, int num);
static Uchar GM_CombineLeft(Uint *array, int num);
static Uchar GM_MoveRight(Uint *array, int num);
static Uchar GM_MoveLeft (Uint *array, int num);

//struct
typedef  struct gameinfo {
	Uint  Board[GMAE
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值