所以我正在做一个文字冒险游戏。它有数百行故事,一个功能齐全的战斗系统,我现在正试图创建一个影响系统。基本上它应该像这样工作:某些响应/动作会增加或减少你对不同角色的影响。我想在choice_6()和story_7()中使用influence变量。我怎么做?请不要发给我任何链接。我已经经历了很多其他答案,但它们对我来说没有意义,所以如果你要复制和粘贴,至少要解释一下与其他答案不同的方式。谢谢。
int choice_6()
{
int influence = 0;
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()
{
printf("\ninfluence is %i\n", influence);
printf("You lead the way walking upstairs\n");
system("pause");
printf("You turn the corner while crouched to find a room full of gremlins and goblins.\n");
system("pause");
printf("You grab Brie's hand and roll to the left behind some crates before they see you.\n");
system("pause");
printf("Even though you realize you will probably not make it out of this situation\n");
printf("alive, you can't help but feel lucky with Brie being so tightly pressed against you on the floor.\n");
system("pause");
printf("After a long period of silence to confirm nobody has seen you both enter,\n");
printf("Brie looks up at you and rolls her eyes.\n");
system("pause");
printf("*whispers*\tBrie: At least buy me dinner first jeez.\n");
system("pause");
printf("*whispers*\tYou: I'd love to but we should probably kill these uglies first.\n");
system("pause");
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 play = 0;
int influence = 0;
intro_1();
story_1();
choice_1();
story_2();
choice_2();
story_3();
choice_3();
story_4();
choice_4();
story_5();
choice_5();
intro_2();
combat_1();
story_6();
choice_6();
story_7();
choice_7();
system("pause");
}
答案 0 :(得分:3)
如果要从函数中修改它,可以将指针传递给influence
变量。像这样:
int choice_6(int *influence)
{
// ...
*influence -= 10;
// use *influence wherever you would have had used influence before
// ...
}
int story_7(int influence)
{
// ...
if (influence > 0) ...
// ...
}
int main()
{
int influence = 0;
// ...
choice_6(&influence);
story_7(influence);
// ...
}
这是将指针传递给choice_6()
,因为您需要在该函数中修改influence
。这是将值传递给story_7()
,因为您不需要修改那里的influence
值。
答案 1 :(得分:1)
你应该创建存储游戏状态的游戏对象,在这种情况下,影响力和其他事件标志。
然后,您需要将此游戏对象设置为全局,或者通过指针/引用将其传递给可能需要检查或修改游戏状态的每个函数。
struct MyGame {
int influence;
int has_cheese;
};
void choice_6(struct MyGame * game) {
...
game->influence += 10;
...
}
int main(...) {
struct MyGame game;
game->influence = 0;
game->has_cheese = FALSE; // or 0
...
choice_6(&game);
....
}
答案 2 :(得分:0)
您需要通过引用传递influence
变量。这意味着您提供引用的函数可以更改变量本身的值(而不仅仅是变量的副本)。
通过在函数签名中添加*
参数,并使用&
前置传递来执行此操作:
int choice_6(int *influence)
{
....
int main()
{
int play = 0;
int influence = 0;
choice_6(&influence);
....