如何使用计时器提高游戏速度?

时间:2014-09-09 15:32:08

标签: c# timer onclick

我正在做一个简单的C#点击游戏,我知道这是愚蠢的问题,但是我还在学习C#而且我问你的专业人士如何让每个按钮点击的定时器间隔更快?

这是我的代码:

public partial class Form1 : Form
{
    private int clicks = 0;
    private int counter = 1;

    public Form1()
    {
       InitializeComponent();
    }

    private void UpdateButton()
    {
        if (clicks >= 50)
            button1.Enabled = true;
        else button1.Enabled = false;  
    }

    private void myDiamond_MouseUp(object sender, MouseEventArgs e)
    {  
        myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");  
    }

    private void myDiamond_MouseDown(object sender, MouseEventArgs e)
    {
        myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond  Clicker\\diamondMouseUp.png");
        clicks++;
        DiamondsScore.Text = "Diamonds: " + clicks.ToString();
        UpdateButton();
    }

    private void timer1_Tick_1(object sender, EventArgs e)
    {
        counter++;

        clicks = clicks + 1;
        DiamondsScore.Text = "Diamonds: " + clicks.ToString();
        UpdateButton();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        clicks = clicks - 50;
        DiamondsScore.Text = "Diamonds: " + clicks.ToString();
        timer1.Enabled = true;
        UpdateButton();
        button1.Enabled = false;
    }
}

感谢。

3 个答案:

答案 0 :(得分:1)

如果你用一个常数因子减少时间间隔,我认为你会有一种更自然的感觉。

将初始间隔和因子定义为双精度。

const double factor = 0.95;
double interval = 1000;

并在每次点击

interval *= factor; // Same as: interval = interval * factor;
timer1.Interval = (int)interval;

答案 1 :(得分:0)

更改计时器Interval属性,请参阅msdn

你可以做例如

timer1.Interval = timer1.Interval - TimeSpan.FromMilliseconds(10).TotalMilliseconds

答案 2 :(得分:0)

这样的事情应该有效:

    private void timer1_Tick_1(object sender, EventArgs e)
    {
        counter++;

       if(timer1.Interval >1) //if the timer interval is bigger than one
       {
          timer1.Interval --;
       } 


       clicks = clicks + 1;
        DiamondsScore.Text = "Diamonds: " + clicks.ToString();
        UpdateButton();
    }

注意:timer1.Interval--;timer1.Interval = timer1.Interval - 1;

相同

这个想法也可以通过clicks完成:

clicks++;