Alex
Alex

Reputation: 9

How do I move an object using a timer and the key press event (Like snake)

I want to move a button automatically using a timer and want it to change direction when another key is pressed, kinda like what happens in snake

I have used a string to set what the direction should be, but this doesn't work.

string x = "right";

    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;
        }
    }
}

The button just disappears, but I want it to move by ten until it gets a key press like "D" then it changes direction

Upvotes: 0

Views: 689

Answers (1)

Mangesh Auti
Mangesh Auti

Reputation: 1153

1: You are creating variable 'x' every time means locally in player_keyDown event so create it globally.

2: you are using while loop it, not needed as you are already using timer_tick.

3: Instead of Top, Left use Location property of button it gives you X, Y co-ordinate

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

    }

}

Upvotes: 1

Related Questions