今天来聊聊关于一些简单的编程代码做小游戏教程,一些简单的编程代码的文章,现在就为大家来简单介绍下一些简单的编程代码做小游戏教程,一些简单的编程代码,希望对各位小伙伴们有所帮助。
1、小游戏2048源码:#include#include#include#include#includeint jsk( ); //计算空格数void rsgm( ); //重置游戏void inkey( ); //按键输入void left( ); //向左移动void right( ); //向右移动void up( ); //向上移动void down( ); //向下移动void show( ); //输出界面void adnum( ); //添加随机数void yes( ); //游戏是否结束(1是0否)void gtxy(int x, int y); //控制光标位置的函数int a[4][4]; //存储16个格子中的数字int score = 0; //每局得分int best = 0; //最高得分int ifnum; //是否需要添加数字(1是0否)int over; //游戏结束标志(1是0否)int i,j,k;int main( ){ rsgm( ); //重置游戏 inkey( ); //按键输入 return 0;}void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0){ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);} //用于控制字符颜色的函数void rsgm( ) //重置游戏{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //启动随机数发生器 int n = rand( ) % 16; //随机函数产生0-15的数字 for (i = 0; i < 4; i++) {for (j = 0; j < 4; j++) { if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; } else { a[i][j] = 4; } n--; } else { a[i][j] = 0; n--; } } } adnum( ); system("cls"); CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);setColor(14, 0); //设置字体淡红色。
2、背景为黑色printf(" 2048小游戏"); setColor(7, 0); //恢复白字黑底printf("┌──────┬──────┬──────┬──────┐");printf("│ │ │ │ │");printf("├──────┼──────┼──────┼──────┤");printf("│ │ │ │ │");printf("├──────┼──────┼──────┼──────┤");printf("│ │ │ │ │");printf("├──────┼──────┼──────┼──────┤");printf("│ │ │ │ │");printf("└──────┴──────┴──────┴──────┘"); show( );}void show( ) //输出界面{ for(i=0;i<4;i++) for(j=0;j<4;j++) { gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字 if(a[i][j]==0){printf(" "); setColor(7, 0); printf("│");} else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); } else if (a[i][j] == 4) {setColor(13, 0); } else if (a[i][j] == 8) {setColor(12, 0); } printf(" %d ", a[i][j]); setColor(7, 0); printf("│"); } else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); } else if (a[i][j] == 32) {setColor(10, 0); } else if (a[i][j] == 64) {setColor(2, 0); } printf(" %d ", a[i][j]); setColor(7, 0); printf("│"); } else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); } else if (a[i][j] == 256) {setColor(1, 0); } else if (a[i][j] == 512) {setColor(13, 0); } printf(" %d ", a[i][j]); setColor(7, 0); printf("│"); } else if (a[i][j] best) { best = score; } if (ifnum) { adnum( ); show( ); } }}int jsk( ) //计算空格数{ int n = 0; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } } return n;}void left( ) //向左移动{ for (i = 0; i < 4; i++) {for (j = 1, k = 0; j 0) { if ( a[i][k] == a[i][j]) { a[i][k] *= 2; k++; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; } else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; } else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; } k++; } } } }}void right( ) //向右移动{for (i = 0; i = 0; j--) {if (a[i][j] > 0) { if (a[i][k] == a[i][j]) {a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; } else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; } else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; } } } }}void up( ) //向上移动{for (i = 0; i < 4; i++) {for (j = 1, k = 0; j 0) {if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; } else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; } else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; } k++; } } } }}void down( ) //向下移动{ for (i = 0; i = 0; j--) {if (a[j][i] > 0) {if (a[k][i] == a[j][i]) {a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; } else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; } else {a[k - 1][i] = a[j][i]; if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; } } } }}void adnum( ) //添加随机数{ srand(time(0)); //启动随机数发生器 int n = rand( ) % jsk( ); for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) { if (a[i][j] == 0) { if (n != 0) { n--; } else {int k = rand() % 3; if (k == 0 || k == 1) {a[i][j] = 2; return; } else {a[i][j] = 4; return; } } } } }}void yes( ) //游戏是否结束{ for (int i = 0; i < 4; i++) {for (int j = 0; j < 3; j++) {if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }} } over = 1;}void gtxy(int x, int y) //控制光标位置的函数{ COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}。
相信通过一些简单的编程代码这篇文章能帮到你,在和好朋友分享的时候,也欢迎感兴趣小伙伴们一起来探讨。