Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SFML.NET - Move an object in response to keyboard input
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.Window;
using SFML.System;
namespace SFMLNetSpriteMover {
static class SFMLNetSpriteMoverPlayground {
public static void Main (String [] args) {
GameWindow gw = new GameWindow ();
gw.Run ();
}
}
class GameWindow {
public RenderWindow PrincipalWindow { get; }
public List<Drawable> ToRender { get; }
private CircleShape PlayerObject;
// A potentially somewhat inelegant associatve array for tethering input detection to expected consumption
private Dictionary<String, Boolean> MovementStates;
public GameWindow () {
PrincipalWindow = new RenderWindow (new VideoMode (800, 600), "SFML.NET Sprite Mover");
ToRender = new List<Drawable> ();
MovementStates = new Dictionary<String, Boolean> () {
{ "Left", false },
{ "Right", false },
{ "Up", false },
{ "Down", false }
};
PrincipalWindow.KeyPressed += this.Window_KeyPressed;
// Now we need to handle releases so that we're aware of when movement should stop in a given direction
PrincipalWindow.KeyReleased += this.Window_KeyReleased;
PlayerObject = new CircleShape (50.0f) {
FillColor = Color.Magenta
};
ToRender.Add (PlayerObject);
}
public void Run () {
while (PrincipalWindow.IsOpen) {
PrincipalWindow.DispatchEvents ();
// The "player" will move according to changes in the movement flags detected in the previous function.
ParsePlayerInput ();
PrincipalWindow.Clear (Color.Green);
if (ToRender.Count > 0) {
foreach (Drawable d in ToRender) {
PrincipalWindow.Draw (d);
}
}
PrincipalWindow.Display ();
// Debugging purposes only. Should vomit all over the console with output per frame.
Console.Write ("Input States: ");
foreach (KeyValuePair<String, Boolean> b in MovementStates) {
if (b.Value) {
Console.Write (b.Key + ", ");
}
}
Console.WriteLine ("");
}
}
/// <summary>
/// Handle key presses. Set movement flags to true if a given directional arrow is pressed.
/// </summary>
/// <param name="sender"><see cref="Window.KeyPressed"/></param>
/// <param name="e"><see cref="Window.KeyPressed"/></param>
private void Window_KeyPressed (Object sender, KeyEventArgs e) {
Window win = sender as Window;
if (e.Code == Keyboard.Key.Escape) {
win.Close ();
}
switch (e.Code) {
case Keyboard.Key.Left:
MovementStates ["Left"] = true;
break;
case Keyboard.Key.Right:
MovementStates ["Right"] = true;
break;
case Keyboard.Key.Up:
MovementStates ["Up"] = true;
break;
case Keyboard.Key.Down:
MovementStates ["Down"] = true;
break;
}
}
/// <summary>
/// Handle key releases. Set movement flags to false if a given directional arrow is pressed.
/// </summary>
/// <param name="sender"><see cref="Window"/></param>
/// <param name="e"><see cref="Window.KeyEventArgs"/></param>
private void Window_KeyReleased (Object sender, KeyEventArgs e) {
switch (e.Code) {
case Keyboard.Key.Left:
MovementStates ["Left"] = false;
break;
case Keyboard.Key.Right:
MovementStates ["Right"] = false;
break;
case Keyboard.Key.Up:
MovementStates ["Up"] = false;
break;
case Keyboard.Key.Down:
MovementStates ["Down"] = false;
break;
}
}
private void ParsePlayerInput () {
// The following code block is for four-directional movement
//if (MovementStates["Left"]) {
// PlayerObject.Position += new Vector2f (-1.0f, 0.0f);
//} else if (MovementStates["Right"]) {
// PlayerObject.Position += new Vector2f (1.0f, 0.0f);
//} else if (MovementStates["Up"]) {
// PlayerObject.Position += new Vector2f (0.0f, -1.0f);
//} else if (MovementStates["Down"]) {
// PlayerObject.Position += new Vector2f (0.0f, 1.0f);
//}
// The following code block is for eight-directional movement
if (MovementStates["Left"]) {
PlayerObject.Position += new Vector2f (-1.0f, 0.0f);
}
if (MovementStates["Right"]) {
PlayerObject.Position += new Vector2f (1.0f, 0.0f);
}
if (MovementStates["Up"]) {
PlayerObject.Position += new Vector2f (0.0f, -1.0f);
}
if (MovementStates["Down"]) {
PlayerObject.Position += new Vector2f (0.0f, 1.0f);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.