21xrx.com
2024-12-22 22:45:04 Sunday
登录
文章检索 我的文章 写文章
C++ 2048游戏代码
2023-07-08 17:05:48 深夜i     --     --
C++ 2048 游戏 代码 实现

C++ 2048游戏代码是一份非常有趣的程序,它可以让你在命令行界面上玩2048游戏。这份代码通过使用C++语言实现了一个简单的2048游戏,让你可以方便地体验到这个游戏的乐趣。

在这个游戏中,你需要使用方向键来控制方块的移动,当两个相同数字的方块相遇时,它们会合并成一个新的方块,数字会翻倍。当你成功获得了2048点以上的分数时,你就算是赢了游戏。

下面是一个简单的C++ 2048游戏代码:


#include<iostream>

#include<ctime>

#include<cstdlib>

#include<conio.h>

#include<Windows.h>

using namespace std;

// 定义游戏面板的大小

const int BOARD_SIZE = 4;

// 定义方向的枚举类型

enum Direction

  UP;

// 游戏面板数据结构

int board[BOARD_SIZE][BOARD_SIZE] = { 0 };

// 初始化游戏,随机生成两个方块

void init() {

  srand(time(NULL));

  int x = rand() % BOARD_SIZE;

  int y = rand() % BOARD_SIZE;

  board[x][y] = 2;

  do {

    x = rand() % BOARD_SIZE;

    y = rand() % BOARD_SIZE;

  } while (board[x][y] != 0);

  board[x][y] = 2;

}

// 在游戏面板上输出数据

void draw_board() {

  system("cls");

  cout << "Score:" << get_score() << endl;

  for (int i = 0; i < BOARD_SIZE; i++) {

    for (int j = 0; j < BOARD_SIZE; j++) {

      cout << "\t" << board[i][j];

    }

    cout << endl;

  }

}

// 获取当前游戏得分

int get_score() {

  int score = 0;

  for (int i = 0; i < BOARD_SIZE; i++) {

    for (int j = 0; j < BOARD_SIZE; j++) {

      score += board[i][j];

    }

  }

  return score;

}

// 判断是否游戏结束

bool is_game_over() {

  for (int i = 0; i < BOARD_SIZE; i++) {

    for (int j = 0; j < BOARD_SIZE; j++) {

      if (board[i][j] == 0)

        return false;

      

      if (i > 0 && board[i-1][j] == board[i][j])

        return false;

      

      if (j > 0 && board[i][j-1] == board[i][j])

        return false;

      

    }

  }

  return true;

}

// 合并相邻的方块

void merge_board(Direction dir) {

  switch (dir) {

    case UP:

      for (int i = 1; i < BOARD_SIZE; i++) {

        for (int j = 0; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i][j] == board[i-1][j]) {

            board[i-1][j] *= 2;

            board[i][j] = 0;

          }

        }

      }

      break;

    case DOWN:

      for (int i = BOARD_SIZE - 2; i >= 0; i--) {

        for (int j = 0; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i][j] == board[i+1][j]) {

            board[i+1][j] *= 2;

            board[i][j] = 0;

          }

        }

      }

      break;

    case LEFT:

      for (int i = 0; i < BOARD_SIZE; i++) {

        for (int j = 1; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i][j] == board[i][j-1]) {

            board[i][j-1] *= 2;

            board[i][j] = 0;

          }

        }

      }

      break;

    case RIGHT:

      for (int i = 0; i < BOARD_SIZE; i++) {

        for (int j = BOARD_SIZE - 2; j >= 0; j--) {

          if (board[i][j] != 0 && board[i][j] == board[i][j+1]) {

            board[i][j+1] *= 2;

            board[i][j] = 0;

          }

        }

      }

      break;

  }

}

// 移动方块

void move_board(Direction dir) {

  bool moved = false;

  switch (dir) {

    case UP:

      for (int i = 1; i < BOARD_SIZE; i++) {

        for (int j = 0; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i-1][j] == 0) {

            board[i-1][j] = board[i][j];

            board[i][j] = 0;

            moved = true;

          }

        }

      }

      break;

    case DOWN:

      for (int i = BOARD_SIZE - 2; i >= 0; i--) {

        for (int j = 0; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i+1][j] == 0) {

            board[i+1][j] = board[i][j];

            board[i][j] = 0;

            moved = true;

          }

        }

      }

      break;

    case LEFT:

      for (int i = 0; i < BOARD_SIZE; i++) {

        for (int j = 1; j < BOARD_SIZE; j++) {

          if (board[i][j] != 0 && board[i][j-1] == 0) {

            board[i][j-1] = board[i][j];

            board[i][j] = 0;

            moved = true;

          }

        }

      }

      break;

    case RIGHT:

      for (int i = 0; i < BOARD_SIZE; i++) {

        for (int j = BOARD_SIZE - 2; j >= 0; j--) {

          if (board[i][j] != 0 && board[i][j+1] == 0) {

            board[i][j+1] = board[i][j];

            board[i][j] = 0;

            moved = true;

          }

        }

      }

      break;

  }

  if (moved) {

    merge_board(dir);

    do {

      int x = rand() % BOARD_SIZE;

      int y = rand() % BOARD_SIZE;

      if (board[x][y] == 0) {

        board[x][y] = 2;

        break;

      }

    } while (true);

  }

}

// 主函数,控制游戏流程

int main() {

  init();

  draw_board();

  while (!is_game_over()) {

    if (kbhit()) {

      char ch = getch();

      if (ch == 27) {

        break;

      }

      if (ch == 'w' || ch == 'W') {

        move_board(UP);

      } else if (ch == 's' || ch == 'S') {

        move_board(DOWN);

      } else if (ch == 'a' || ch == 'A') {

        move_board(LEFT);

      } else if (ch == 'd' || ch == 'D') {

        move_board(RIGHT);

      }

      draw_board();

    }

    Sleep(100);

  }

  cout << "Game over" << endl;

  system("pause");

  return 0;

}

这份代码非常简洁明了,使用的是C++语言的基本语法和数据结构,适合新手学习和开发。如果你对C++语言感兴趣,想要尝试开发类似的小游戏,不妨试试这份C++ 2048游戏代码,相信它会给你带来不少乐趣和启示。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复