when running my game (snake) I am suppose to be able to move the snake around the form using the keys w,a,s and d. (atm I have only written the code for left and right movement just has a jumping off point). However, when running the program nothing happens. I have tried using break points, however, it seems as if my program isn’t even reading the keypress method, even though I am pressing keys.
Here is the Move method in the snake class.
public void Move(int pixels)
{
if (pixels < 0)
{
xPosition_ = xPosition_ -= SNAKE_WIDTH;
}
else if (pixels > 0)
{
xPosition_ = xPosition_ += SNAKE_WIDTH;
}
}
And here is the keypress method.
private void GameScreen_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
{
snake.Move(-1);
}
if (e.KeyChar == 'd')
{
snake.Move(1);
}
this.Refresh();
}
the graphics get drawn fine to the pictureBox control.
Thanks in advance.
Do you have the KeyPreview set to true on the form?