如何在C#中制作一个必须在特定时间范围内在特定位置单击的按钮

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

我正在尝试使用 C# 在 Visual Studio 中创建一个游戏,其中有成排的滑动框,您必须在时间用完之前在某些位置一个接一个地按下一个键,有点像堆垛机。

我正在使用它作为基础,但不确定如何调整速度以使其首先工作,因为代码显然运行没有问题,但我只是在按钮给你一个消息框的地方得到它。

https://www.youtube.com/watch?v=O78n7apXjG8

只是询问有关去哪里的提示。 https://files.catbox.moe/x3wx22.zip

需要澄清的其他信息 https://catbox.moe/c/fakpxe

// Project by 124
// Redid:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        private int _ticks;
        private object textBox1;

        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }
        //This should be the part that moves the button back and forth but it's not working
        int Left = 140;
        private void timer1_tick(object sender, EventArgs e)
        {

          Left += 10;
            if (Left > 430)
            {
                Left = -138;
                Left += Left;
                if (Left > 140 )
                {

                } else
        {
                    button1.Left = Left;
                }
            }
            else
                button1.Left = Left;
            {
                button1.Visible = false;

                button2.Visible = true;
            }

            {
                _ticks++;
                this.Text = _ticks.ToString();
                if (_ticks == 9)
                {
                    this.Text = "Game Over";
                    timer1.Stop();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)

        //since the  movement button function is not working so is this one where it's supposed to give you the win but the win is there regardless
        {
            if (_ticks < 9) ;
            {

                    MessageBox.Show("good job you beat the time");
                    MessageBox.Show("You are a winner");

                }

            }
        private void button2_Click(object sender, EventArgs e)
        {

            Application.Restart();
            Environment.Exit(0);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
        }
    }
}

调整运行速度:

Left += Convert.ToInt32(label1.Text);

控制速度+1:

private void Button4_Click(object sender, EventArgs e) {
    label1.Text = (Convert.ToInt32(label1.Text) + 1).ToString();
}

控制速度-1:

private void Button5_Click(object sender, EventArgs e) {
    label1.Text = (Convert.ToInt32(label1.Text) - 1).ToString();
}

更新:

写在最上面:

可以通过修改'V0'和'a'的值来控制按键的速度和位置

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

设置定时器外的初始时间值

private double _ticks = 0;

将初始位置设置为0。

double Left = 0;

每秒增加1。因为internal是0.2所以每次加0.2

_ticks+=0.2;

已修改 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);
}

修改button_Click:'if'后不要加';',没有用

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");

    }
}

输出: