Exemplo n.º 1
0
  public void move() {
    Vector2 nextDirection =
        Vector2.add(new Vector2(getHead().getX(), getHead().getY()), _direction);

    if (nextDirection.x < SNAKE_WIDTH * 2
        || nextDirection.x >= _mapWidth - SNAKE_WIDTH * 2
        || nextDirection.y < SNAKE_WIDTH * 2
        || nextDirection.y >= _mapHeight - SNAKE_WIDTH * 2) {
      turned();
      return;
    }

    if (_steps == 0) {
      turned();
    }

    _steps++;

    boolean isTurned = false;

    if (_steps >= SNAKE_WIDTH) {
      _steps = 0;
      isTurned = true;
    }
    _body.move(isTurned);
  }
Exemplo n.º 2
0
  public float handleSceneTouch(TouchEvent pSceneTouchEvent) {
    Vector2 v =
        new Vector2(
            getHead().getX() - pSceneTouchEvent.getX(), getHead().getY() - pSceneTouchEvent.getY());

    float angle = v.getAngle();
    if ((0 <= angle && angle <= 45) || (315 < angle && angle < 360)) {
      setDirection(SnakeDirection.RIGHT);
    } else if (45 < angle && angle <= 135) {
      setDirection(SnakeDirection.UP);
    } else if (135 < angle && angle <= 225) {
      setDirection(SnakeDirection.LEFT);
    } else if (225 < angle && angle <= 315) {
      setDirection(SnakeDirection.DOWN);
    }
    return angle;
  }
Exemplo n.º 3
0
  public boolean setDirection(final SnakeDirection pDirection) {
    Vector2 d = SnakeDirection.toVector(pDirection);
    if (_lastDirection == null || !Vector2.opposite(d, _lastDirection)) {
      _direction = d;
      return true;
    }

    return false;
  }