在C#中使用单击计时器,越来越快地移动图片框

时间:2019-03-15 18:45:31

标签: c#

我有一个课堂作业,可以在整个表格中随机移动图片框。单击图片框后,它会尖叫并更改图片,然后将其更改回原始图片。当您再次单击时,它应该走得更快。我正在努力使其变得更快。这是我的代码:

公共局部类Form1:表单     {         公共Form1()

    {
        InitializeComponent();
        tm1.Interval = 1000;
        tm1.Tick += new EventHandler(tm_Tick);
    }
        Timer tm1 = new Timer();

        int X = 0;
        int Y = 0;

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled)
        {
            timer1.Stop();
            pictureBox1.Image = Properties.Resources.Mimikyu;
            Application.DoEvents();
            pictureBox1.WaitOnLoad = true;
            System.Threading.Thread.Sleep(10);
            SoundPlayer sp = new SoundPlayer(Properties.Resources.screa);
            sp.PlaySync();
            pictureBox1.Image = Properties.Resources.Evee;
        }
        else
            timer1.Start();
    }

    private void tm_Tick(object sender, EventArgs e)
    {
        int X = ((int)(new Random().Next(0, 1000)));
        int Y = ((int)(new Random().Next(0, 500)));

        if (X > 1025 - pictureBox1.Width)
            {
                X = 1025 - pictureBox1.Width;
            }
        if (Y > 545 - pictureBox1.Height)
            {
                Y = 545 - pictureBox1.Height;
            }
        pictureBox1.Location = new Point(X, Y);
    }
}

}

将我指向需要的位置,以使每次单击后间隔越来越快。谢谢。

2 个答案:

答案 0 :(得分:0)

更改计时器的时间间隔?

private void pictureBox1_Click(object sender, EventArgs e)
{

    timer1.Interval -= 10;
}

答案 1 :(得分:0)

减小tm1.Interval应该可以做到

  ...
    else
    if (tm1.Interval>10){tm1.Interval -= 10;} 
    timer1.Start();