玩法:
即模仿市场上的2048游戏。
效果图:
实现原理:
- 就当是一个非常复杂的模拟题,整理好思路,搭好大体结构不难写。
- 重点是如何判断方块是否合并那里,还有检测是否游戏结束。
- 分数用一个全局变量储存,每次更新出性方块时加分。
- 为方块分数加了个等比例增加的#号,要不玩起来难受的要死。
-
源代码:
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<conio.h> int a[4][4], SCORE = 0, prt; int num() { return rand()%4; } int scan() { int i, j, t = 0; for(i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { if (a[i][j] == 0) { return 1; } } } return 0; } void crt() { SCORE += 2; int i,j; for(;1;) { i = num(); j = num(); printf("%d %d", i, j); if (a[i][j] == 0) { a[i][j] = 2; break; } } } void frmt() { int i, d; for (i = 0; i < 4; i++) { for (d = 0; d < 4; d++) { a[i][d] = 0; } } } void put() { int i,d; printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\tScode: \t%d\n\n\t\t\t┌──────┬──────┬──────┬──────┐\n\t\t\t", SCORE); for (i = 0; i < 4; i++) { for (d = 0; d < 4; d++) { if (a[i][d] == 2048) printf("│##########%d ", a[i][d]); else if (a[i][d] == 1024) printf("│#########%2d ", a[i][d]); else if (a[i][d] == 512) printf("│########%3d ", a[i][d]); else if (a[i][d] == 256) printf("│#######%4d ", a[i][d]); else if (a[i][d] == 128) printf("│######%5d ", a[i][d]); else if (a[i][d] == 64) printf("│#####%6d ", a[i][d]); else if (a[i][d] == 32) printf("│####%7d ", a[i][d]); else if (a[i][d] == 16) printf("│###%8d ", a[i][d]); else if (a[i][d] == 8) printf("│##%9d ", a[i][d]); else if (a[i][d] == 4) printf("│#%10d ", a[i][d]); else if (a[i][d] == 2) printf("│%11d ", a[i][d]); else printf("│ ", a[i][d]); } printf("│"); if (i != 3) { printf("\n\t\t\t├──────┼──────┼──────┼──────┤\n\t\t\t"); } } printf("\n\t\t\t└──────┴──────┴──────┴──────┘\n\n\n"); } void A_() { int i, j, t; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (a[i][j] == 0) continue; else { for (t = j + 1; t < 4; t++) { if (t != 0 && a[i][t] == a[i][j]) { prt=1; a[i][j] *= 2; a[i][t] = 0; } else if (t != 0 && a[i][t] != a[i][j]) break; } } } } for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (a[i][j] == 0) { for (t = j + 1; t < 4; t++) { if (a[i][t] != 0) { prt=1; a[i][j] = a[i][t]; a[i][t] = 0; break; } } } } } } void D_() { int i, j, t; for (i = 0; i < 4; i++) { for (j = 3; j >= 0; j--) { if (a[i][j] == 0) continue; else { for (t = j - 1; t >= 0; t--) { if (t != 0 && a[i][t] == a[i][j]) { prt=1; a[i][j] *= 2; a[i][t] = 0; } else if (t != 0 && a[i][t] != a[i][j]) break; } } } } for (i = 0; i < 4; i++) { for (j = 3; j >= 0; j--) { if (a[i][j] == 0) { for (t = j - 1; t >= 0; t--) { if (a[i][t] != 0) { prt=1; a[i][j] = a[i][t]; a[i][t] = 0; break; } } } } } } void S_() { int i, j, t; for (i = 0; i < 4; i++) { for (j = 3; j >= 0; j--) { if (a[j][i] == 0) continue; else { for (t = j - 1; t >= 0; t--) { if (t != 0 && a[j][i] == a[t][i]) { prt=1; a[j][i] *= 2; a[t][i] = 0; } else if (t != 0 && a[t][i] != a[j][i]) break; } } } } for (i = 0; i < 4; i++) { for (j = 3; j >= 0; j--) { if (a[j][i] == 0) { for (t = j - 1; t >= 0; t--) { if (a[t][i] != 0) { prt=1; a[j][i] = a[t][i]; a[t][i] = 0; break; } } } } } } void W_() { int i, j, t; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (a[j][i] == 0) continue; else { for (t = j + 1; t < 4; t++) { if (t != 0 && a[j][i] == a[t][i]) { prt=1; a[j][i] *= 2; a[t][i] = 0; } else if (t != 0 && a[t][i] != a[j][i]) break; } } } } for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (a[j][i] == 0) { for (t = j + 1; t < 4; t++) { if (a[t][i] != 0) { prt=1; a[j][i] = a[t][i]; a[t][i] = 0; break; } } } } } } int main() { int judge; char ch; srand((unsigned) time(NULL)); printf("\n\n\n\n\n\n\t\t\tW S A D Control direction\n\n\n\n\t\t\tPress any key to start the game...\n"); getch(); frmt(); crt(); put(); for (;1;) { prt = 0; for (;1;) { printf("\n\n\n\n\n\n\n\nPlease enter: "); ch = getch(); putchar(ch); if(ch >= 'a' && ch <= 'z') ch -= 32; if(ch == 'W' || ch == 'A' || ch == 'D' || ch == 'S') break; } if (ch == 'A') A_(); if (ch == 'D') D_(); if (ch == 'S') S_(); if (ch == 'W') W_(); if (scan()) { if (prt) crt(); put(); } else { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\tGame Over\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); break; } } getchar(); }
PS:
印象中好像是十月一刚开学时,非常无聊写的,当作是锻炼代码能力了,也是图个好玩,成就感也是有的,毕竟是第一个有点可玩性的游戏,有时候真的停不下来233。
来源:https://www.cnblogs.com/ACMFish/p/6403480.html