网站地图1 500 怎么做,网站规范建设,惠州网站制作找哪家,深圳公司设立本文实例为大家分享了C语言实现2048游戏具体代码#xff0c;供大家参考#xff0c;具体内容如下效果图:使用文本界面的屏幕绘图库 ncurses.设计思路:在满足条件情况下消除方块允许在游戏主界面(16 宫格)中任意一格输出数据实现代码:#include #include #include #include #inc…本文实例为大家分享了C语言实现2048游戏具体代码供大家参考具体内容如下效果图:使用文本界面的屏幕绘图库 ncurses.设计思路:在满足条件情况下消除方块允许在游戏主界面(16 宫格)中任意一格输出数据实现代码:#include #include #include #include #include #include void draw(); // 用于绘制游戏界面void play(); // 游戏运行的逻辑主体void init(); // 初始化函数用于完成一些必要的初始化操作void draw_one(int y, int x); // 绘制单个数字void cnt_value(int *new_y, int *new_x); //统计(y, x)对应的格子周围一圈的空格的个数int game_over(); // 结束游戏int cnt_one(int y, int x); //统计(y, x)对应的格子周围一圈的空格的个数// 游戏主界面是一个 4*4 的 16 宫格使用二维数组进行表示用 0 表示空格int a[4][4] { 0 };// 16 宫格中空格的个数int empty;int old_y, old_x;int main(){init();play();endwin();return 0;}void init(){int x, y;initscr(); //开启curses模式cbreak(); //开启cbreak模式除 DELETE 或 CTRL 等仍被视为特殊控制字元外一切输入的字元将立刻被一一读取noecho(); //echo() and noecho(): 此函式用来控制从键盘输入字元时是否将字元显示在终端机上curs_set(0); //设置光标模式empty 15;srand(time(0));x rand() % 4;y rand() % 4;a[y][x] 2;draw();}void draw(){int n, m, x, y;char c[4] {0, 0, 0, 0};clear(); //清除终端屏幕for(n 0; n 9; n 2){for(m 0; m 21; m){move(n, m);//将游标移动至 x,y 的位置addch(-);//在当前位置画字符-refresh();//将做清除萤幕的工作}}for(m 0; m 22; m 5){for(n 1; n 8; n){move(n, m);addch(|);refresh();}}for(y 0; y 4; y){for(x 0; x 4; x){draw_one(y, x);}}}void draw_one(int y, int x){int i, m, k, j;char c[5] {0x00};i a[y][x];m 0;while(i 0){j i % 10;c[m] j 0;i i / 10;}m 0;k (x 1) * 5 - 1;while(c[m] ! 0x00){move(2 * y 1, k);addch(c[m]);k--;}}void play(){int x, y, i, new_x, new_y, temp;int old_empty, move;char ch;while(1){move 0;old_empty empty;ch getch();switch(ch){case 97: //左移 acase 104: // hcase 68: // 左移方向键for(y 0; y 4; y)for(x 0; x 4; ){if(a[y][x] 0){x;continue;}else{for(i x 1; i 4; i){if(a[y][i] 0){continue;}else{if(a[y][x] a[y][i]){a[y][x] a[y][i];a[y][i] 0;empty;break;}else{break;}}}x i;}}for(y 0; y 4; y)for(x 0; x 4; x){if(a[y][x] 0){continue;}else{for(i x; (i 0) (a[y][i - 1] 0); i--){a[y][i - 1] a[y][i];a[y][i] 0;move 1;}}}break;case 100: //右移 dcase 108: // lcase 67: //右移方向键for(y 0; y 4; y)for(x 3; x 0; ){if(a[y][x] 0){x--;continue;}else{for(i x - 1; i 0; i--){if(a[y][i] 0){continue;}else if(a[y][x] a[y][i]){a[y][x] a[y][i];a[y][i] 0;empty;break;}else{break;}}x i;}}for(y 0; y 4; y)for(x 3; x 0; x--){if(a[y][x] 0){continue;}else{for(i x; (i 3) (a[y][i 1] 0); i){a[y][i 1] a[y][i];a[y][i] 0;move 1;}}}break;case 119: //上移 wcase 107: //kcase 65: //上移方向键for(x 0; x 4; x)for(y 0; y 4; ){if(a[y][x] 0){y;continue;}else{for(i y 1; i 4; i){if(a[i][x] 0){continue;}else if(a[y][x] a[i][x]){a[y][x] a[i][x];a[i][x] 0;empty;break;}else{break;}}y i;}}for(x 0; x 4; x)for(y 0; y 4; y){if(a[y][x] 0){continue;}else{for(i y; (i 0) (a[i - 1][x] 0); i--){a[i - 1][x] a[i][x];a[i][x] 0;move 1;}}}break;case 115: //下移 scase 106: //jcase 66: //下移方向键for(x 0; x 4; x)for(y 3; y 0; ){if(a[y][x] 0){y--;continue;}else{for(i y - 1; i 0; i--){if(a[i][x] 0){continue;}else if(a[y][x] a[i][x]){a[y][x] a[i][x];a[i][x] 0;empty;break;}else{break;}}y i;}}for(x 0; x 4; x)for(y 3; y 0; y--){if(a[y][x] 0){continue;}else{for(i y; (i 3) (a[i 1][x] 0); i){a[i 1][x] a[i][x];a[i][x] 0;move 1;}}}break;case Q:case q:game_over();break;default:continue;break;}if(empty 0)game_over();if((empty ! old_empty) || (move 1)){do{new_x rand() % 4;new_y rand() % 4;}while(a[new_y][new_x] ! 0);cnt_value(new_y, new_x);do{temp rand() % 4;}while(temp 0 || temp 2);a[new_y][new_x] temp 1;empty--;}draw();}}int cnt_one(int y, int x){int value 1;if(y - 1 0)a[y - 1][x] ? 0 : value;if(y 1 4)a[y 1][x] ? 0 : value;if(x - 1 0)a[y][x - 1] ? 0 : value;if(x 1 4)a[y][x 1] ? 0 : value;if(y - 1 0 x - 1 0)a[y - 1][x - 1] ? 0 : value;if(y - 1 0 x 1 4)a[y - 1][x 1] ? 0 : value;if(y 1 4 x - 1 0)a[y 1][x - 1] ? 0 : value;if(y 1 4 x 1 4)a[y 1][x 1] ? 0 : value;return value;}void cnt_value(int *new_y, int *new_x){int max_x, max_y, x, y, value;int max 0;max cnt_one(*new_y, *new_x);for(y 0; y 4; y)for(x 0; x 4; x){if(!a[y][x]){value cnt_one(y, x);if(value max old_y ! y old_x ! x){*new_y y;*new_x x;old_x x;old_y y;break;}}}}int game_over(){sleep(1); //暂停1秒endwin(); //关闭curses并重置tty(结束curses编程时最后调用的一个函数)exit(0);}以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。