/****
* Classes
****/
// Obstacle class for the snake to avoid
var Obstacle = Container.expand(function () {
  var self = Container.call(this);
  self.attachAsset('obstacle', {
    anchorX: 0.5,
    anchorY: 0.5
  });
});
// Prey class for the snake to eat
var Prey = Container.expand(function () {
  var self = Container.call(this);
  self.attachAsset('prey', {
    anchorX: 0.5,
    anchorY: 0.5
  });
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Example assets: snakeHead, snakeBody, prey, obstacle
// Snake class to manage the snake's segments and movement
var Snake = Container.expand(function () {
  var self = Container.call(this);
  self.segments = [];
  self.direction = {
    x: 0,
    y: 1
  }; // Initial direction: down
  // Initialize snake with a head
  var head = self.attachAsset('snakeHead', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.segments.push(head);
  // Method to grow the snake
  self.grow = function () {
    var newSegment = self.attachAsset('snakeBody', {
      anchorX: 0.5,
      anchorY: 0.5
    });
    self.segments.push(newSegment);
  };
  // Method to update snake's position
  self.update = function () {
    for (var i = self.segments.length - 1; i > 0; i--) {
      self.segments[i].x = self.segments[i - 1].x;
      self.segments[i].y = self.segments[i - 1].y;
    }
    self.segments[0].x += self.direction.x * 20; // Move head
    self.segments[0].y += self.direction.y * 20;
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var snake = new Snake();
game.addChild(snake);
snake.segments[0].x = 2048 / 2;
snake.segments[0].y = 2732 / 2;
var prey = new Prey();
game.addChild(prey);
prey.x = Math.random() * 2048;
prey.y = Math.random() * 2732;
var obstacles = [];
for (var i = 0; i < 5; i++) {
  var obstacle = new Obstacle();
  game.addChild(obstacle);
  obstacle.x = Math.random() * 2048;
  obstacle.y = Math.random() * 2732;
  obstacles.push(obstacle);
}
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
  var head = snake.segments[0];
  if (x < head.x) {
    snake.direction = {
      x: -1,
      y: 0
    }; // Move left
  } else if (x > head.x) {
    snake.direction = {
      x: 1,
      y: 0
    }; // Move right
  } else if (y < head.y) {
    snake.direction = {
      x: 0,
      y: -1
    }; // Move up
  } else {
    snake.direction = {
      x: 0,
      y: 1
    }; // Move down
  }
};
// Update game state
game.update = function () {
  snake.update();
  // Check for collision with prey
  if (snake.segments[0].intersects(prey)) {
    snake.grow();
    prey.x = Math.random() * 2048;
    prey.y = Math.random() * 2732;
  }
  // Check for collision with obstacles
  for (var i = 0; i < obstacles.length; i++) {
    if (snake.segments[0].intersects(obstacles[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  // Check for collision with self
  for (var i = 1; i < snake.segments.length; i++) {
    if (snake.segments[0].intersects(snake.segments[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  // Check for out of bounds
  var head = snake.segments[0];
  if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
    LK.effects.flashScreen(0xff0000, 1000);
    LK.showGameOver();
  }
}; /****
* Classes
****/
// Obstacle class for the snake to avoid
var Obstacle = Container.expand(function () {
  var self = Container.call(this);
  self.attachAsset('obstacle', {
    anchorX: 0.5,
    anchorY: 0.5
  });
});
// Prey class for the snake to eat
var Prey = Container.expand(function () {
  var self = Container.call(this);
  self.attachAsset('prey', {
    anchorX: 0.5,
    anchorY: 0.5
  });
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Example assets: snakeHead, snakeBody, prey, obstacle
// Snake class to manage the snake's segments and movement
var Snake = Container.expand(function () {
  var self = Container.call(this);
  self.segments = [];
  self.direction = {
    x: 0,
    y: 1
  }; // Initial direction: down
  // Initialize snake with a head
  var head = self.attachAsset('snakeHead', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.segments.push(head);
  // Method to grow the snake
  self.grow = function () {
    var newSegment = self.attachAsset('snakeBody', {
      anchorX: 0.5,
      anchorY: 0.5
    });
    self.segments.push(newSegment);
  };
  // Method to update snake's position
  self.update = function () {
    for (var i = self.segments.length - 1; i > 0; i--) {
      self.segments[i].x = self.segments[i - 1].x;
      self.segments[i].y = self.segments[i - 1].y;
    }
    self.segments[0].x += self.direction.x * 20; // Move head
    self.segments[0].y += self.direction.y * 20;
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var snake = new Snake();
game.addChild(snake);
snake.segments[0].x = 2048 / 2;
snake.segments[0].y = 2732 / 2;
var prey = new Prey();
game.addChild(prey);
prey.x = Math.random() * 2048;
prey.y = Math.random() * 2732;
var obstacles = [];
for (var i = 0; i < 5; i++) {
  var obstacle = new Obstacle();
  game.addChild(obstacle);
  obstacle.x = Math.random() * 2048;
  obstacle.y = Math.random() * 2732;
  obstacles.push(obstacle);
}
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
  var head = snake.segments[0];
  if (x < head.x) {
    snake.direction = {
      x: -1,
      y: 0
    }; // Move left
  } else if (x > head.x) {
    snake.direction = {
      x: 1,
      y: 0
    }; // Move right
  } else if (y < head.y) {
    snake.direction = {
      x: 0,
      y: -1
    }; // Move up
  } else {
    snake.direction = {
      x: 0,
      y: 1
    }; // Move down
  }
};
// Update game state
game.update = function () {
  snake.update();
  // Check for collision with prey
  if (snake.segments[0].intersects(prey)) {
    snake.grow();
    prey.x = Math.random() * 2048;
    prey.y = Math.random() * 2732;
  }
  // Check for collision with obstacles
  for (var i = 0; i < obstacles.length; i++) {
    if (snake.segments[0].intersects(obstacles[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  // Check for collision with self
  for (var i = 1; i < snake.segments.length; i++) {
    if (snake.segments[0].intersects(snake.segments[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  // Check for out of bounds
  var head = snake.segments[0];
  if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
    LK.effects.flashScreen(0xff0000, 1000);
    LK.showGameOver();
  }
};