点击C#,通过计时器更快更快地移动图片框

问题描述 投票:1回答:2

我有一个类分配,可以在表单中随机移动一个图片框。一旦你点击图片框,它应该尖叫并改变图片然后将其改回原始图片。当你再次点击时,它应该更快。我让它努力使它变得更快。这是我的代码:

公共部分类Form1:表格{public 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);
    }
}

}

指出我需要去的地方让每次点击后的间隔越来越快谢谢。

c#
2个回答
0
投票

减少tm1.Interval应该这样做

  ...
    else
    if (tm1.Interval>10){tm1.Interval -= 10;} 
    timer1.Start();
© www.soinside.com 2019 - 2024. All rights reserved.