【问题标题】:How do I move an object using a timer and the key press event (Like snake)如何使用计时器和按键事件移动对象(如蛇)
【发布时间】:2019-07-15 10:35:19
【问题描述】:

我想使用计时器自动移动一个按钮,并希望它在按下另一个键时改变方向,有点像蛇中发生的事情

我使用了一个字符串来设置方向,但这不起作用。

字符串 x = "正确";

    public Form1()
    {
        InitializeComponent();
        timer1.Interval = 1000;
        timer1.Start();

    }

    private void Player_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            string x = "up";
        }
        else if (e.KeyCode == Keys.S)
        {
            string x = "down";
        }
        else if (e.KeyCode == Keys.D)
        {
            string x = "right";
        }
        else if (e.KeyCode == Keys.A)
        {
            string x = "left";
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Timer1_Tick(object sender, EventArgs e)
    {
        while (x == "up")
        {
            Player.Top -= 10;
        }
        while (x == "down")
        {
            Player.Top += 10;
        }
        while (x == "right")
        {
            Player.Left += 10;
        }
        while (x == "left")
        {
            Player.Left -= 10;
        }
    }
}

按钮刚刚消失,但我希望它移动 10 直到它得到一个像“D”这样的按键然后它改变方向

【问题讨论】:

  • 尝试使用 Location 属性。 Player.Loaction = new Point(x, y)
  • 你是这个意思吗? while (x == "up") { Player.Location = new Point(Y += 10); }
  • 不要在 Tick 事件处理程序中使用 while(),这将是一个非常短的游戏。使用 if()。并添加“游戏结束”检查。
  • 如果您将 X="right" 设置为默认计时器,它将在玩家开始行动之前将其移动。因此,如果 x 为空,则返回条件

标签: c# winforms


【解决方案1】:

1:您每次都在本地创建变量 'x' 意味着在 player_keyDown 事件中,因此全局创建它。

2:您正在使用 while 循环它,不需要,因为您已经在使用 timer_tick。

3:使用按钮的 Location 属性代替 Top,Left 为您提供 X、Y 坐标

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        timer1.Interval = 1000;
        timer1.Start();
    }

    string x = "right";
    private void Player_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            x = "up";
        }
        else if (e.KeyCode == Keys.S)
        {
            x = "down";
        }
        else if (e.KeyCode == Keys.D)
        {
            x = "right";
        }
        else if (e.KeyCode == Keys.A)
        {
            x = "left";
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        if (x == "up")
        {
            Player.Location = new System.Drawing.Point(Player.Location.X, Player.Location.Y - 10);
        }
        if (x == "down")
        {
            Player.Location = new System.Drawing.Point(Player.Location.X, Player.Location.Y + 10);
        }
        if (x == "right")
        {
            Player.Location = new System.Drawing.Point(Player.Location.X + 10, Player.Location.Y);
        }
        if (x == "left")
        {
            Player.Location = new System.Drawing.Point(Player.Location.X - 10, Player.Location.Y);
        }

    }

}

【讨论】:

  • 好的,非常感谢,你知道我是怎么做到的,所以如果它朝一个方向移动,比如向右,它就不能朝相反的方向移动,在这种情况下是向左吗?跨度>
  • 如果它向右方向则不要将其值更改为向左意味着您限制用户将值更改为向左。仅在 if 块中添加条件 if (e.KeyCode == Keys.A && x!="right") { x = "left"; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2016-11-19
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多