Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Most games don't wait for events. They poll the input device when neccessary and act accodringly. In fact, if you ever take a look at XNA, you'll see that theyre's a Keyboard.GetState() method (or Gamepad.GetState()) that you'll call in your update routine, and update your game logic based on the results. When working with Windows.Forms, there's nothing out of the box to do this, however you can P/Invoke the GetKeyBoardState() function to take advantage of this. The good thing about this is, that you can poll multiple keys at once, and you can therefore react to more than one key press at a time. Here's a simple class I found online that helps with this:</p> <p><a href="http://sanity-free.org/17/obtaining_key_state_info_in_dotnet_csharp_getkeystate_implementation.html" rel="noreferrer">http://sanity-free.org/17/obtaining_key_state_info_in_dotnet_csharp_getkeystate_implementation.html</a></p> <p>To demonstrate, I wrote a simple windows app that basically moves a ball around based on keyboard input. It uses the class I linked you to, to poll the keyboard's state. You'll notice that if you hold down two keys at a time, it'll move diagonally.</p> <p>First, Ball.cs:</p> <pre><code> public class Ball { private Brush brush; public float X { get; set; } public float Y { get; set; } public float DX { get; set; } public float DY { get; set; } public Color Color { get; set; } public float Size { get; set; } public void Draw(Graphics g) { if (this.brush == null) { this.brush = new SolidBrush(this.Color); } g.FillEllipse(this.brush, X, Y, Size, Size); } public void MoveRight() { this.X += DX; } public void MoveLeft() { this.X -= this.DX; } public void MoveUp() { this.Y -= this.DY; } public void MoveDown() { this.Y += this.DY; } } </code></pre> <p>Really nothing fancy at all....</p> <p>Then here's the Form1 code:</p> <pre><code> public partial class Form1 : Form { private Ball ball; private Timer timer; public Form1() { InitializeComponent(); this.ball = new Ball { X = 10f, Y = 10f, DX = 2f, DY = 2f, Color = Color.Red, Size = 10f }; this.timer = new Timer(); timer.Interval = 20; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } void timer_Tick(object sender, EventArgs e) { var left = KeyboardInfo.GetKeyState(Keys.Left); var right = KeyboardInfo.GetKeyState(Keys.Right); var up = KeyboardInfo.GetKeyState(Keys.Up); var down = KeyboardInfo.GetKeyState(Keys.Down); if (left.IsPressed) { ball.MoveLeft(); this.Invalidate(); } if (right.IsPressed) { ball.MoveRight(); this.Invalidate(); } if (up.IsPressed) { ball.MoveUp(); this.Invalidate(); } if (down.IsPressed) { ball.MoveDown(); this.Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.ball != null) { this.ball.Draw(e.Graphics); } } } </code></pre> <p>Simple little app. Just creates a ball and a timer. Every 20 milliseconds, it checks the keyboard state, and if a key is pressed it moves it and invalidates so that it can repaint.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload