控制台游戏无法正常工作

时间:2015-07-17 21:57:03

标签: c++

我正在为学习目的制作一款简单的游戏,最近我遇到了这个问题。请记住,我仍然是一个伟大的初学者。当我从菜单进入游戏并在"命令行"我立即饿死并脱水。我已经能够连接互联网几天了,而且我已经阅读了整个程序,但我找不到任何错误。

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 Roadtrip\n";
    std::cout << "Type \"/help\" for help\n";
    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 stats\n";
            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;
}

**编辑:**图片:http://i.imgur.com/yu1V1pq.png(需要10个回复才能发布图片) 这真的很奇怪。我进入/步骤它工作,然后我进入/运行它也工作。我不明白......

4 个答案:

答案 0 :(得分:3)

您的一些if语句执行赋值而不是比较

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

您可能需要将=更改为==

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

答案 1 :(得分:1)

由于

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

b没有带你回到命令行只是等待一个字符被读取然后它返回。如果你想回去,你应该按照你的方式来做。例如,退出播放将返回到菜单循环,显然使用menuLoop = 1,因此它将退出整个程序但是通过修改,这不是一个糟糕的循环系统。

编辑:我已经在&#34;命令行&#34;中看到了你的意思。

答案 2 :(得分:1)

像其他人所说的那样,你有很多条件意外拼写为作业。

事实上,b()函数正在吃后续命令。

也许你应该

  1. 使用std::getline()一次读取一行命令
  2. std::cin.ignore()内使用b()实际消费直至行尾
  3. PS。由于使用全局变量,我很难验证游戏循环逻辑。我只知道在/step之后/step被忽略而没有效果。将输入与循环控件分开,并尝试删除全局变量。

答案 3 :(得分:0)

INFO 不必每次都写std::cout,而只需在开头写using namespace std;,就不必写std::cout,而只需写cout << "" ;