1

I am a beginner at C# (C Sharp) and I couldn't figure out why my Arrow keys would not work. Can anyone help me? Note: I am a beginner, I have been working at this for a while now and I can't figure it out. I have tried researching it with no luck, I hope I don't bother you. When I try and move it it doesn't work.

Here is my Form1

public partial class Form1 : Form
{
    Graphics paper;
    Snake snake = new Snake();

    bool left = false;
    bool right = false;
    bool down = false;
    bool up = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        paper = e.Graphics;
        snake.drawSnake(paper);
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Down && up == false)
        {
            down = true;
            up = false;
            right = false;
            left = false;
        }
        if (e.KeyData == Keys.Up && down == false)
        {
            down = false;
            up = true;
            right = false;
            left = false;
        }
        if (e.KeyData == Keys.Right && left == false)
        {
            down = false;
            up = false;
            right = true;
            left = false;
        }
        if (e.KeyData == Keys.Left && right == false)
        {
            down = false;
            up = false;
            right = false;
            left = true;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (down) { snake.moveDown(); }
        if (up) { snake.moveUp(); }
        if (right) { snake.moveRight(); }
        if (left) { snake.moveLeft(); }

        this.Invalidate();
    }
 }

} Here is my Snake class if you need it.

{

    public Snake()
    {
        snakeRec = new Rectangle[3];
        brush = new SolidBrush(Color.Blue);

        x = 20;
        y = 0;
        width = 10;
        height = 10;

        for(int i = 0; i < snakeRec.Length; i++)
        {
            snakeRec[i] = new Rectangle(x, y, width, height);
            x -= 10;
        }
    }
    public void drawSnake(Graphics paper)
    {
        foreach (Rectangle rec in snakeRec)
        {
            paper.FillRectangle(brush, rec);
        }
    }

    public void drawSnake()
    {
        for (int i = snakeRec.Length - 1; i > 0; i--)
        {
            snakeRec[i] = snakeRec[i - 1];
        }
    }

    public void moveDown()
    {
        drawSnake();
        snakeRec[0].Y += 10;
    }
    public void moveUp()
    {
        drawSnake();
        snakeRec[0].Y -= 10;
    }
    public void moveRight()
    {
        drawSnake();
        snakeRec[0].X += 10;
    }
    public void moveLeft()
    {
        drawSnake();
        snakeRec[0].X -= 10;
    }
}

}

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

1

I tried your code and it works well, so this is the only thing I can think of:

private void Form1_Load(object sender, EventArgs e)
{
   timer1.Enabled = true;
}

Make sure that big guy is enabled.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
0

Don't hold on to the Graphics from the Paint() event like that. Just pass e.Graphics directly to your Draw() method like this:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        snake.drawSnake(e.Graphics);
    }

Also, make sure the Paint() event of the Form is wired up. Select the Form. Now click the "Lightning" Bolt" icon in the Properties Pane (bottom right by default). Find the Paint entry and change the dropdown to the right to "Form1_Paint".

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40