Thursday, 1 October 2015

Simple Sprite Based Web Games In Dart - Part One

This is a new series where we will look a Dart package I am developing for simple Canvas based sprites games. I have written a couple of little games in Dart and always thought it would be an idea to split off all the general code into a reusable package.

The library is called simplegamelib and is heavily inspired by PyGame. The source is on GitHub here and the package is on pub. My personal aims are to write a Space Invaders type game and have really understandable library so it can be used to learn Dart. The code (so far!) is on GitHub and the project's homepage is here on blogger.

Yes it is time to get around to our 'Hello World'. We will create a simple scene and have a familiar looking sprite move from one side of the screen to the other.

import 'dart:html';
import 'package:simplegamelib/simplegamelib.dart';

void main() {
  Game game = new Game("My Game", '#surface');
  Sprite player = game.createSprite("images/ninjadude.png");

  player
    ..position = new Point(0, 10)
    ..movement = Movements.east;
  print('starting game...');

  game.start();
}

As you can see it is easy to get started with just a few lines of code. Click here to see it in action. Hit reload to start again.

Next time, we will look at adding more than one sprite and collision detection!