21xrx.com
2025-03-28 02:38:04 Friday
文章检索 我的文章 写文章
C++小游戏代码大全
2023-07-12 12:23:26 深夜i     11     0
C++ 小游戏 代码 大全

C++作为一种高级编程语言,在游戏开发中表现得非常出色。C++可以轻松地与OpenGL或DirectX等图形引擎集成,创建令人惊叹的游戏效果。在这里,我们将为你展示一些C++小游戏的代码,包括推箱子、贪吃蛇、俄罗斯方块等。

推箱子游戏是很多人小时候玩过的经典游戏之一。以下是C++实现推箱子游戏的代码:

#include <iostream>
using namespace std;
const int N = 20;
int m, n, ax, ay, bx, by;
char a[N][N];
void dfs(int x, int y, int bx, int by, int step) {
  if(step > 10) return;
  if(x == bx && y == by) return;
  if(a[bx][by] == 'E')
    cout << "You win!" << endl;
    return;
  
  if(x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '#' || (x == bx && y == by)) return;
  a[x][y] = '#';
  dfs(x - 1, y, bx - 1, by, step + 1);
  dfs(x + 1, y, bx + 1, by, step + 1);
  dfs(x, y - 1, bx, by - 1, step + 1);
  dfs(x, y + 1, bx, by + 1, step + 1);
  a[x][y] = '.';
}
int main() {
  cin >> n >> m;
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cin >> a[i][j];
      if(a[i][j] == 'S')
        ax = i;
        ay = j;
      
      if(a[i][j] == 'E')
        bx = i;
        by = j;
      
    }
  }
  dfs(ax, ay, bx, by, 0);
  return 0;
}

接下来是贪吃蛇游戏,贪吃蛇游戏是一款非常经典的游戏。以下是用C++实现贪吃蛇游戏的代码:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100], nTail;
enum eDirecton STOP = 0;
eDirecton dir;
void Setup()
{
  gameOver = false;
  dir = STOP;
  x = width / 2;
  y = height / 2;
  fruitX = rand() % width;
  fruitY = rand() % height;
  score = 0;
}
void Draw()
{
  system("cls");
  for (int i = 0; i < width+2; i++)
    cout << "#";
  cout << endl;
  for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
      if (j == 0)
        cout << "#";
      if (i == y && j == x)
        cout << "O";
      else if (i == fruitY && j == fruitX)
        cout << "F";
      else {
        bool print = false;
        for (int k = 0; k < nTail; k++) {
          if (tailX[k] == j && tailY[k] == i)
            cout << "o";
            print = true;
          
        }
        if (!print)
          cout << " ";
      }
      if (j == width - 1)
        cout << "#";
    }
    cout << endl;
  }
  for (int i = 0; i < width+2; i++)
    cout << "#";
  cout << endl;
  cout << "Score:" << score << endl;
}
void Input()
{
  if (_kbhit()) {
    switch (_getch())
    case 'a':
      dir = LEFT;
      break;
    case 'd':
      dir = RIGHT;
      break;
    case 'w':
      dir = UP;
      break;
    case 's':
      dir = DOWN;
      break;
    case 'x':
      gameOver = true;
      break;
    
  }
}
void Logic()
{
  int prevX = tailX[0];
  int prevY = tailY[0];
  int prev2X, prev2Y;
  tailX[0] = x;
  tailY[0] = y;
  for (int i = 1; i < nTail; i++) {
    prev2X = tailX[i];
    prev2Y = tailY[i];
    tailX[i] = prevX;
    tailY[i] = prevY;
    prevX = prev2X;
    prevY = prev2Y;
  }
  switch (dir) {
  case LEFT:
    x--;
    break;
  case RIGHT:
    x++;
    break;
  case UP:
    y--;
    break;
  case DOWN:
    y++;
    break;
  default:
    break;
  }
  if (x > width || x < 0 || y > height || y < 0)
    gameOver = true;
  for (int i = 0; i < nTail; i++)
    if (tailX[i] == x && tailY[i] == y)
      gameOver = true;
  if (x == fruitX && y == fruitY) {
    score += 10;
    fruitX = rand() % width;
    fruitY = rand() % height;
    nTail++;
  }
}
int main()
{
  Setup();
  while (!gameOver) {
    Draw();
    Input();
    Logic();
    Sleep(50);
  }
  return 0;
}

最后是俄罗斯方块游戏,这是一款大家都熟知的游戏。以下是用C++实现俄罗斯方块游戏的代码:

#include <iostream>
#include <time.h>
#include <Windows.h>
using namespace std;
bool gameOver; // 游戏是否结束
const int width = 12; // 游戏界面宽度
const int height = 20; // 游戏界面高度
int score; // 分数
int x, y; // 方块位置
int map[height][width] = {0}; // 地图
int nextBlock[4][4] = { // 下一个方块
   0,
   0,
   0,
  0,
};
int block[4][4] = {0}; // 当前方块
int types[7][4][4] = { // 方块类型
  {
     0,
     1,
    0,
    0,
  },
  {
    0,
     0,
     0,
    0,
  },
  {
     0,
     1,
     1,
     0,
  },
  {
     0,
     1,
    1,
     0,
  },
  {
     0,
     1,
     0,
     0,
  },
  {
    0,
     0,
    1,
     0,
  },
  {
     0,
     0,
    1,
    0,
  }
};
void init() // 初始化函数
{
  srand(time(NULL));
  x = width / 2 - 2;
  y = 0;
  int type = rand() % 7;
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      block[i][j] = types[type][i][j];
    }
  }
}
void draw() // 绘制函数
{
  cout << "分数:" << score << endl;
  for(int i = 0; i < height; ++i) {
    for(int j = 0; j < width; ++j) {
      if(i == y && j == x) cout << "■";
      else if(map[i][j]) cout << "□";
      else cout << " ";
    }
    cout << endl;
  }
}
bool check(int nx, int ny) // 是否可以移动函数
{
  if(nx < 0 || nx + 4 > width || ny + 4 > height)
    return false;
  
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      if(block[i][j] && map[ny + i][nx + j])
        return false;
      
    }
  }
  return true;
}
bool move(int nx, int ny) // 移动函数
{
  if(check(nx, ny))
    x = nx;
    y = ny;
    return true;
  
  else
    return false;
  
}
void rotate() // 旋转方块函数
{
  int temp[4][4];
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      temp[i][j] = block[i][j];
    }
  }
  for(int i = 0; i < 4; ++i) {
    for(int j = 0; j < 4; ++j) {
      block[i][j] = temp[3 - j][i];
    }
  }
}
bool update() // 更新函数
{
  if(move(x, y+1))
    return true;
  
  else {
    for(int i = 0; i < 4; ++i) {
      for(int j = 0; j < 4; ++j) {
        if(block[i][j]) {
          map[y+i][x+j] = 1;
        }
      }
    }
    int fullLine = 0;
    for(int i = 0; i < height; ++i) {
      bool isFull = true;
      for(int j = 0; j < width; ++j) {
        if(!map[i][j])
          isFull = false;
        
      }
      if(isFull) {
        fullLine++;
        for(int k = i; k > 0; --k) {
          for(int j = 0; j < width; ++j) {
            map[k][j] = map[k-1][j];
          }
        }
      }
    }
    score += fullLine * 10;
    int type = rand() % 7;
    for(int i = 0; i < 4; ++i) {
      for(int j = 0; j < 4; ++j) {
        block[i][j] = types[type][i][j];
      }
    }
    x = width / 2 - 2;
    y = 0;
    if(!check(x, y)) {
      return false;
    }
    else {
      return true;
    }
  }
}
int main() // 主函数
{
  init();
  while(!gameOver) {
    draw();
    if(update()) {
      Sleep(500);
    }
    else {
      cout << "GameOver!" << endl;
      gameOver = true;
    }
    if(kbhit()) { // 读取键盘输入进行移动方块
      char ch = getch();
      switch(ch) {
        case 'a': move(x-1, y); break;
        case 'd': move(x+1, y); break;
        case 'w': rotate(); break;
        case 's': move(x, y+1); break;
      }
    }
  }
  return 0;
}

好了,以上C++小游戏的代码就为大家呈现完毕了。以上代码提供了小游戏的实现方法,读者可以根据自己的兴趣进行修改和调整,实现自己的小游戏。希望本篇文章对大家有所帮助,谢谢阅读!

  
  

评论区

请求出错了