如何将局部变量传递给另一个函数?

用户名

所以我正在做一个文字冒险游戏。它有数百条故事线,一个功能齐全的战斗系统,我现在正在尝试创建一个影响系统。基本上应该按以下方式工作:某些响应/动作将增加或减小您对不同角色的影响。我想在choice_6()和story_7()中使用影响变量。我怎么做?请不要给我发送任何链接。我经历了许多其他答案,它们对我来说没有任何意义,因此,如果您要复制和粘贴,请至少与其他答案以不同的方式进行解释。谢谢。

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");
}
格雷格·休吉尔

如果要在函数中进行修改,可以将指针传递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那里修改值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将变量传递给与另一个控制器共享的局部变量?

来自分类Dev

将局部变量从函数传递给另一个 - 自动化无聊的练习

来自分类Dev

如何将另一个函数作为变量传递给R中的函数?

来自分类Dev

如何根据也传递给它的另一个变量的值将特定的字符串传递给我的局部变量?

来自分类Dev

在C中如何将局部函数变量值传递给另一个函数并打印值而不会出现错误

来自分类Dev

如何将$(this)变量传递给另一个函数javascript

来自分类Dev

Angular 2 firebase 如何将变量传递给另一个函数?

来自分类Dev

如何将字符串变量从一个函数传递给另一个函数?

来自分类Dev

如何将变量值从一个函数传递给另一个函数

来自分类Dev

如何将变量从一个函数传递给另一个函数

来自分类Dev

如何将变量从一个函数传递给另一个函数?

来自分类Dev

将一个函数中的局部变量返回给另一个函数

来自分类Dev

如何将局部变量传递给 lambda 函数?

来自分类Dev

Ajax:如何将一个函数的变量传递给另一个jQuery

来自分类Dev

如何将一个函数的值传递给另一个函数?

来自分类Dev

另一个函数中的局部变量如何影响主函数中的变量?

来自分类Dev

如何将函数和参数的变量列表传递给C中的另一个函数?

来自分类Dev

将局部变量传递到另一个视图的JS-Rails

来自分类Dev

如何将Js函数数组传递给另一个Js函数?

来自分类Dev

如何将函数输出传递给另一个函数

来自分类Dev

如何将void函数传递给另一个void函数?

来自分类Dev

如何将tanh函数作为参数传递给另一个函数?

来自分类Dev

如何将函数作为参数传递给另一个函数?

来自分类Dev

如何将泛型函数作为参数传递给另一个函数?

来自分类Dev

如何将命名参数传递给另一个函数需要的函数?

来自分类Dev

如何将参数传递给作为另一个函数参数的函数

来自分类Dev

如何在C中的另一个函数中使用局部变量的值

来自分类Dev

如何在python中将局部变量用于另一个函数?

来自分类Dev

如何将变量传递给另一个场景中的脚本

Related 相关文章

  1. 1

    如何将变量传递给与另一个控制器共享的局部变量?

  2. 2

    将局部变量从函数传递给另一个 - 自动化无聊的练习

  3. 3

    如何将另一个函数作为变量传递给R中的函数?

  4. 4

    如何根据也传递给它的另一个变量的值将特定的字符串传递给我的局部变量?

  5. 5

    在C中如何将局部函数变量值传递给另一个函数并打印值而不会出现错误

  6. 6

    如何将$(this)变量传递给另一个函数javascript

  7. 7

    Angular 2 firebase 如何将变量传递给另一个函数?

  8. 8

    如何将字符串变量从一个函数传递给另一个函数?

  9. 9

    如何将变量值从一个函数传递给另一个函数

  10. 10

    如何将变量从一个函数传递给另一个函数

  11. 11

    如何将变量从一个函数传递给另一个函数?

  12. 12

    将一个函数中的局部变量返回给另一个函数

  13. 13

    如何将局部变量传递给 lambda 函数?

  14. 14

    Ajax:如何将一个函数的变量传递给另一个jQuery

  15. 15

    如何将一个函数的值传递给另一个函数?

  16. 16

    另一个函数中的局部变量如何影响主函数中的变量?

  17. 17

    如何将函数和参数的变量列表传递给C中的另一个函数?

  18. 18

    将局部变量传递到另一个视图的JS-Rails

  19. 19

    如何将Js函数数组传递给另一个Js函数?

  20. 20

    如何将函数输出传递给另一个函数

  21. 21

    如何将void函数传递给另一个void函数?

  22. 22

    如何将tanh函数作为参数传递给另一个函数?

  23. 23

    如何将函数作为参数传递给另一个函数?

  24. 24

    如何将泛型函数作为参数传递给另一个函数?

  25. 25

    如何将命名参数传递给另一个函数需要的函数?

  26. 26

    如何将参数传递给作为另一个函数参数的函数

  27. 27

    如何在C中的另一个函数中使用局部变量的值

  28. 28

    如何在python中将局部变量用于另一个函数?

  29. 29

    如何将变量传递给另一个场景中的脚本

热门标签

归档