如何沿路径设置圆圈动画

时间:2021-01-17 15:04:57

标签: c# windows-forms-designer

我有一个动画,其中一个圆圈跟随一个正方形的边: enter image description here

现在我想让圆沿着多边形的边。有人可以帮我吗?

代码

namespace polygon
{
    public partial class Form1 : Form
    {
        int x, y;
        Pen p1 = new Pen(Color.DarkBlue, 5);
        PointF[] puncte = new PointF[4];

        public Form1()
        {
            InitializeComponent();
            x = 100;
            y = 100;
            
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            puncte[0] = new PointF(100, 100);
            puncte[1] = new PointF(320, 100);
            puncte[2] = new PointF(380, 320);
            puncte[3] = new PointF(100, 320);
            e.Graphics.DrawPolygon(p1, puncte);

            e.Graphics.FillEllipse(Brushes.Red, x, y, 40, 40);
        }

        public void timer1_Tick(object sender, EventArgs e)
        {
            if (x < 300 && y == 80)
                x = x + 20;
            else
              if (y < 300 && x == 300)
                y = y + 20;
            else
          if (y == 300 && x > 80)
                x = x - 20;
            else
                y = y - 20;
            Invalidate();
        }
    }
}

0 个答案:

没有答案