首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我试图使我的库存系统跨功能在我的文字冒险游戏。我是

我试图使我的库存系统跨功能在我的文字冒险游戏。我是
EN

Stack Overflow用户
提问于 2020-02-27 15:44:15
回答 1查看 206关注 0票数 0

我正在使用C#。

我得到的错误是:

名称“库存”在当前上下文中不存在。

我是编程方面的新手,并且正在为我在大学里的最后一个项目创建这个游戏。这是在几个星期后,所以我不着急,但我喜欢编码,所以我想知道如何改变这一点。我的教授说我需要分块做我的代码,而不是用一种流畅的方式.这使得局部变量有问题,所以我需要某种类型的全局变量。我想要任何帮助和建议。

代码语言:javascript
复制
using System;
using System.ComponentModel.Design;

namespace newGameFinal
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inventory = new string[20];
            intro();
            gamebegin();
            gamebegin2();
        }
        public static void invalid()
        {
            Console.WriteLine("Invalid Input");
        }
        public static void death()
        {
            Console.WriteLine("Would you like to play again? 1 for yes, 2 for no");
            string choice6 = Console.ReadLine();

            if (choice6 == "1")

            {
                Console.Clear();
                intro();


            }

            if (choice6 == "2")

            {

                Environment.Exit(0);

            }
        }

        public static void gamebegin()
        {
            {

                bool roomchoice = true;

                while (roomchoice == true)

                {
                    string room = "0";


                    if (room == "0")

                    {
                        Console.WriteLine("Your goal is to survive in this dungeon....");
                        Console.WriteLine("");
                        Console.WriteLine("which room would you like to visit? (1, 2, 3, 4) 1 = South, 2 = East, 3 = West, 4 = North");
                        room = Console.ReadLine();

                    }

                    if (room == "1")

                    {

                        Console.WriteLine("You went South, you ran right into a wall. Please choose something else. (2, 3, or 4) ");
                        Console.WriteLine("-----------------------------------------------");
                        room = Console.ReadLine();

                    }

                    else if (room == "2")

                    {

                        Console.WriteLine("You went East, you went right into a Goblin... You died ");
                        Console.WriteLine("-----------------------------------------------");
                        death();
                    }

                    else if (room == "3")

                    {

                        Console.WriteLine("You went West, you saw a blinding light that shot a light spear through your chest, and you slowly bleed to death, while agonizing in pain!");
                        Console.WriteLine("");
                        Console.WriteLine("I wonder what did that. Maybe a diety of some kind?");
                        death();
                    }


                    else if (room == "4")

                    {
                        Console.WriteLine("You went North, you walked into a room with many monsters...");
                        Console.WriteLine("");
                        Console.WriteLine("You can bow down to them and beg to not be killed, or quickly look for a weapon. Please type 'beg' or 'look'");
                        gamebegin2();
                    }
                    else
                    {
                        invalid();
                        Console.WriteLine("Plesae choose 1, 2, 3 or 4.");
                        room = Console.ReadLine();

                    }
                }
            }
        }

        //******************
        public static void intro()
        {
            player newplayer = new player();

            Console.WriteLine("What is your name...?");
            Console.WriteLine("");


            newplayer.name = Console.ReadLine();
            Console.Clear();

            Console.WriteLine("Its dark " + newplayer.name + " I cant see anything but a dim light...");
            Console.WriteLine("");

            Console.WriteLine("You have one mission, and one mission alone " + newplayer.name + "...." + " To survive!");
            Console.WriteLine("");


            newplayer.health = 100;

            Console.WriteLine("Your health is " + newplayer.health);
            Console.WriteLine("");


            Console.WriteLine("Would you like to begin?");
            Console.WriteLine("");


            string newgame = "no";
            while (newgame == "no")
            {

                Console.WriteLine("Press 1 for Yes, or 2 for No");
                Console.WriteLine("");



                string choice = Console.ReadLine();

                if (choice == "1")

                {
                    Console.Clear();
                    break;


                }

                if (choice == "2")

                {

                    Console.WriteLine("Okay I will wait till you are");

                    newgame = "no";

                }


                else
                {
                    invalid();
                    newgame = "no";
                }
            }
        }
        public static void gamebegin2()
        {

            string choice = Console.ReadLine().ToUpper();

            if (choice == "BEG")
            {
                Console.WriteLine("They kill you. Should have looked...");
                death();
            }



            else if (choice == "LOOK")

            {


            }


        }
        public static void looking()
        {
            Console.WriteLine("You found a dagger, do you choose to fight, or to run? Please type 'run' or 'fight'");
            string choice = Console.ReadLine().ToUpper();

            if (choice == "RUN")
            {

                inventory[0] = "Dagger";
            }
        }

    }
    class player { public string name { get; set; } public int health { get; set; } }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-27 15:51:22

看到此错误的原因是您的inventory[]Main()方法的本地对象。

您应该将它移到类级别,并使其成为一个模块化变量。

代码语言:javascript
复制
class Program
{
    static string[] inventory = new string[20]; //Move it here and along with anything else you need to access all methods in this class
    static void Main(string[] args)
    {
        intro();
        gamebegin();
        gamebegin2();
    }
  //And the rest of your methods...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60436877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档