Input and Basic Interaction

The next step toward making a video game using Otter is registering player input and responding to it.  For basic input things the Input class can be used.  When the Input class is used in a Scene or Entity it will use the Input object that belongs to the current running Game.  If you need to use the Input class outside of a Scene or Entity you can use Input.Instance to get a reference to the current active Input object.

The following example code will create a "PlayerEntity" that responds to the W, A, S, and D keys, and also will change movement speeds when the spacebar is pressed.  Take a look at the comments for more details.

using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InputInteraction {
  class Program {
    static void Main(string[] args) {
      // Create a Game
      var game = new Game("Input Example");

      // Create a Scene
      var scene = new Scene();
      // Add a PlayerEntity to the Scene at half of the Game's width, and half of the Game's height (centered)
      scene.Add(new PlayerEntity(Game.Instance.HalfWidth, Game.Instance.HalfHeight));

      // Start the game using the created Scene.
      game.Start(scene);
    }
  }

  class PlayerEntity : Entity {

    /// <summary>
    /// The current move speed of the Entity.
    /// </summary>
    public float MoveSpeed;

    /// <summary>
    /// The move speed for when the Entity is moving slowly.
    /// </summary>
    public const float MoveSpeedSlow = 5;
    /// <summary>
    /// The move speed for when the Entity is moving quickly.
    /// </summary>
    public const float MoveSpeedFast = 10;

    public PlayerEntity(float x, float y) : base(x, y) {
      // Create a rectangle image.
      var image = Image.CreateRectangle(32);
      // Add the rectangle graphic to the Entity.
      AddGraphic(image);
      // Center the image's origin.
      image.CenterOrigin();

      // Assign the initial move speed to be the slow speed.
      MoveSpeed = MoveSpeedSlow;
    }

    public override void Update() {
      base.Update();
      // Every update check for input and react accordingly.
      
      // If the W key is down,
      if (Input.KeyDown(Key.W)) {
        // Move up by the move speed.
        Y -= MoveSpeed;
      }
      // If the S key is down,
      if (Input.KeyDown(Key.S)) {
        // Move down by the move speed.
        Y += MoveSpeed;
      }
      // If the A key is down,
      if (Input.KeyDown(Key.A)) {
        // Move left by the move speed.
        X -= MoveSpeed;
      }
      // If the D key is down,
      if (Input.KeyDown(Key.D)) {
        // Move right by the move speed.
        X += MoveSpeed;
      }

      // If the space bar key is pressed,
      if (Input.KeyPressed(Key.Space)) {
        // If the Entity is currently slow,
        if (MoveSpeed == MoveSpeedSlow) {
          // Set the Entity to fast,
          MoveSpeed = MoveSpeedFast;
          // And make its Color red.
          Graphic.Color = Color.Red;
        }
        // If the Entity is currently fast,
        else {
          // Set the Entity to slow,
          MoveSpeed = MoveSpeedSlow;
          // And make its Color white.
          Graphic.Color = Color.White;
        }
      }
    }

  }
}

That wasn't so bad!  A simple Player box character running around with just a few lines of magical code.

Examples