Thursday, 8 October 2015

Sprite Keyboard Movement - Simplegamelib Part Four

This is the fourth part in an ongoing series where we will look at the package I am currently writing called simplegamelib. Development is usually a little ahead of the current blog post. The projects homepage is located here.

This time we will be able to move the ninja around using the arrow/cursor keys. Game controls are specific to each game but for getting started simplegamelib provides basic movement of the player sprite. We have to tell the Game object which sprite is the player for this to work. When writing a larger game is is likely that you would sub-class or replace entirely the Game object.

Sprite player;
Game game = new Game("My Game", '#surface');

void main() {
  player = game.createSprite("images/ninjadude.png");
  game.player.sprite = player;

  player
    ..position = new Point(0, 30)
    ..movement = new Point(0, 0);

  print('starting game...');
  game.setUpKeys();
  game.start();
}

Click here to see it in action.

Next time, we will look at adding a background and some sound to liven things up!