You are going to need to set up a velocity system and a key buffer to get around the accessibility key presses on Windows or the movement is going to be terrible and herky-jerky, even once you get it working.
Basically, each time through the loop you move the picturebox += vx and += vy. Then, you create your "key down" event listener, and inside that listener, you alter the vx and vy variables based on which key was being moved. These are the corresponding movement values:
Right: vx = 5
Left: vx = -5
Down: vy = 5
Up: vy = -5
So that way, each time through the loop, the picturebox's real x and y values have the vx and vy values added to them, respectively. That will have them move to the right, left, up, down, or in any diagonal, and gets rid of that "move one step when the key is pressed, wait 1 second, and then move again" thing that Windows' key buffer causes.
Then you have to add another event listener for the "key up" event, and set the vx or vy of the key just released back to 0. That way they stop moving once the key is released.
While I have never tried to make a game in a Windows Form application, I have this exact system in ActionScript 3, which you should be able to read and apply to C# without much trouble. Just keep in mind that in Flash, you use the onFrameEnter event listener to create the loop, so you just substitute that for your looping mechanism. Here is a complete, simple character movement system:
Code: Select all
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class moveCharacter_main extends MovieClip {
private var vx:int;
private var vy:int;
private var myChar:Character;
public function moveCharacter_main() {
init();
myChar = new Character();
myChar.x = 50;
myChar.y = 50;
vy = 0;
vx = 0;
stage.addChild(myChar);
}
private function init():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownF);
stage.addEventListener(Event.ENTER_FRAME, onFrameEnterF);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpF);
}
private function onKeyDownF(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP) {
vy = -5;
} else if (event.keyCode == Keyboard.DOWN) {
vy = 5;
} else if (event.keyCode == Keyboard.LEFT) {
vx = -5;
} else if (event.keyCode == Keyboard.RIGHT) {
vx = 5;
}
}
private function onFrameEnterF(event:Event):void {
myChar.x += vx;
myChar.y += vy;
}
private function onKeyUpF(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP) {
vy = 0;
} else if (event.keyCode == Keyboard.DOWN) {
vy = 0;
} else if (event.keyCode == Keyboard.LEFT) {
vx = 0;
} else if (event.keyCode == Keyboard.RIGHT) {
vx = 0;
}
}
}
}
EDIT: Sorry, but since I have never actually done this in Windows Forms, I am not that familiar with the way C#'s keyDown event system works in Forms. But I know it probably works like most other events in Windows Forms, meaning you would probably need to set the event on the main form itself. Probably click the form to select it, and then change to events in the Property Explorer, and add the key down event. Then the code will pop up, allowing you to add whatever code to the event you want. I think that is how it should work.