0

我正在开发一款类似 Arena 的主机游戏。我想要它,这样我就可以让你与某些怪物战斗,而不是每次都必须与怪物战斗。这是我的代码:

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;


char select;
int randnum;
int level=1;
int maxhp=10;
int hp=maxhp;
int mhp;
int exp;
int dmg;
int m_maxhp;
int req_exp=10;
int day;
char boot;
int a;

ifstream leveli;
ofstream levelo;

ifstream playerhpi;
ofstream playerhpo;

ifstream expi;
ofstream expo;

ifstream maxhpi;
ofstream maxhpo;

ifstream req_expi;
ofstream req_expo;

ifstream dayi;
ofstream dayo;

void wait( time_t delay )
{
time_t timer0, timer1;
time( &timer0 );
do {
time( &timer1 );
} while (( timer1 - timer0 ) < delay );
}


// This is the void that's not being called
void bat()
{

    m_maxhp=10;
    mhp=m_maxhp;
    do{
    dmg= rand() % 4 + 1;
    cout << "Batt attacks you for " << dmg;
    hp=hp-dmg;
    cout << ", leaving you with [" << hp << "/" << maxhp << "]" << endl;
    wait(1);

    if(hp<=0)
    {
        cout << "You've lose. Try harder next time.";
        exp=exp/2;
        day=day+1;

        dayo.open("daynumber.dat");
        dayo << day;
        dayo.close();
    }

    else if(mhp<=0)
    {
        cout << "You have killed the Bat. You gain 6 EXP." << endl;
        exp=exp+6;
        day=day+1;

        dayo.open("daynumber.dat");
        dayo << day;
        dayo.close();

        expo.open("experience.dat");
        expo << exp;
        expo.close();
    }





        }while(mhp>0);
    }



int main()
{

leveli.open("level.dat");
leveli >> level;
leveli.close();

playerhpi.open("health.dat");
playerhpi >> hp;
playerhpi.close();

expi.open("experience.dat");
expi >> exp;
expi.close();

maxhpi.open("maxhealth.dat");
maxhpi >> maxhp;
maxhpi.close();

req_expi.open("req_exp.dat");
req_expi >> req_exp;
req_expi.close();


cout << "Data successfully loaded. Start the game now? y/n" << endl;
boot=_getch();

if(boot=='n')
{

    cout << "Exiting..." << endl;
    wait(3);
    exit(0);
}

else if(boot=='y'){
do{
if (exp==req_exp)
{
    level=level+1;
    cout << "Level Up! You are now level " << level << "!" << endl;

    exp=0;
    req_exp=req_exp*1.5;

    req_expo.open("req_exp.dat");
    req_expo << req_exp;
    req_expo.close();

    levelo.open("level.dat");
    levelo << level;
    levelo.close();
}


else{
cout << endl << "Day " << day << " in The Arena." << endl << "1. Fight" << endl << "2. Stats" << endl << "3. Full Heal" << endl << "4. Half Heal" << endl;
select=_getch();


    if(select=='1')
    {
            srand((unsigned)time(0));
            randnum = rand() % 500 + 1;

         //   cout << "*DEBUG* " << randnum << endl;

           // This part doesn't work
            if(randnum<300 && level<4)
            {
                cout << "You've been chosen to fight a Bat!" << endl;
                wait(1.5);
                void bat();

}
            else
            {

            }
    }


    else if(select=='2')
    {
        cout << endl << "Health: [" << hp << "/" << maxhp << "]" << endl << "Level: [" << level << "]" << endl << "Experience: " << "[" << exp << "/" << req_exp << "]" << endl;
        wait(0.5);
    }

    else
    {
        cout << "Invalid Command";
    }

}

}while (boot=='y');



}

return 0;
}

我是否错误地使用了空隙?如果是这样,有人可以指出我应该改变什么吗?

4

2 回答 2

2
if(randnum<300 && level<4)
{
    cout << "You've been chosen to fight a Bat!" << endl;
    wait(1.5);
    void bat(); // ?
}

这里的预期行为是什么?void bat();是一个函数声明。这只是介绍了名称bat,没有做任何其他事情。它不调用名为bat. 如果你只需要打电话bat,你应该写:

bat(); // a call to "bat"
于 2012-05-12T14:24:13.443 回答
0

当您调用该函数时,您省略了返回类型,例如您的代码:

       // This part doesn't work
        if(randnum<300 && level<4)
        {
            cout << "You've been chosen to fight a Bat!" << endl;
            wait(1.5);
            void bat();

...

应该读:

       // This part doesn't work
        if(randnum<300 && level<4)
        {
            cout << "You've been chosen to fight a Bat!" << endl;
            wait(1.5);
            bat();

...

于 2012-05-12T14:22:50.627 回答