C# 使绘制的正方形可选

C# 使绘制的正方形可选,c#,winforms,C#,Winforms,我正在尝试使用winforms制作一个塔防游戏,在这个游戏中我绘制了一张正方形的地图。我希望能够使用箭头键选择一个正方形,以便在选定的正方形上建造一座塔。然而,我不知道用什么来实现这一点,如果有人对如何开始它有任何提示,我将不胜感激 class Square { int xPos; int yPos; int Width; int Height; string Type; public

我正在尝试使用winforms制作一个塔防游戏,在这个游戏中我绘制了一张正方形的地图。我希望能够使用箭头键选择一个正方形,以便在选定的正方形上建造一座塔。然而,我不知道用什么来实现这一点,如果有人对如何开始它有任何提示,我将不胜感激

    class Square
    {
        int xPos;
        int yPos;
        int Width;
        int Height;
        string Type;
        public Square(int x, int y, int w, int h)
        {
            xPos = x;
            yPos = y;
            Width = w;
            Height = h;
        }
        public void DrawSquare(Graphics g)
        {
            Pen p = new Pen(Color.Red);
            Rectangle rect = new Rectangle(xPos + 50, yPos + 50, Width, Height);
            g.DrawRectangle(p, rect);
            if (Type == "Path")
            {
                SolidBrush b = new SolidBrush(Color.Brown);
                g.FillRectangle(b, rect);
                b.Dispose();
            }
            else if (Type == "Spawn")
            {
                SolidBrush b = new SolidBrush(Color.Green);
                g.FillRectangle(b, rect);
                b.Dispose();
            }
            else if (Type == "Goal")
            {
                SolidBrush b = new SolidBrush(Color.Purple);
                g.FillRectangle(b, rect);
                b.Dispose();
            }
            else
            {
                SolidBrush lb = new SolidBrush(Color.LightBlue);
                g.FillRectangle(lb, rect);
                lb.Dispose();
            }
            p.Dispose();
        }

        public void SetType(string type)
        {
            this.Type = type;
        }
    }
类映射
{
列表映射=新列表();
int[]路径=新int[78]{ 0, 1, 21, 22, 42, 62, 82, 83, 84, 64, 44, 24, 25, 26, 46, 66, 86, 106, 126, 125, 124, 123, 122, 121, 141, 161, 181, 182, 183, 184, 185, 165, 166, 167, 168, 148, 128, 108, 88, 68, 48, 28, 29, 30, 50, 70, 90, 91, 92, 112, 132, 131, 130, 150, 170, 171, 172, 173, 153, 154, 134, 114, 94, 74, 54, 34, 35, 36, 56, 57, 77, 97, 117, 137, 157, 177, 178, 179 };
公共无效生成器映射()
{
对于(int y=0;y<10;y++)
{
对于(int x=0;x<20;x++)
{
正方形=新正方形(x*50+1*x,y*50+1*y,50,50);
地图。添加(方形);
}
}
for(int i=0;i
一旦画出正方形,它们就是像素。你可以1)选择鼠标位置,从所有正方形列表中获得一个包含该位置的列表。(Ractangle.Contains(Point))将一个标记为选中并绘制为不同的颜色或笔样式..?您也可以尝试使用一些控件作为基础,例如标签,并使用其外观属性和绘制事件以您喜欢的方式绘制和设置样式。
    class Map
    {
        List<Square> map = new List<Square>();
        int[] path = new int[78] { 0, 1, 21, 22, 42, 62, 82, 83, 84, 64, 44, 24, 25, 26, 46, 66, 86, 106, 126, 125, 124, 123, 122, 121, 141, 161, 181, 182, 183, 184, 185, 165, 166, 167, 168, 148, 128, 108, 88, 68, 48, 28, 29, 30, 50, 70, 90, 91, 92, 112, 132, 131, 130, 150, 170, 171, 172, 173, 153, 154, 134, 114, 94, 74, 54, 34, 35, 36, 56, 57, 77, 97, 117, 137, 157, 177, 178, 179 };

        public void GenerateMap()
        {
            for (int y = 0; y < 10; y++)
            {
                for (int x = 0; x < 20; x++)
                {
                    Square square = new Square(x * 50 + 1 * x, y * 50 + 1 * y, 50, 50);
                    map.Add(square);
                }
            }
            for (int i = 0; i < path.Length; i++)
            {
                map[path[i]].SetType("Path");

            }
            map[path[0]].SetType("Spawn");
            map[path[path.Length - 1]].SetType("Goal");
        }

        public void DrawMap(Graphics e)
        {
            foreach (var sq in map)
            {
                sq.DrawSquare(e);
            }
        }

    }