以下是用C++语言实现的简单真心话大冒险游戏代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(0));  // 用于生成随机数的种子
    
    string players[4] = {'玩家1', '玩家2', '玩家3', '玩家4'};
    string truths[4] = {'真心话1', '真心话2', '真心话3', '真心话4'};
    string dares[4] = {'大冒险1', '大冒险2', '大冒险3', '大冒险4'};
    
    int playerIndex = rand() % 4;
    int choice;
    
    cout << '欢迎来到真心话大冒险游戏!' << endl;
    cout << '按下任意键开始游戏...' << endl;
    cin.get();
    
    while (true) {
        cout << players[playerIndex] << ',轮到你了!' << endl;
        cout << '请选择:1. 真心话 2. 大冒险' << endl;
        cin >> choice;
        
        if (choice == 1) {
            cout << '真心话: ' << truths[playerIndex] << endl;
        } else if (choice == 2) {
            cout << '大冒险: ' << dares[playerIndex] << endl;
        } else {
            cout << '无效选择,请重新选择!' << endl;
            continue;
        }
        
        cout << '按下任意键继续游戏,按q退出游戏...' << endl;
        cin.get();  // 清空输入缓冲区
        cin.get();
        
        if (cin.peek() == 'q') {
            break;
        }
        
        playerIndex = (playerIndex + 1) % 4;
    }
    
    cout << '游戏结束!谢谢参与!' << endl;
    
    return 0;
}

这个游戏有4个玩家,每个玩家依次选择'真心话'或'大冒险'。根据玩家的选择,程序会随机选择对应的真心话或大冒险进行展示。玩家可以按下任意键继续游戏,按下q退出游戏。游戏会循环进行,直到玩家选择退出。

代码说明:

  • srand(time(0)) 用于生成随机数种子,确保每次运行游戏时随机数序列不同。
  • players、truths 和 dares 数组分别存储玩家姓名、真心话和挑战内容。
  • playerIndex 用于记录当前轮到哪个玩家。
  • choice 用于存储玩家选择的选项 (1: 真心话,2: 大冒险)。
  • 游戏循环使用 while (true) 实现,直到玩家按下 'q' 退出。
  • cin.get() 用于清空输入缓冲区。
  • cin.peek() 用于查看下一个输入字符,判断玩家是否按下了 'q'。

你可以根据需要修改玩家数量、真心话和挑战内容,以及游戏逻辑。希望这份代码对你有所帮助!


原文地址: https://www.cveoy.top/t/topic/pOKl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录