主机游戏不能正常工作

Console game not working properly

本文关键字:工作 常工作 游戏 不能 主机      更新时间:2023-10-16

我主要是为了学习而制作一款简单的游戏,最近我遇到了这个问题。记住,我仍然是一个巨大的初学者。当我从菜单进入游戏并在"命令行"中编写任何内容时,我会立即挨饿和脱水。我已经好几天不能上网了,我把整个程序都看了一遍,但没有发现任何问题。

menu.h

#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <dos.h>
#include <windows.h>
#include <WinBase.h>
//-------------//
#include "tutorial.h"
#include "game.h"

void menu() {
    std::cout << "-------MENU-------         n";
    std::cout << " 1.Play                    n";
    std::cout << " 2.Tutorial                n";
    std::cout << " 3.Exit                    n";
    std::cout << "                           n";
    std::cout << "                           n";
    std::cout << "                           n";                     
    std::cout << "Choose Option: ";
    int menuOption;
    std::cin >> menuOption;
    int menuLoop = 0;
    while (menuLoop != 1) {
        if (menuOption == 1) {
            menuLoop = 1;
            play();
        }
        if (menuOption == 2) {
            menuLoop = 1;
            system("CLS");
            tutorial();
        }
        if (menuOption == 3) {
            menuLoop = 1;
            std::cout << "Bye!";
            Sleep(1000);
        }
        if (menuOption > 3)
            std::cout << """ << menuOption << """ << " is not a valid option.n";
    }
}

game.h

#include <iostream>
#include <string>
#include <windows.h>
#include <WinBase.h>
//initiating functions
void step();
void run();
void theme();
void starve();
void die();
void dehydrate();
void b();
//globals
std::string name;
std::string commandLine;
int onRoad = 1; // 1 = True, 0 = False
int steps = 0;
double hunger = 0.0;
double thirst = 0.0;
int energy = 5;
void play() {
    system("CLS");
    std::cout << "Enter your name: n";
    std::cin >> name;
    system("CLS");
    theme();
    Sleep(350);
    std::cout << " " << name << "'s Roadtripn";
    std::cout << "Type "/help" for helpn";
    std::cout << "---------Command Line---------n";
    std::cin >> commandLine;
    while (onRoad != 0){
        //------------------Conditions start------------------
        // Hunger Conditions
        if (hunger = 0){
            if (hunger < 0){
                std::cout << "You can't eat that, you're not hungry.n";
                b();
            }
        }
        if (hunger > 100){
            hunger = 100;
        }
        if (hunger < 0){
            hunger = 0;
        }
        if (hunger = 100){
            starve();
        }
        else if (hunger > 96){
            std::cout << "You're extremely hungry! If you don't eat something quick you're going to die!n";
            b();
        }
        else if (hunger > 90) {
                std::cout << "You're very hungry.n";
                b();
        }
        else if (hunger > 80) {
            std::cout << "You're hungry.n";
            b();
        }
        // Thirst Conditions
        if (thirst = 0){
            if (thirst < 0){
                std::cout << "You can't drink that, you're not thirsty.n";
            }
        }
        if (thirst < 0){  
            thirst = 0;
        }
        if (thirst > 100) {
            thirst = 100;
        }
        if (thirst = 100){
            dehydrate();
        }
        else if (thirst > 90){
            std::cout << "You're extremely thirsty! If you don't drink something quick you're going to die!n";
            b();
        }
        else if (thirst > 75) { 
            std::cout << "You're very thirsty.n";
            b();
        }
        else if (thirst > 50){
            std::cout << "You're thirsty.n";
            b();
        }
        //Energy Conditions
        if (energy > 10){
            energy = 10;
        }
        if (energy < 0){
            energy = 0;
        }


        //-------------------Conditions end-------------------

        if (commandLine == "/commands"){
            std::cout << "-Command-          -Action-n";
            std::cout << " /help              Displays this menu.n";
            std::cout << " /commands          Displays list of commands.n";
            std::cout << " /step              Take a step and display total amount of steps.n";
            std::cout << " /run               Take 5 steps and consume 5 energy.n";
            std::cout << "                     Doesn't increase hunger or thirst.n";
            std::cout << " /inventory         Displays inventory.n";
            std::cout << " /info              Displays stats.n";
            b();
        }
        if (commandLine == "/step") {
            step();
            b();
        }
        if (commandLine == "/info") {
            std::cout << name << "'s statsn";
            std::cout << "Hunger: " << hunger << std::endl;
            std::cout << "Thirst: " << thirst << std::endl;
            std::cout << "Energy: " << energy << std::endl;
            b();
        }
        else {
            std::cout << commandLine << " is not a valid command. Type /commands to display commands.n";
            b();
        }
    }
}
void step(){
    steps += 1;
    std::cout << steps;
    hunger += 5;
    thirst += 5;
}
void run() {
    steps += 5;
    std::cout << steps;
}
void starve(){
    std::cout << "You starved to death!n";
    die();
}
void dehydrate(){
    std::cout << "You dehydrated!n";
    die();
}
void die(){
    std::cout << "Steps taken: " << steps << std::endl;
    onRoad = 0;
}
void theme(){
    Beep(600, 200);
    Beep(500, 200);
    Beep(800, 400);
}
//   b takes you back to the command line
void b(){
    std::cin >> commandLine;
} 

main.cpp

#include <iostream>
#include "menu.h"
#include <WinBase.h>
#include <windows.h>

int main(){
    menu();
    system("PAUSE");
    return 0;
}

**编辑:** Pic: https://i.stack.imgur.com/UrZee.png(需要10个代表发布图片)这真的很奇怪。我输入/step,它工作,然后我输入/run,它也工作。我不明白……

你的一些if语句使用赋值而不是比较

if (hunger = 100){
            starve();
        }

您可能需要将=改为==

在编译时启用警告,如果您还没有这样做。

因为

//   b takes you back to the command line
void b(){
    std::cin >> commandLine;
} 

b不会将您带回到命令行,只是等待读取一个字符,然后返回。如果你想回去,你应该沿着你来的路走。例如,退出播放将返回到菜单循环,显然,使用menuLoop = 1,它将退出整个程序,但通过修改,这不是一个糟糕的循环系统。

编辑:我知道你说的"命令行"是什么意思了。

就像别人说的,你有很多条件不小心拼成了赋值。

实际上,b()函数正在吞食后续命令。

也许你应该

  1. 使用std::getline()一次一行读取命令
  2. 使用std::cin.ignore()b()中实际消耗,直到行结束

p。由于使用全局变量,我很难验证游戏循环逻辑。我只知道/step之后的/step现在被忽略了,没有效果。将您的输入与循环控件分开,并尝试删除全局变量。

INFO而不是每次都写std::cout你可以只写using namespace std;在开头之后你不需要写std::cout只写cout << "" ;