跟着B站老师 做的,但最后老师没做完,我完善了一下(实现核心功能以及游戏成功、失败的弹窗界面)。【C/C++】如何快速用C语言写一个2048游戏?从零到成功,只需2小时,你还在担心学不会编程吗_哔哩哔哩_bilibili
#include#include #include #include #include #include #include #include #include using namespace std; #define TARGET 2048 #define SIZE 4 #define INTERVAL 15 #define GRID_W 100 enum Color //枚举格子颜色 { zero = RGB(205, 193, 180), //0的颜色 twoTo1 = RGB(238, 228, 218), //2的颜色 twoTo2 = RGB(237, 224, 200), //4的颜色 twoTo3 = RGB(242, 177, 121), //8的颜色 twoTo4 = RGB(245, 149, 99), //16的颜色 twoTo5 = RGB(246, 124, 95), //32的颜色 twoTo6 = RGB(246, 94, 59), //64的颜色 twoTo7 = RGB(242, 177, 121), //128的颜色 twoTo8 = RGB(237, 204, 97), //256的颜色 twoTo9 = RGB(255, 0, 128), //512的颜色 twoTo10 = RGB(145, 0, 72), //1024的颜色 twoTo11 = RGB(242, 17, 158), //2048的颜色 back = RGB(187, 173, 160), //背景颜色 }; Color arr[13] = {zero,twoTo1,twoTo2,twoTo3,twoTo4,twoTo5,twoTo6,twoTo7,twoTo8,twoTo9,twoTo10,twoTo11, back}; bool isLose; int map[SIZE][SIZE]; bool flag;//添加flag判断是否发生了移动。若无移动则不执行gameInit(b); bool isWin; void upMove(){ for (int j = 0; j < SIZE; j++) { int temp = 0; for (int i = 1; i < SIZE; i++) { if ( map[i][j] ) { if(map[temp][j] == 0){ map[temp][j] = map[i][j]; map[i][j] = 0; flag =true; } else if(map[temp][j] == map[i][j]){ map[temp][j] += map[i][j]; map[i][j] = 0; if(map[temp][j] == TARGET){ isWin =true; break; } temp++; flag =true; } else { map[temp + 1][j] = map[i][j]; if(temp +1 != i){ map[i][j] =0 ; flag =true; } temp++; } } } } } void rightMove(){ for (int i = SIZE -1; i >= 0; i--) { int temp = SIZE-1; for (int j = SIZE -2; j >= 0; j--) { if ( map[i][j] ) { if(map[i][temp] == 0){ map[i][temp] = map[i][j]; map[i][j] = 0; flag =true; } else if(map[i][temp] == map[i][j]){ map[i][temp] += map[i][j]; map[i][j] = 0; if(map[i][temp] ==TARGET){ isWin =true; break; } flag =true; temp--; } else { map[i][temp -1] = map[i][j]; if(temp -1 != j){ map[i][j] =0 ; flag =true; } temp--; } } } } } void downMove(){ for (int j = SIZE -1; j >= 0; j--) { int temp = SIZE-1; for (int i = SIZE -2; i >= 0; i--) { if ( map[i][j] ) { if(map[temp][j] == 0){ map[temp][j] = map[i][j]; map[i][j] = 0; flag =true; } else if(map[temp][j] == map[i][j]){ map[temp][j] += map[i][j]; map[i][j] = 0; if(map[temp][j] ==TARGET){ isWin =true; break; } flag =true; temp--; } else { map[temp - 1][j] = map[i][j]; if(temp -1 != i){ map[i][j] =0 ; flag =true; } temp--; } } } } } void leftMove(){ for (int i = 0; i < SIZE; i++) { int temp = 0; for (int j = 1; j < SIZE; j++) { if ( map[i][j] ) { if(map[i][temp] == 0){ map[i][temp] = map[i][j]; map[i][j] = 0; flag =true; } else if(map[i][temp] == map[i][j]){ map[i][temp] += map[i][j]; map[i][j] = 0; if(map[i][temp] ==TARGET){ isWin =true; break; } flag =true; temp++; } else { map[i][temp + 1] = map[i][j]; if(temp +1 != j){ map[i][j] =0 ; flag =true; } temp++; } } } } } void keyDeal(){ char key =_getch(); flag = false; isWin=false; switch (key) { case 'w': case 'W': upMove(); break; case 'S': case 's': downMove(); break; case 'A': case 'a': leftMove(); break; case 'd': case 'D': rightMove(); break; default: break; } } int create_num(){ //srand((int)time(NULL)); int num = rand()%10; if(num >2){ return 2; } else return 4; } //产生随机数(2或4) void gameInit(int num){ srand((int)time(NULL)); for (int i = 0; i < num; ) { int x = rand()%SIZE; int y = rand()%SIZE; if(map[x][y] ==0){ map[x][y] = create_num(); i++; } } } void judge_lose(){ isLose = true; for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE-1; ++j) { if (map[i][j] == map[i][j + 1] || map[j][i] == map[j + 1][i]) { isLose = false; return; } } } } void draw(){ setbkcolor(RGB(187,173,160)); cleardevice(); for(int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { int m = (j+1)*INTERVAL + j*GRID_W; int n = (i+1)*INTERVAL + i*GRID_W; COLORREF color = arr[(int) log2( map[i][j] )]; // cout<<(int) log2( map[i][j] )<



