VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码)

VisualStudio2010 C#怎么做迷宫啊(纯小白求全过程和代码),第1张

你好,

软糖花时间使用C#控制台做了个演示程序,见下图:

迷宫这个程序还是有些复杂的,初学者可能有点难懂。不懂再问吧。

一、定义结构 -->点,表示坐标X和Y

public struct 点 {

    public 点(int vX, int vY) { X = vX Y = vY }

    public int X { get set }

    public int Y { get set }

}

二、定义抽象迷宫类,提供一个二维数组

public abstract class 迷宫 {

    internal int[,] _map

    internal 点 _起始点

    internal int _高

    internal int _宽

    public int[,] map {

        get {

            return _map

        }

    }

    public 点 originPos {

        get {

            return _起始点

        }

    }

    public int 高 {

        get {

            return _高

        }

    }

    public int 宽 {

        get {

            return _宽

        }

    }

    public abstract void 生成迷宫(int width, int height, 点 originPos)

}

三、定义阻挡墙和阻挡方向

[Flags]

public enum 阻挡方向 {

    无 = 0,

    上 = 1,

    下 = 2,

    左 = 4,

    右 = 8,

}

public class 迷宫墙 {

    public 迷宫墙(int x, int y, 阻挡方向 方向) {

        this.x = x

        this.y = y

        this.阻挡方向 = 方向

    }

    public int x

    public int y

    public 阻挡方向 阻挡方向

}

四、迷宫程序 -->输出到控制台

using System

namespace 迷宫生成 {

    class Program {

        static void Main(string[] args) {

            var maze = new 普利姆迷宫()

            Console.BackgroundColor = ConsoleColor.White

            Console.ForegroundColor = ConsoleColor.Black

            Console.WriteLine("【生成迷宫 21 X 21 】")

            Console.ReadKey()

            //重新开始生成迷宫

            重新生成:

            Console.CursorVisible = false

            Console.CursorLeft = 0

            Console.CursorTop = 1

            int startX = 9

            int startY = 9

            int 宽 = 21

            int 高 = 21

            Console.SetWindowSize(60, 30)

            Console.SetWindowPosition(0, 0)

            maze.生成迷宫(宽, 高, new 点(startX, startY))

            for (int y = 0 y < maze.高 y++) {

                for (int x = 0 x < maze.宽 x++) {

                    var k = maze.map[y, x]

                    if (k == 0) {

                        //绘制墙

                        Console.BackgroundColor = ConsoleColor.Gray

                        Console.Write("  ")

                    } else {

                        //绘制通路

                        Console.BackgroundColor = ConsoleColor.DarkCyan

                        Console.Write("  ")

                    }

                }

                Console.WriteLine()

            }

            //退出

            Console.BackgroundColor = ConsoleColor.Black

            Console.WriteLine()

            Console.BackgroundColor = ConsoleColor.Black

            Console.ForegroundColor = ConsoleColor.White

            Console.WriteLine("按空格键重新生成迷宫,其他任意键退出...")

            //绘制开始点

            Console.CursorLeft = startX

            Console.CursorTop = startY + 1

            Console.BackgroundColor = ConsoleColor.DarkGray

            Console.ForegroundColor = ConsoleColor.Yellow

            Console.Write("始")

            var j = Console.ReadKey()

            if (j.Key == ConsoleKey.Spacebar) {

                goto 重新生成

            }

        }

    }

}

具体有什么不懂的再问把,代码拿走~~慢慢研究把。

记得点赞、采纳、好评哟~~

c#界面绘制的时候,底层重绘每次会清除画布背景,然后再全部重新绘制,这才是导致闪烁最主要的原因。于是重载消息发送函数 *** 作,禁掉这条消息。代码如下:

protected override void WndProc(ref Message m)

{

if (m.Msg == 0x0014) // 禁掉清除背景消息

return

base.WndProc(ref m)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11922742.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存