Help with C# Movement with Arrow keys on a Windows form

  • Thread starter Thread starter Gabbo G
  • Start date Start date
G

Gabbo G

Guest
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 WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyDown += new KeyEventHandler(Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{

int x = pictureBox2.Location.X;
int y = pictureBox2.Location.Y;

{
if(e.KeyCode == Keys.Right)
{x += 2;}
else if (e.KeyCode == Keys.Left)
{ x -= 2; }
else if (e.KeyCode == Keys.Up)
{ y -= 2; }
else if (e.KeyCode == Keys.Down)
{
if (pictureBox2.Image == WindowsFormsApplication12.Properties.Resources.minerFrontRight)
{
pictureBox2.Image = WindowsFormsApplication12.Properties.Resources.minerFrontLeft;
y += 2;

}
else
{
pictureBox2.Image = WindowsFormsApplication12.Properties.Resources.minerFrontRight;
y += 2;

}

}
}


pictureBox2.Location = new System.Drawing.Point(x, y);



}



}
}



I am attempting to create movement on my game, by moving the character around the screen and also changing the image for animation. At the moment it is very basic and is just using a KeyDown event with arrows keys to move it. I have just started trying to use the down arrow key, implementing the change in picture with each step. However, the program remains constantly on the right hand image after the down key is first pressed and I am unable to fix this. Any help would be greatly appreciated.

Thank you

Continue reading...
 
Back
Top