代码400行左右,操作十分流畅,完全由C语言实现!
B站同名UP主 需要源码可以加我Q 207732272 (手机右上角的小加号->添加好友,不是搜索!)
int main() {
int score = new2048();
printf("游戏结束,最终分数 %d.\n", score);
return 0;
}
int new2048(){
system("cls");
printf("操作按键:请用上下左右方向键来操作\n");
int board[SIZE][SIZE] = {{0}};
int c, score, moveScore, gameWon;
initBoard(board);
score = 0;
gameWon = 0;
while (canMove(board) == 1) {
printf("\n");
printBoard(board);
printf("\n当前分数:%d.\n\n", score);
if (gameWon == 0 && boardContains2048(board)) {
gameWon = 1;
printf("\n恭喜你胜利了!\n");
}
c = getKey();
moveScore = 0;
if (c == 3) {
moveScore = moveLeft(board);
} else if (c == 2) {
moveScore = moveDown(board);
} else if (c == 1) {
moveScore = moveUp(board);
} else if (c == 4) {
moveScore = moveRight(board);
}
if (moveScore != -1){
insertNewNumber(board);
score = score + moveScore;
}
system("cls");
}
printBoard(board);
return score;
}
也可以自己选择难度
|