0

I am trying to make a tower defense game using winforms in which i draw a map of squares. I want to be able to select a square using the arrowkeys so i can build a tower on the selected square. However i'm not sure what to use to achieve this, if anyone has any tips on how to get started on it i would appreciate that.

    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;
        }
    }
    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);
            }
        }

    }
  • 1
    Once drawn the squares are just pixels. You can 1) take the mouse position and get a List that contain the position from you list of allSquares. (Ractangle.Contains(Point) ) and the mark one as selected and draw it is a different color or pen style.. – TaW Nov 28 '19 at 12:07
  • [How can I treat the circle as a control after drawing it? - Moving and selecting shapes](https://stackoverflow.com/a/38347945/3110834) – Reza Aghaei Nov 28 '19 at 20:40
  • [How to drag and move shapes in C#](https://stackoverflow.com/a/38749134/3110834)? – Reza Aghaei Nov 28 '19 at 20:41
  • You can also try using some control as basis, for example Label, and using its appearance properties and Paint event to draw and style it the way you like. – ymdred16 Nov 29 '19 at 06:44

0 Answers0