Home > Software design >  How to make a button that you must click at certain places within a certain time frame in C#
How to make a button that you must click at certain places within a certain time frame in C#

Time:09-30

I am trying to create a game in Visual studios using C# where you have rows of sliding boxes that you must press a key in certain locations one after the other, kinda like stacker, before the time runs out.

I am using this as the basis but unsure how to adjust the speed among making it work in the first place since the code apparently runs with no issues but I only got it where the button just gives you a message box.

enter image description here

Updated:

Write at the top:

You can control the speed and position of the button by modifying the values of'V0' and'a'.

//current position       
Left  = 20* _ticks (a* _ticks* _ticks)/2;//s = v0·t   a·t²/2

Set the initial time value outside the timer

private double _ticks = 0;

Set the initial position to 0.

double Left = 0;

Increase by 1 every second. Because internal is 0.2, add 0.2 every time.

_ticks =0.2;

enter image description here

Modified timer1_tick:

private void timer1_tick(object sender, EventArgs e) {
    //Set acceleration
    double a = 0.5;
    //Increase by 1 every second. Because internal is 0.2, add 0.2 every time.
    _ticks  = 0.2;
    this.Text = _ticks.ToString();
    //Time to stop at 9s
    if (_ticks == 9) {
        this.Text = "Game Over";
        timer1.Stop();
        MessageBox.Show("Game Over");
        button1.Visible = false;
        button2.Visible = true;
    }
    //current position       
    Left  = 20 * _ticks   (a * _ticks * _ticks) / 2;//s = v0·t   a·t²/2
                                                    //When the total distance exceeds the set width on the interface, perform operations such as subtraction until the current position is less than the set value.
rtn:
    if (Left > 300) {
        Left -= 300;
        if (Left > 300) {
            goto rtn;//Use goto: Perform an iterative operation.
        } else {
            button1.Left = Convert.ToInt32(Left);//Cast double data type to int
        }
    } else
        button1.Left = Convert.ToInt32(Left);
}

Modified button_Click: Don’t add ‘;’ after ‘if’, it will be useless.

private void button1_Click(object sender, EventArgs e) {
    if (_ticks < 9) //Don’t add ‘;’ after ‘if’, it will be useless.
    {
        timer1.Stop();
        button1.Visible = false;
        button2.Visible = true;
        MessageBox.Show("good job you beat the time");
        MessageBox.Show("You are a winner");

    }
}

Output:

enter image description here

  • Related