C#中的Rouglike地图生成 [英] Rouglike map generation in C#

查看:83
本文介绍了C#中的Rouglike地图生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助这个代码让角色吃掉墙壁。我需要有关如何解决这个问题的建议和想法,顺便说一下我正在尝试做RPG游戏。



提前谢谢

NB我还处于早期游戏开发阶段(第一场比赛)。



这是代码:

对于地图

I need Help with this code the character eats the walls. I need suggestions and ideas on how I can fix this problem, btw I''m trying to do an rpg game.

Thank you in advance
N.B I''m still in my very early game dev stages(first game).

This is the code:
for the map

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Map_v1._0
{
    class Map
    {

        public int XMax = 100;
        public int YMax = 57;
        public char[,] MapArray = new char[100, 57];
        public int XExit = 5;
        public int YExit = 5;
        public int XEntrance = -1;
        public int YEntrance = -1;
        public int[] MonsterArray = new int[50];
        public Random random = new Random(DateTime.Now.Millisecond);
        public int AmountOfCaves = 17;
        public int CaveCount = 0;
        public int DeathCount = 0;


        public void Generate_Run(Map map, Navigation player1)
        {
            // Clear rooms
            map.Generator_Clear(map, player1);

            // Generate Maze
            map.Generator_Maze(map, player1);
        }

        // map Generation
        public void Generator_Clear(Map map, Navigation player1)
        {
            for (int y = 0; y < YMax; y++)
            {
                for (int x = 0; x < XMax; x++)
                {
                    // Create dirt level clear it.
                    map.MapArray[x, y] = '█';

                }
            }
        }

        public void Generator_Maze(Map map, Navigation player1)
        {
            int AmountOfCaves = 17;
            int CaveCount = 0;
            int DeathCount = 0;

            while (CaveCount < AmountOfCaves)
            {
                // Increment 1 cave
                CaveCount++;

                // Create a random varible
                Random randy = new Random(DateTime.Now.Millisecond);

                // Random a start location
                int x = randy.Next(0, map.XMax);
                int y = randy.Next(0, map.YMax);
                bool Dead = false;

                // Set the direction
                int Direction = 1;
                int count = 0;

                // Move a random direction, if Direction isn't 0 continue
                while (count < 1000)
                {
                    // Clear the space
                    if (y > 1 && y < map.YMax - 1 && Dead == false)
                        map.MapArray[x, y] = ' ';

                    // Set the exit
                    map.MapArray[map.XExit, map.YExit] = 'E';

                    // Bools
                    bool Up = false;
                    bool Right = false;
                    bool Down = false;
                    bool Left = false;
                    bool UUp = false;
                    bool RRight = false;
                    bool DDown = false;
                    bool LLeft = false;
                    bool Moved = false;
                    char LastMove = ' ';
                    Dead = false;

                    // Check spaces
                    // Move only selected direction

                    while (Moved == false)
                    {
                        // Check spaces
                        // If we're within bounds
                        if ((y - 1 > 1 && map.MapArray[x, y - 1] == '█') || LastMove == 'D')
                        {
                            Up = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 1 < map.XMax - 1 && map.MapArray[x + 1, y] == '█') || LastMove == 'L')
                        {
                            Right = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 1 < map.YMax - 1 && map.MapArray[x, y + 1] == '█') || LastMove == 'U')
                        {
                            Down = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 1 > 1 && map.MapArray[x - 1, y] == '█') || LastMove == 'R')
                        {
                            Left = true;
                        }
                        // 1 across
                        // If we're within bounds
                        if ((y - 2 > 1 && map.MapArray[x, y - 2] == '█') || LastMove == 'D')
                        {
                            UUp = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 2 < map.XMax - 1 && map.MapArray[x + 2, y] == '█') || LastMove == 'L')
                        {
                            RRight = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 2 < map.YMax - 1 && map.MapArray[x, y + 2] == '█') || LastMove == 'U')
                        {
                            DDown = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 2 > 1 && map.MapArray[x - 2, y] == '█') || LastMove == 'R')
                        {
                            LLeft = true;
                        }


                        // Set the direction for this loop
                        Direction = randy.Next(1, 5);

                        // Move accordingly
                        switch (Direction)
                        {
                            case 0:
                                break;
                            // Up
                            case 1:
                                // If we're within bounds
                                if (UUp == true)
                                {
                                    y--;
                                    Moved = true;
                                    LastMove = 'U';
                                }
                                break;
                            // Right
                            case 2:
                                // If we're within bounds
                                if (RRight == true)
                                {
                                    x++;
                                    Moved = true;
                                    LastMove = 'R';
                                }
                                break;
                            // Down
                            case 3:
                                // If we're within bounds
                                if (DDown == true)
                                {
                                    y++;
                                    Moved = true;
                                    LastMove = 'D';
                                }
                                break;
                            // Left
                            case 4:
                                // If we're within bounds
                                if (LLeft == true)
                                {
                                    x--;
                                    Moved = true;
                                    LastMove = 'L';
                                }
                                break;
                        }

                        if ((Up == false && Right == false && Down == false && Left == false) ||
                            (UUp == false && RRight == false && DDown == false && LLeft == false))
                        {
                            Moved = true;
                            map.MapArray[x, y] = ' ';
                            x = randy.Next(0, map.XMax);
                            y = randy.Next(0, map.YMax);
                            Dead = true;
                            DeathCount++;
                        }
                    }

                    count++;

                }

            }

            while (CaveCount < AmountOfCaves)
            {
                // Increment 1 cave
                CaveCount++;

                // Create a random varible
                Random randy = new Random(DateTime.Now.Millisecond);

                // Random a start location
                int x = randy.Next(0, map.XMax);
                int y = randy.Next(0, map.YMax);
                bool Dead = false;

                // Set the direction
                int Direction = 1;
                int count = 0;

                // Move a random direction, if Direction isn't 0 continue
                while (count < 1000)
                {
                    // Clear the space
                    if (y > 1 && y < map.YMax - 1 && Dead == false)
                        map.MapArray[x, y] = ' ';

                    // Set the exit
                    map.MapArray[map.XExit, map.YExit] = 'E';

                    // Bools
                    bool Up = false;
                    bool Right = false;
                    bool Down = false;
                    bool Left = false;
                    bool UUp = false;
                    bool RRight = false;
                    bool DDown = false;
                    bool LLeft = false;
                    bool Moved = false;
                    char LastMove = ' ';
                    Dead = false;

                    // Check spaces
                    // Move only selected direction

                    while (Moved == false)
                    {

                        // Check spaces
                        // If we're within bounds
                        if ((y - 1 > 1 && map.MapArray[x, y - 1] == '█') || LastMove == 'D')
                        {
                            Up = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 1 < map.XMax - 1 && map.MapArray[x + 1, y] == '█') || LastMove == 'L')
                        {
                            Right = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 1 < map.YMax - 1 && map.MapArray[x, y + 1] == '█') || LastMove == 'U')
                        {
                            Down = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 1 > 1 && map.MapArray[x - 1, y] == '█') || LastMove == 'R')
                        {
                            Left = true;
                        }
                        // 1 across
                        // If we're within bounds
                        if ((y - 2 > 1 && map.MapArray[x, y - 2] == '█') || LastMove == 'D')
                        {
                            UUp = true;
                        }
                        // Right
                        // If we're within bounds
                        if ((x + 2 < map.XMax - 1 && map.MapArray[x + 2, y] == '█') || LastMove == 'L')
                        {
                            RRight = true;
                        }
                        // Down
                        // If we're within bounds
                        if ((y + 2 < map.YMax - 1 && map.MapArray[x, y + 2] == '█') || LastMove == 'U')
                        {
                            DDown = true;
                        }
                        // Left
                        // If we're within bounds
                        if ((x - 2 > 1 && map.MapArray[x - 2, y] == '█') || LastMove == 'R')
                        {
                            LLeft = true;
                        }

                    }

                    // Set the direction for this loop
                    Direction = randy.Next(1, 5);

                    // Move accordingly
                    switch (Direction)
                    {
                        // Up
                        case 1:
                            // If we're within bounds
                            if (UUp == true)
                            {
                                y--;                                
                                Moved = true;
                                LastMove = 'U';
                            }
                            break;
                        // Right
                        case 2:
                            // If we're within bounds
                            if (RRight == true)
                            {
                                x++;
                                Moved = true;
                                LastMove = 'R';
                            }
                            break;
                        // Down
                        case 3:
                            // If we're within bounds
                            if (DDown == true)
                            {
                                y++;
                                Moved = true;
                                LastMove = 'D';
                            }
                            break;
                        // Left
                        case 4:
                            // If we're within bounds
                            if (LLeft == true)
                            {
                                x--;
                                Moved = true;
                                LastMove = 'L';
                            }
                            break;
                    }


                    if ((Up == false && Right == false && Down == false && Left == false) ||
    (UUp == false && RRight == false && DDown == false && LLeft == false))
                    {
                        Moved = true;
                        map.MapArray[x, y] = ' ';
                        x = randy.Next(0, map.XMax);
                        y = randy.Next(0, map.YMax);
                        Dead = true;
                        DeathCount++;
                    }


                }
                count++;

            }



        }



        // Display map
        public void Generator_Display(Map map, Navigation player1)
        {

            for (int y = 0; y < map.YMax; y++)
            {
                for (int x = 0; x < map.XMax; x++)
                {
                    if (map.MapArray[x, y] == '@')
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write(map.MapArray[x, y]);
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else
                        Console.Write(map.MapArray[x, y]);

                }
                Console.WriteLine(" ");
            }
        }
    }
}



The main program, which uses character movement by using cursor controls.






The main program, which uses character movement by using cursor controls.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Map_v1._0
{
    class Program
    {

        public static Navigation Player { get; set; } // Player string " " that will be moved around


        static void Main(string[] args)
            {
                

                LoadGame();
                
                ConsoleKeyInfo keyInfo;
                while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
                {
                    switch (keyInfo.Key)
                    {
                        case ConsoleKey.UpArrow:
                            MoveHero(0, -1);
                            break;

                        case ConsoleKey.RightArrow:
                            MoveHero(1, 0);
                            break;

                        case ConsoleKey.DownArrow:
                            MoveHero(0, 1);
                            break;

                        case ConsoleKey.LeftArrow:
                            MoveHero(-1, 0);
                            break;   
                        case ConsoleKey.Enter:
                            int MapLevels = 5;
                            Map[] map = new Map[MapLevels];

                            Map currentmap = new Map();

                           currentmap.Generate_Run(currentmap, Player);
                           currentmap.Generator_Display(currentmap, Player);
                            break;
                        
                    }
                }
            }

            /// <summary>
            /// Paint the new hero
            /// </summary>
            static void MoveHero(int x, int y)
            {
                Navigation newHero = new Navigation()
                {
                    Up = Player.Up + x,
                    Down = Player.Down + y
                };

                if (CanMove(newHero))
                {
                    RemoveHero();

                    //Console.BackgroundColor = My_Player;
                    Console.SetCursorPosition(newHero.Up, newHero.Down);
                    Console.Write("i");

                    Player = newHero;
                }
            }

            /// <summary>
            /// Overpaint the old hero
            /// </summary>
            static void RemoveHero()
            {
                //Console.BackgroundColor = BACKGROUND_COLOR;
                Console.SetCursorPosition(Player.Up, Player.Down);
                Console.Write(" ");                
                
            }
            

            /// <summary>
            /// Make sure that the new coordinate is not placed outside the
            /// console window (since that will cause a runtime crash
            /// </summary>
            static bool CanMove(Navigation c)
            {
                if (c.Up < 0 || c.Up >= Console.WindowWidth)
                    return false;

                if (c.Down < 0 || c.Down >= Console.WindowHeight)
                    return false;

                return true;
            }

            /// <summary>
            /// Paint a background color
            /// </summary>
            /// <remarks>
            /// It is very important that you run the Clear() method after
            /// changing the background color since this causes a repaint of the background
            /// </remarks>
            static void SetBackgroundColor()
            {
               // Console.BackgroundColor = BACKGROUND_COLOR;
                Console.Clear(); //Important!
            }

            
            static void LoadGame()
            {
                SetBackgroundColor();

                Player = new Navigation()
                {
                    Up = 0,
                    Down = 0
                };

                MoveHero(0, 0);

            }
        }
    }





The Navigation class





The Navigation class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Map_v1._0
{    

    class Navigation
        {
            
            public int Up { get; set; } //Left x+ 1 will move player to the right & x-1 will move the obj left
            public int Down { get; set; } //Top and bottom axis y+1 up and y- 1 bottom
        }
}

推荐答案

There seem to be a few things that are a bit weird with that code, but I’’m going to focus on what causes the Hero to eat the walls.



In your method static bool CanMove(Navigation c), the only constraints applied are checking if you Hero moves off the sceren, you also need to check if they’’ve hit a wall. Changing it to something like this might do the trick for you;

There seem to be a few things that are a bit weird with that code, but I''m going to focus on what causes the Hero to eat the walls.

In your method static bool CanMove(Navigation c), the only constraints applied are checking if you Hero moves off the sceren, you also need to check if they''ve hit a wall. Changing it to something like this might do the trick for you;
static bool CanMove(Navigation c, Map map)
{
    if (c.Up < 0 || c.Up >= Console.WindowWidth)
        return false;

    if (c.Down < 0 || c.Down >= Console.WindowHeight)
        return false;

    // Check if the map is clear of everything (this needs to be refined to cater for exists and such)
    if (map.MapArray[c.Up, c.Down] != ' ')
        return false;

    return true;
}





This also means that you can’’t have your Hero starting at (0, 0) as that is a wall, I’’ve therefore also amended the LoadGame to cater for this, below is my rewritten version of your Program class;





This also means that you can''t have your Hero starting at (0, 0) as that is a wall, I''ve therefore also amended the LoadGame to cater for this, below is my rewritten version of your Program class;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Map_v1._0
{
    class Program
    {

        public static Navigation Player { get; set; } // Player string " " that will be moved around


        static void Main(string[] args)
        {



            int MapLevels = 5;
            Map[] map = new Map[MapLevels];

            Map currentmap = new Map();

            currentmap.Generate_Run(currentmap, Player);
            currentmap.Generator_Display(currentmap, Player);

            LoadGame(currentmap);

            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        MoveHero(0, -1, currentmap);
                        break;

                    case ConsoleKey.RightArrow:
                        MoveHero(1, 0, currentmap);
                        break;

                    case ConsoleKey.DownArrow:
                        MoveHero(0, 1, currentmap);
                        break;

                    case ConsoleKey.LeftArrow:
                        MoveHero(-1, 0, currentmap);
                        break;
                }
            }
        }

        /// <summary>
        /// Paint the new hero
        /// </summary>
        static void MoveHero(int x, int y, Map map)
        {
            Navigation newHero = new Navigation()
            {
                Up = Player.Up + x,
                Down = Player.Down + y
            };

            if (CanMove(newHero, map))
            {
                RemoveHero(map);

                //Console.BackgroundColor = My_Player;
                Console.SetCursorPosition(newHero.Up, newHero.Down);
                Console.Write("i");

                Player = newHero;
            }
        }

        /// <summary>
        /// Overpaint the old hero
        /// </summary>
        static void RemoveHero(Map map)
        {
            //Console.BackgroundColor = BACKGROUND_COLOR;
            Console.SetCursorPosition(Player.Up, Player.Down);

            Console.Write(map.MapArray[Player.Up, Player.Down]);
        }


        /// <summary>
        /// Make sure that the new coordinate is not placed outside the
        /// console window (since that will cause a runtime crash
        /// </summary>
        static bool CanMove(Navigation c, Map map)
        {
            if (c.Up < 0 || c.Up >= Console.WindowWidth)
                return false;

            if (c.Down < 0 || c.Down >= Console.WindowHeight)
                return false;

            if (map.MapArray[c.Up, c.Down] == 'E')
                return true;

            // See if map is clear
            if (map.MapArray[c.Up, c.Down] != ' ')
                return false;

            return true;
        }

        /// <summary>
        /// Paint a background color
        /// </summary>
        /// <remarks>
        /// It is very important that you run the Clear() method after
        /// changing the background color since this causes a repaint of the background
        /// </remarks>
        static void SetBackgroundColor()
        {
            // Console.BackgroundColor = BACKGROUND_COLOR;
            Console.Clear(); //Important!
        }


        static void LoadGame(Map map)
        {
            //SetBackgroundColor();

            Player = new Navigation()
            {
                Up = 0,
                Down = 0
            };


            for (int r = 0; r < 10; ++r)
            {
                for (int c = 0; c < 10; ++c)
                {
                    if (map.MapArray[c, r] == ' ')
                        MoveHero(c, r, map);
                }
            }

        }
    }
}







I know game programming can be a daunting experience, but keep at it as it is very rewarding when you make something that works :)



Hope this helps,

Fredrik




I know game programming can be a daunting experience, but keep at it as it is very rewarding when you make something that works :)

Hope this helps,
Fredrik


这篇关于C#中的Rouglike地图生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆