none
Keep enemy position in console game RRS feed

  • Question

  • I'm developing a small console game, similar to "a dark room". and I have just created a way to spawn enemies, problem is I cant figure out how to make them stay.

    Here's the code

    using System;
    
    namespace Field_of_Beasts
    {
        class MainClass
        {
            public static Random rndm = new Random();
    
            public static float posY = vertical;
            public static float posX = horizontal;
    
            public static int vertical = 10;
            public static int horizontal = 20;
    
            public static void Main(string[] args)
            {
                Console.CursorVisible = false;
                Map();
                EnemySpawn();
                Move();
            }
    
    
    
    
    
            public static void Map()
            {
                
    
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,M,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                Console.WriteLine(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
            }
    
            // Method For Movement
            public static void Move()
            {
                bool running = true;
    
    
                ConsoleKeyInfo keyInfo;
                do
                {
                    keyInfo = Console.ReadKey(true);
                    Console.Clear();
                    Map();
    
                    switch (keyInfo.Key)
                    {
                        //Movement to the right
                        case ConsoleKey.RightArrow:
                            horizontal++;
                            Console.SetCursorPosition(horizontal, vertical);
                            if (horizontal >= 39)
                            {
                                horizontal = 39;
                            }
    
                            posX = horizontal / 40;
                            Console.Write("@");
                            break;
    
                        //Movement to the left
                        case ConsoleKey.LeftArrow:
                            horizontal--;
                            Console.SetCursorPosition(horizontal, vertical);
                            if (horizontal <= 1)
                            {
                                horizontal = 1;
                            }
    
                            posX = horizontal / 40;
                            Console.Write("@");
                            break;
    
                        //Movement up
                        case ConsoleKey.UpArrow:
                            vertical--;
                            Console.SetCursorPosition(horizontal, vertical);
                            if (vertical <= 1)
                            {
                                vertical = 1;
                            }
    
                            posY = vertical / 20;
                            Console.Write("@");
                            break;
    
                        //Movement down
                        case ConsoleKey.DownArrow:
                            vertical++;
                            Console.SetCursorPosition(horizontal, vertical);
                            if (vertical >= 19)
                            {
                                vertical--;
                            }
    
                            posY = vertical / 20;
                            Console.Write("@");
                            break;
                    }
                    EnemySpawn();
                } while (running == true);
    
            }
            public static void EnemySpawn()
            {
                int ePosX = rndm.Next(1, 40);
                int ePosY = rndm.Next(1, 20);
                int spawnChanse = rndm.Next(1, 5);
    
                if (spawnChanse == 1)
                {
                    Console.SetCursorPosition(ePosX, ePosY);
                    Console.WriteLine("#");
    
                }
            }
        }
    }
    

    I didn't know what code would be important to the answer, so i posted it all. Thanks on beforehand

    Sunday, August 12, 2018 8:39 AM

All replies

  • Well, you've posted C# code to the VB.Net forum so I'll need to move this to the C# forum.

    Ultimately though, you'll need to keep track of the enemies and their positions.  Consider maintaining a List of Points that hold the current position of an enemy when you spawn or move one.  When the player moves you can then check to see if the new destination is in the list of enemy locations before overwriting the enemy character.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, August 12, 2018 11:55 AM
    Moderator
  • Hi DiabolicalGremlin,

    Thank you for posting here.

    For your question, I test your code. But I am not sure what is the things you want to make them stay. Could you provide details about the results you want. And what is the question when you run your application?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 13, 2018 5:43 AM
    Moderator