C# 慢慢地使速度表平静下来

C# 慢慢地使速度表平静下来,c#,timer,keyevent,C#,Timer,Keyevent,我在做汽车驾驶模拟器。想法很简单:此时你只需按下空格键,当按下空格键时,速度会不断增长。释放空格键后,速度应缓慢下降,但在正确的时刻下降。 那么,有没有一种方法可以让我的speedbar以一种平稳的方式缓慢下降呢 下面是gif演示: 我甚至试着用KeyUp来做,但它正好在合适的时候起作用。我也试过,但故事是一样的 这是我的密码: float angle = -35.0f; Image img; public MPh() { Initia

我在做汽车驾驶模拟器。想法很简单:此时你只需按下空格键,当按下空格键时,速度会不断增长。释放空格键后,速度应缓慢下降,但在正确的时刻下降。

那么,有没有一种方法可以让我的speedbar以一种平稳的方式缓慢下降呢

下面是gif演示: 我甚至试着用KeyUp来做,但它正好在合适的时候起作用。我也试过,但故事是一样的

这是我的密码:

        float angle = -35.0f;
    Image img;

    public MPh()
    {
        InitializeComponent();

        this.MaximizeBox = false;
        this.pictureBox1.Parent = image;
        this.pictureBox1.BackColor = Color.Transparent;
        this.pictureBox1.Location = new Point(402, 100);

        img = new Bitmap("../../resources/speed.png");
        pictureBox1.Image = Utilities.RotateImage(img, angle);

        timer1.Start();
    }

    private void MPh_KeyDown(object sender, KeyEventArgs e)
    {
        timer1.Stop();
        if (e.KeyCode == Keys.Space)
        {
            if (angle < 215.0f)
                angle += 1.0f;

            pictureBox1.Image = (Bitmap)img.Clone();

            Image oldImage = pictureBox1.Image;

            pictureBox1.Image = Utilities.RotateImage(img, angle);

            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }
    }

    private void test()
    {

        angle -= 1.0f;

        pictureBox1.Image = (Bitmap)img.Clone();

        Image oldImage = pictureBox1.Image;

        pictureBox1.Image = Utilities.RotateImage(img, angle);

        if (oldImage != null)
        {
            oldImage.Dispose();
        }
    }

    private void MPh_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        {
            timer1.Start();
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        while (angle > -35.0f)
            test();
    }
浮动角度=-35.0f;
图像img;
公共卫生
{
初始化组件();
this.ebox=false;
this.pictureBox1.Parent=图像;
this.pictureBox1.BackColor=Color.Transparent;
this.pictureBox1.Location=新点(402100);
img=新位图(“../../resources/speed.png”);
pictureBox1.Image=实用程序.旋转图像(img,角度);
timer1.Start();
}
私有void mphu KeyDown(对象发送器,KeyEventArgs e)
{
timer1.Stop();
if(e.KeyCode==Keys.Space)
{
如果(角度<215.0f)
角度+=1.0f;
pictureBox1.Image=(位图)img.Clone();
图像oldImage=pictureBox1.图像;
pictureBox1.Image=实用程序.旋转图像(img,角度);
if(oldImage!=null)
{
Dispose();
}
}
}
专用无效测试()
{
角度-=1.0f;
pictureBox1.Image=(位图)img.Clone();
图像oldImage=pictureBox1.图像;
pictureBox1.Image=实用程序.旋转图像(img,角度);
if(oldImage!=null)
{
Dispose();
}
}
私有void mphu KeyUp(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.Space)
{
timer1.Start();
}
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
同时(角度>-35.0f)
test();
}

您的计时器设置的时间间隔是多少?@Sean它是100。我试图改变它,但它只是减慢了节目,然后同样快速地回到原点。