0

I set the points and when the points of the same color form a square, I draw a polygon. But when a new square is formed, the old one disappears. can you tell me how to make sure that when drawing a new polygon, the old one does not disappear? in the checkpoint() function, I check whether there is a square of points of the same color and return e coordinates for drawing.

first result

second

function CheckPoint()

  public partial class Form1 : Form
    {
        private Class1 Class1 = new Class1();
        private CellState currentPlayer = CellState.Red;
        public const int SIZE = 11;
        public const int Icon_Size = 30;
        public Form1()
        {
            InitializeComponent();
        }
        //ставит точки
        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            var p = new Point((int)Math.Round(1f * e.X / Icon_Size), (int)Math.Round(1f * e.Y / Icon_Size));
            if (Class1[p] == CellState.Empty)
            {
                Class1.SetPoint(p, currentPlayer);
                currentPlayer = Class1.Inverse(currentPlayer);
                Invalidate();
            }
        }
        //рисуем
        private void OnPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.ScaleTransform(Icon_Size, Icon_Size);
            //рисуем сеточку
            using (var pen = new Pen(Color.Gainsboro, 0.1f))
            {
                for (int x = 1; x < SIZE; x++)
                    e.Graphics.DrawLine(pen, x, 1, x, SIZE - 1);
                for (int y = 1; y < SIZE; y++)
                    e.Graphics.DrawLine(pen, 1, y, SIZE - 1, y);
            }
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //рисуем точки
            using (var brush = new SolidBrush(Color.White))
                for (int x = 1; x < Form1.SIZE; x++)
                    for (int y = 1; y < Form1.SIZE; y++)
                    {
                        var p = new Point(x, y);
                        var cell = Class1[p];
                        if (cell != CellState.Empty)
                        {
                            brush.Color = StateToColor(cell);
                            e.Graphics.FillEllipse(brush, x - 0.2f, y - 0.2f, 0.4f, 0.4f);

                        }
                    }


            using (var PenP = new Pen(Color.Black, 0.1f))
            using (var brush = new SolidBrush(Color.White))
            {
                Class1.CheckPoint();
                int i = Class1.CheckPoint()[0];
                int j = Class1.CheckPoint()[1];
                int cp = Class1.CheckPoint()[2];

                if (cp == 1)
                {
                    PenP.Color = Color.Red;
                    brush.Color = Color.IndianRed;
                    Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
                    e.Graphics.FillPolygon(brush, a);
                    e.Graphics.DrawPolygon(PenP, a);

                }   
                if (cp == 2)
                {
                    PenP.Color = Color.Blue;
                    brush.Color = Color.RoyalBlue;
                    Point[] a = { new Point(i, j), new Point(i + 1, j), new Point(i + 1, j + 1), new Point(i, j + 1) };
                    e.Graphics.FillPolygon(brush, a);
                    e.Graphics.DrawPolygon(PenP, a);
                }     
            }
            

        }
        
        

        //условие смены цвета под ход игрока
        Color StateToColor(CellState state, byte alpha = 255)
        {
            var res = state == CellState.Blue ? Color.Blue : Color.Red;
            return Color.FromArgb(alpha, res);
        }

    }
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
DrGlide
  • 9
  • 3
  • 1
    Don't post pictures of code. Are you clearing the canvas? We don't see enough code. – LarsTech Dec 16 '21 at 00:21
  • What triggers this code? are you hooking on the `.Paint` event? – John Alexiou Dec 16 '21 at 00:22
  • Essentially, you aren't persisting your drawing. In a paint event, you have to draw everything. Your code is only drawing the value of `cp`, which is one or two. – LarsTech Dec 16 '21 at 00:58
  • 2
    You need a list of things to draw. [Discussion](https://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887#28716887) – TaW Dec 16 '21 at 01:06

0 Answers0