慢慢冷静一下车速表

时间:2017-01-22 17:42:49

标签: c# timer keyevent

我正在开发一款汽车驾驶模拟器。这个想法很简单:现在你只需要按空格键,当它按下时,速度会不断增长。释放空格键后,速度应缓慢下降,但在适当的时刻下降。 Preview

那么有什么方法可以让我的速度条缓慢而平稳地下降?

这是gif演示: https://psv4.vk.me/c810336/u26848512/docs/44c13660f362/gif.gif?extra=4MqKvdhJzi7zl0QBLC6Vw2sUPznozeuDjmPcsMRJGRmN3VaN-Oa7Tjp_0nZrlDgjAfDGVuKO7bhwu-363yx_XGYVh8FoGGhXHzBoROEQjhuB-QsJghVq1f3s 我尝试用KeyUp做到这一点,但它恰到好处。我也尝试过Timer但同样的故事。

这是我的代码:

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

0 个答案:

没有答案