21xrx.com
2025-03-27 17:11:47 Thursday
文章检索 我的文章 写文章
C++小游戏的简单代码
2023-07-01 18:37:41 深夜i     13     0
C++ 小游戏 简单代码 游戏开发 编程语言

C++小游戏是一种基于计算机编程技术开发的简单游戏,通过编写代码实现游戏目标、游戏规则和游戏操作等功能。下面我们将为大家介绍一些基于C++语言编写的小游戏简单代码。

1、猜数字游戏:

该游戏是一个猜数字的游戏,需要玩家通过输入数字来猜测所设定的随机数字。以下是该游戏的简单C++代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
  srand((unsigned)time(0)); // 生成随机数
  int num = rand() % 100 + 1// 生成 1~100 之间的随机数
  int guess = 0, count = 0// 初始化玩家猜测次数
  while (true)
  {
    cout << "请输入猜测的数字(1~100):" << endl;
    cin >> guess;
    count++;
    if (guess < num)
    猜小了!" << endl;
    
    else if (guess > num)
    猜大了!" << endl;
    
    else
    游戏结束
    
  }
  return 0;
}

2、贪吃蛇游戏:

该游戏是一款经典的小游戏,需要玩家通过控制蛇的方向和行动,尽可能多的吃到食物,并防止蛇头碰到游戏边界或蛇身。以下是该游戏的简单C++代码:

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
const int width = 20// 游戏界面宽度
const int height = 20// 游戏界面高度
int x, y, foodx, foody, score; // 初始化游戏变量
int tailx[100], taily[100], ntail; // 初始化蛇身
void setup()
{
  srand(time(0)); // 生成随机数
  x = width / 2// 初始化蛇头位置
  y = height / 2;
  foodx = rand() % width; // 初始化食物位置
  foody = 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 == foody && j == foodx) // 绘制食物
        cout << "*";
      else
      {
        bool print = false;
        for (int k = 0; k < ntail; k++)
        {
          if (tailx[k] == j && taily[k] == i) // 绘制蛇身
          
            cout << "x";
            print = true;
          
        }
        if (!print)
          cout << " ";
      }
      
      if (j == width-1)
        cout << "|";
    }
    cout << endl;
  }
  
  for (int i = 0; i < width+2; i++) // 下边框
    cout << "-";
  cout << endl << endl;
  cout << "得分:" << score << endl; // 输出得分
}
void input()
{
  if (_kbhit()) // 判断键盘输入
  {
    switch (_getch())
    {
      case 'a'// 向左移动
        x--;
        break;
      case 'd'// 向右移动
        x++;
        break;
      case 'w'// 向上移动
        y--;
        break;
      case 's'// 向下移动
        y++;
        break;
      case 'x'// 退出游戏
        exit(0);
    }
  }
}
void logic()
{
  int prevx = tailx[0], 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 (rand() % 3// 随机生成食物
  {
    case 0:
      foodx++;
      break;
    case 1:
      foodx--;
      break;
    case 2:
      foody++;
      break;
    case 3:
      foody--;
      break;
  }
  if (x == foodx && y == foody) // 当蛇头坐标与食物坐标重合时,得分并重新生成食物
  {
    ntail++;
    score += 10;
    foodx = rand() % width;
    foody = rand() % height;
  }
  if (x < 0 || x >= width || y < 0 || y >= height) // 判断游戏边界
  {
    cout << "游戏结束!" << endl;
    exit(0); // 边界碰撞,退出游戏
  }
  for (int i = 0; i < ntail; i++) // 判断蛇头与蛇身碰撞,并退出游戏
  {
    if (tailx[i] == x && taily[i] == y)
    {
      cout << "游戏结束!" << endl;
      exit(0);
    }
  }
}
int main()
{
  setup(); // 初始化游戏
  while (true)
  {
    draw(); // 绘制游戏界面
    input(); // 获取键盘输入
    logic(); // 更新游戏逻辑
  }
  return 0;
}

以上是两个基于C++语言编写的简单小游戏代码,希望能够给大家带来一些参考和启示。同时,我们也鼓励广大开发者朋友们在C++语言的基础上,继续钻研、开发更多有趣、实用的小游戏,为游戏行业的发展贡献自己的力量。

  
  

评论区