我是一名初学者程序员,我目前正在学习C ++作为我的第一语言。我一直在尝试制作一个简单的游戏,但它只是不起作用。当我开始时,它不会给我一个错误它没有做它应该做的事情。(使用Code :: Blocks)我也知道很多未使用的类,函数,变量等。
代码:
#include <iostream>
#include <conio.h>
using namespace std;
int main();
//variables
const int X = 0;
const int Y = 10;
const int Z = 0;
int X1 = X;
int Y1 = Y;
int Z1 = Z;
int Input;
int Input2;
int Menu()
{
cout<<"Please type what you want to do."<<endl;
cin>>Input;
return Input;
//deciding what they want to do.
}
int Calculating()
{
//Calculating Function(Unused)
}
class Player
{
public:
void Movement()
{
//the loop that activates when the Input == 1
while(1)
{
cout<<"2 - Move"<<endl;
do
{
cin>>Input2;
}while(Input2 < 3 && Input2 > 1);
switch(Input2)
{
case 1:
cout<<"You moved"<<endl;
}
}
}
void Attack()
{
//the loop that activates when the Input == 2
while(1)
{
cout<<"1 - Sword Dance"<<endl;
do
{
cin>>Input2;
}while(Input2 < 3 && Input2 > 1);
switch(Input2)
{
case 1:
cout<<"DMG"<<endl;
break;
}
}
}
};
class Enemy
{
//Enemy class(Unused)
};
int main()
{
Player Pl;
//if the chosen number was 1 it will make the Player move.
//if the chosen number was 2 it will make the Player attack.
if(Input == 1)
{
Pl.Movement();
}
else if(Input == 2)
{
Pl.Attack();
}
Menu();
//calling the Menu function
return 0;
}
答案 0 :(得分:1)
在您的主页中,您必须将Menu()
称为:
int main()
{
Player Pl;
Input=Menu();
//if the chosen number was 1 it will make the Player move.
//if the chosen number was 2 it will make the Player attack.
if(Input == 1)
{
Pl.Movement();
}
else if(Input == 2)
{
Pl.Attack();
}
Menu();
//calling the Menu function
return 0;
}
答案 1 :(得分:1)
据我所知,您将Input up设置为全局变量,但从未初始化它。
所以你的程序流程是:
我的建议是在创建P1后立即调用Menu()。