好的,所以我正在制作这个文字冒险游戏。为了完全独特和不同,我决定添加一个影响系统,你对其他角色的反应会影响他们对你的反应以及他们的战斗力。 我在C ++方面有相当多的经验,我刚刚在C开始了我的第二个学期的课程。到目前为止,我尝试过使用全局变量,结构,静态变量以及在其他函数中使用函数。我也尝试过使用指针,但每次都不断出错,所以我就停止了。这是一段试图使用影响系统的代码(对不起星星,不想泄露任何故事情节):
#include <stdlib.h>
#include <stdio.h>
static int positive_Influence; int negative_Influence; int overall_Influence;
void printpInI(int positive_Influence, int negative_Influence);//positive and negative influence
int choice_6()
{
int choice = 0;
int positive_Influence, negative_Influence, overall_Influence;
positive_Influence = 0;
negative_Influence = 0;
overall_Influence = 0;
printf("What do you do?\n");
printf("\t1. You: ****\n");
printf("\t2. You: ****\n");
printf("\t3. You: ****\n");
do
{
scanf_s("%i", &choice);
switch (choice)
{
case 1:
{
printf("\t****?\n");
system("pause");
negative_Influence += 10;
printf("You lose influence and are now at %i with ****.\n", positive_Influence-negative_Influence);
break;
}
case 2:
{
printf("\t****");
system("pause");
positive_Influence += 10;
printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
break;
}
case 3:
{
printf("**** smiles at this.\n");
system("pause");
positive_Influence += 10;
printf("You gain influence and are now at %i influence with ****.\n", positive_Influence-negative_Influence);
break;
}
}
}while(choice != 1 && choice != 2 && choice != 3);
overall_Influence = positive_Influence-negative_Influence;
printf("%i\n", overall_Influence);
}
void story_7()
{
printf("Your overall influence is %i\n", overall_Influence);
}
int main()
{
choice_6();
story_7();
system("pause");
}
答案 0 :(得分:3)
您已将overall_influence
声明为全局,但在choice_6
中声明为本地。本地宣言优先;只是删除它,你应该没问题。变量positive_influence
和negative_influence
也是如此。
答案 1 :(得分:1)
你能告诉我你遇到了哪种类型的错误?你也已经声明了用于计算影响的全局变量,所以为什么你再次在choice_6
函数中本地声明它,这是你程序中的错误情况,所以局部变量比它们声明的函数中的全局变量更优先。因此,从函数choice_6
中删除声明。
答案 2 :(得分:0)
我实际上只是真的很愚蠢,因为我忘记了指针的工作原因(大坝&符号!)无论如何谢谢大家的回复我真的非常感激。我也在制作一个可以使用多个敌人的战斗系统,所以继续关注我的个人资料,因为我以后可能会有一些问题。如果你想进行beta测试,我的电子邮件是mikemanahan15@gmail.com,如果你想玩我到目前为止所做的。当然,这里是完成的代码(对于choice_6和story_7)享受:
#include <stdlib.h>
#include <stdio.h>
int choice_6(int *influence)
{
int choice = 0;
printf("What do you do?\n");
printf("\t1. You: No. I know absolutely nothing about you. You could be a monster\n");
printf("\ttoo for all I know! Normal people don't turn into frogs!\n");
printf("\t2. You: Your right we should keep moving. You can tell me when your\n");
printf("\tready.\n");
printf("\t3. You: Okay where do you think should we go then?\n");
do
{
scanf_s("%i", &choice);
switch (choice)
{
case 1:
{
printf("\tBrie flashes a pained look on her face and you immediately regret saying that.\n");
printf("\tBrie: You really think I'm a monster?\n");
system("pause");
*influence -= 10;
printf("You lose influence and are now at %i with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: Your right we should keep moving.\n");
break;
}
case 2:
{
printf("\tBrie: Thank you. I'd rather not discuss this my life story in a dark\n");
printf("\tdungeon.\n");
system("pause");
*influence += 10;
printf("You gain influence and are now at %i influence with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their combat effectiveness, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: I'd have to agree with you there. Let's see what is up these stairs.\n");
choice = 2;
break;
}
case 3:
{
printf("Brie smiles at this.\n");
printf("\tBrie: Well the only way out seems to be these stairs so let's go up.\n");
system("pause");
*influence += 10;
printf("You gain influence and are now at %i influence with Brie.\n", *influence);
system("pause");
printf("Influence affects characters reactions towards you, their effectiveness in combat, and even their allegiances in rare cases.\n");
printf("However, depending on the situation, low influence is not always a bad thing...\n");
system("pause");
printf("\tYou: Sounds good to me I am quite frankly done with dungeons about now.\n");
break;
}
default:
{
printf("Type the number for the choice you want to do\n");
system("pause");
break;
}
}
}while(choice != 1 && choice != 2 && choice != 3);
}
int story_7(int influence)
{
if (influence > 0)
{
printf("Brie laughs at this and slowly leans you off her to have both of you crouch.");
system("pause");
}
else
{
printf("Brie: Ugh just get off of me!\n");
printf("Brie pushes you off her violently and you manage to stay crouched.");
system("pause");
}
}
int main()
{
int influence = 0;
// ...
choice_6(&influence);
story_7(influence);
// ...
}
答案 3 :(得分:0)
您应该删除局部变量并使用范围解析运算符来使用全局变量 这可以解决你的问题。
您也可以在以后想要的任何功能中使用这些变量。