Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

First project in Dart - looking for style tips. I feel like I'm writing it a bit like Java, and would like to learn how to write dart-y dart.

Also - what's the convention on multiple files in a project? There are other files in this project, and I do not know how to include them in each other, etc...

See more

library snake;
import 'dart:html';
import 'dart_snake.dart';
import 'eats.dart';

class Snake {
  Map<int, Function> directions;
  Function nextMove;
  List<List<int>> links = [[330, 330]];

  Snake() {
    directions = {
      38: () {
        move(0, SQUARE_SIZE);
      },
      40: () {
        move(0, -SQUARE_SIZE);
      },
      37: () {
        move(-SQUARE_SIZE, 0);
      },
      39: () {
        move(SQUARE_SIZE, 0);
      }
    };
    nextMove = directions[40];
  }

  void move(int moreX, int moreY) => links.insert(0, [addToX(moreX), addToY(moreY)]);

  int headX() => links.first[0];
  int headY() => links.first[1];

  bool isOnEats(Eats eats) => headX() == eats.x && headY() == eats.y;
  bool isDead() => links.skip(1).any((e) => (e[0] == headX() && e[1] == headY()));

  int addToX(int more) => (headX() + more) % windowWidth();
  int addToY(int more) => (headY() + more) % windowHeight();

  void keyPressed(KeyboardEvent e) {
    if (directions.keys.contains(e.keyCode)) {
      nextMove = directions[e.keyCode];
    }
  }

  void doNextMove(bool ate) {
    nextMove();
    if (!ate) links.removeLast();
  }

  void addListeners() {
    window.onKeyUp.listen((KeyboardEvent e) {
      keyPressed(e);
    });
  }

  void draw() {
    for (List<int> link in links) {
      querySelector('#board').children.add(new DivElement()
          ..className = 'snake'
          ..style.left = "${link[0]}px"
          ..style.bottom = "${link[1]}px");
    }
  }
}
share|improve this question
    
I believe one of the main goals of dart is to look more like Java than javascript, so you should probably not worry about your code looking too much like Java. –  toto2 Jun 24 '14 at 14:00
    
Although I don't know dart, your code seems pretty clean. –  toto2 Jun 24 '14 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.