制造一个匀速运动的物体

本文关键字:运动 一个 制造 | 更新日期: 2023-09-27 18:16:33

我正在使用计时器来使一个对象以恒定的速度移动。

下面是我的代码:
  Class class1 = new Class();
    public int x;
    public int y;
    public Form1()
    {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e)
    {    
        Graphics g = e.Graphics;
        class1.Draw(g);
    }
 private void timer1_Tick(object sender, EventArgs e)
    {
        x += 1;
        class1.Move(x/2, x/2);
        Invalidate();
    }
类:

  class Class
{
    private int x;
    private int y;
    public void Draw(Graphics g)
    {
        SolidBrush Brush = new SolidBrush(Color.White);
        g.FillRectangle(Brush, x, y, 10, 10);
    }
    public void Move(int X, int Y)
    {
        x = x + X/3;
        y = y + Y/3;
    }
}

正方形在加速,有什么办法让它匀速行进吗?

制造一个匀速运动的物体

每次增加"x",然后将其用作移动的增量。注释:

 // x += 1;
 class1.Move(x/2, x/2);

你还需要为this指定一个默认的"x"

相关文章: