#include "my_head.h"//可以是自己设置的,可以是系统的
int game_array[12] = {2,4,8,16,32,64,128,256,512,1024,2048,0};
int gameover ;
void game_start()
{
matrix_array[4][4] ;
//游戏界面初始化数值
//获取随机因子
srand(time(NULL));
int row_1,col_1,row_2,col_2;
row_1 = rand()%4;
col_1 = rand()%4;
while(1)
{
row_2 = rand()%4;
col_2 = rand()%4;
//只有位置不一样时,才退出
if(row_1 != row_2 && col_1 != col_2)
{
break;
}
}//随机生成两个随机坐标
// 下面的语句等价于注释的语句
// int num=;
// num = rand()%2;
// game_array[num];
//先执行 rand()%2 --->获取到数字 0/1
//再把获取到的数字作为是数组的下标 ---》game_array[0] / game_array[1]
matrix_array[row_1][col_1] = game_array[rand()%2];
matrix_array[row_2][col_2] = game_array[rand()%2];
red_bmp("up.bmp",0,400);
red_bmp("down.bmp",200,400);
red_bmp("left.bmp",400,400);
red_bmp("right.bmp",600,400);
//随机赋值2,4给数组的随机坐标
show_2048_game();
}
void game_over() {
gameover = 0; //将gameover设置为0 如果以下循环不能使gameover变为1就结束游戏
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (matrix_array[i][j] == 0) { //还存在0就可以继续
gameover = 1;
}
if (i >= 1) { //i>=1是因为i-1要>=0
if (matrix_array[i][j] ==matrix_array[i - 1][j]) { //如果上下相邻元素右相等的就继续
gameover = 1;
}
}
if (j >= 1) { //j>=1是因为j-1要>=0
if (matrix_array[i][j] == matrix_array[i][j - 1]) { //如果左右相邻元素右相等的就继续
gameover = 1;
}
}
}
}
}
//随机生成2,4在游戏的空位值里 包含:随机生成数字2,4 判断是否为空位置
void game_keep ()
{
int zero_number;
//先统计空位的个数
zero_number = return_null_number();
if(0 == zero_number)
{
printf("没有空位\n");
game_over();
if(1==gameover)
{
printf("游戏即将结束,在努力观察一下\n"); //游戏还能继续
}
else if(0==gameover)//判断游戏失败了
{
printf("游戏已结束\n");
up_gameopen();
}
return;
}//调用函数看是否有空位置
//获取随机因子
srand(time(NULL));
int row_rank,col_rank;
while(1)//循环找空位置
{
row_rank = rand()%4;
col_rank = rand()%4;
//随机生成一个空位置
if(0 == matrix_array[row_rank][col_rank])
{
matrix_array[row_rank][col_rank] = game_array[rand()%2];//空位置复制
break;
}
}
}
int return_null_number()
{
int sum = 0;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(0 == matrix_array[i][j])
{
sum++;
}
}
}
return sum;
}//统计空位的个数
void move_down()//往下移动功能
{
int tmp;//临时的存储空间
int new_size = 3;
for(int col=0; col<4; col++)
{
new_size = 3;
tmp = 0;
for(int row=0; row<4; row++)
{
//先判断位置是否为空位(0)
if(0 == matrix_array[3-row][col])
{
continue;
}
//如果不是空
//临时位置如果是空的,提取数据
//row = 1,tmp拿到数据
//那么就会在row为2或者3的时候才会对比
if(tmp == 0)
{
tmp = matrix_array[3-row][col];
matrix_array[3-row][col] = 0;
}
else
{
//对比数据
//相同
if(tmp == matrix_array[3-row][col])
{
matrix_array[new_size--][col] = tmp*2;
//容器自身的内容已经被用掉,因此需要清空
matrix_array[3-row][col] = 0;
tmp = 0;
}
//不相同
else
{
//将tmp中的数据存放到矩阵中
matrix_array[new_size--][col] = tmp;
tmp = matrix_array[3-row][col];
matrix_array[3-row][col] = 0;
}
}
}
//如果临时位置不为0,那么需要将数据存放回矩阵中
if(tmp != 0)
{
matrix_array[new_size--][col] = tmp;
}
}
}//往下移动代码结束
void move_up()//往上移动功能
{
int tmp;//临时的存储空间
int new_size = 0;
for(int col=0; col<4; col++)
{
new_size = 0;
tmp = 0;
for(int row=0; row<4; row++)
{
//先判断位置最上面的是否为空位(0)
if(0 == matrix_array[row][col])
{
continue;
}
//如果不是空
//临时位置如果是空的,提取数据
//row = 1,tmp拿到数据
//那么就会在row为2或者3的时候才会对比
if(tmp == 0)
{
tmp = matrix_array[row][col];
matrix_array[row][col] = 0;
}
else
{
//对比数据
//相同
if(tmp == matrix_array[row][col])
{
matrix_array[new_size++][col] = tmp*2;
//容器自身的内容已经被用掉,因此需要清空
matrix_array[row][col] = 0;
tmp = 0;
}
//不相同
else
{
//将tmp中的数据存放到矩阵中
matrix_array[new_size++][col] = tmp;
tmp = matrix_array[row][col];
matrix_array[row][col] = 0;
}
}
}
//如果临时位置不为0,那么需要将数据存放回矩阵中
if(tmp != 0)
{
matrix_array[new_size++][col] = tmp;
}
}
}//往上移动代码结束
void move_left()//往左移动功能
{
int tmp;//临时的存储空间
int new_size = 0;
for(int row=0; row<4; row++)
{
new_size = 0;
tmp = 0;
for(int col=0; col<4; col++)
{
//先判断位置最左边面的是否为空位(0)
if(0 == matrix_array[row][col])
{
continue;
}
//如果不是空
//临时位置如果是空的,提取数据
//row = 1,tmp拿到数据
//那么就会在row为2或者3的时候才会对比
if(tmp == 0)
{
tmp = matrix_array[row][col];
matrix_array[row][col] = 0;
}
else
{
//对比数据
//相同
if(tmp == matrix_array[row][col])
{
matrix_array[row][new_size++] = tmp*2;
//容器自身的内容已经被用掉,因此需要清空
matrix_array[row][col] = 0;
tmp = 0;
}
//不相同
else
{
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
在开发版上,用C语言写了一个现在比较火的小游戏2048,首先我设定我的游戏是4*4的,然后可以记录历史最高分,程序开始运行时,先初始化4*4的游戏棋盘,然后随机在棋盘上的空白位置放置2或者4,然后调用屏幕触碰事件函数等待用户对屏幕进行操作,用户的上滑下滑等操作的判断独立写成一个函数,每次判断就调用一次,判断后,系统就会对棋盘上的棋子进行检测,如果该方向上有同样的数字则合并,靠后的则被空白替代,并且有一定几率在空白位置刷出来新的2或4,然后每次读取棋盘数组每个数据时都会和当前历史最高分进行比较,历史最高分初定义为全局的静态变量,然后在显示数字时也把历史最高显示给用户,主函数是一个有条件判断和触摸等待的while循环,用户结束后,会进入请求界面,如果用户点击再来一次按钮,也游戏重新开始,但历史最高分不会改变,如果用户选择退出,则整个程序关闭。代码一共1000行(不包括触摸库等)共用4天时间完成。
资源推荐
资源详情
资源评论
收起资源包目录
2048.rar (45个子文件)
2048
16.bmp 59KB
gameopen.jpg 97KB
2048.c 4KB
lcd.h 695B
0.bmp 59KB
.vscode
c_cpp_properties.json 447B
settings.json 631B
launch.json 693B
64.bmp 59KB
2048.bmp 59KB
lcd.c 4KB
512.bmp 59KB
256.bmp 59KB
show_pic.o 5KB
8.bmp 59KB
behind_gameove.o 3KB
libactive_text.so 15KB
gameover.jpg 176KB
32.bmp 59KB
libmain.so 23KB
4.bmp 59KB
dir_retrieval.c 2KB
up.bmp 47KB
128.bmp 59KB
show_pic.c 5KB
text 14KB
behind_gameove.c 2KB
jconfig.h 2KB
game_touch.c 3KB
dir_retrieval.o 2KB
game_touch.o 3KB
right.bmp 47KB
jpeglib.h 48KB
down.bmp 47KB
my_head.h 2KB
1024.bmp 59KB
left.bmp 47KB
main 7KB
main.c 1KB
jmorecfg.h 15KB
game.c 11KB
2.bmp 59KB
game.o 6KB
lcd.o 4KB
jerror.h 14KB
共 45 条
- 1
资源评论
希望成为main
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功