Creative Commons Licence This work is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License

Source: gitlab

Snake in Dart

Andy Balaam
artificialworlds.net/blog

Contents

Snake

Dart

No surprises

void main()
{
    print( "Hello, world!" );
}

No surprises

int twice( int x )
{
    return x * 2;
}

No surprises

class Game
{
    int width;
    int left;
    Game( int w, int l )
    {
        width = w;
        left = l;
    }
}

No surprises

for ( int i = 1; i < snake.length; ++i )
{
  if ( snake[i] == head )
  {
    dead = true;
  }
}

No surprises

switch( e.keyCode )
{
  case KeyCode.UP:
    nextDir = Dir.up;
    break;
  // ...

No surprises

snake = new List<Point>();

Lexical scope

void main()
{
  var x = 3;
  for ( int i = 0; i < 10; ++i )
  {
    var x = 5;
    // ...
  }
  print( x ); // prints 3
}

Construction and properties

class Game
{
    int width;
    int left;
    Game( this.width, this.left ) {}

    get int right =>
        return left + width;

    set right( int val ) =>
        width = val - left;
}

Development environment

Deployment

More info

Patreon! patreon.com/andybalaam
Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net