/****
* Classes
****/
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var segmentGraphic = self.createAsset('snakeSegment', 'Snake Segment', 0.5, 0.5);
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphic = self.createAsset('food', 'Food', 0.5, 0.5);
self.place = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var snake = [];
var food;
var direction = {
x: 0,
y: 0
};
var nextDirection = {
x: 0,
y: 0
};
var snakeSpeed = 5;
var gridSize = 64;
var score = 0;
var scoreTxt;
var isGameOver = false;
function initGame() {
// Initialize the snake in the center of the screen
var initialX = 2048 / 2;
var initialY = 2732 / 2;
var snakeHead = new SnakeSegment();
snakeHead.move(initialX, initialY);
snake.push(snakeHead);
game.addChild(snakeHead);
// Initialize the food at a random position
placeFood();
// Initialize score display
scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Set initial direction
direction = {
x: gridSize,
y: 0
};
}
function placeFood() {
var foodX = Math.floor(Math.random() * (2048 / gridSize)) * gridSize + gridSize / 2;
var foodY = Math.floor(Math.random() * (2732 / gridSize)) * gridSize + gridSize / 2;
if (!food) {
food = new Food();
game.addChild(food);
}
food.place(foodX, foodY);
}
function updateScore() {
score += 1;
scoreTxt.setText(score.toString());
}
function gameOver() {
isGameOver = true;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
function handleInput(obj) {
var touchPos = obj.event.getLocalPosition(game);
var head = snake[0];
if (Math.abs(touchPos.x - head.x) > Math.abs(touchPos.y - head.y)) {
if (touchPos.x > head.x && direction.x === 0) {
nextDirection = {
x: gridSize,
y: 0
};
} else if (touchPos.x < head.x && direction.x === 0) {
nextDirection = {
x: -gridSize,
y: 0
};
}
} else {
if (touchPos.y > head.y && direction.y === 0) {
nextDirection = {
x: 0,
y: gridSize
};
} else if (touchPos.y < head.y && direction.y === 0) {
nextDirection = {
x: 0,
y: -gridSize
};
}
}
}
game.on('down', handleInput);
LK.on('tick', function () {
if (isGameOver) return;
// Update direction
direction = nextDirection;
// Move snake
var newX = snake[0].x + direction.x;
var newY = snake[0].y + direction.y;
// Check for collisions with walls
if (newX < 0 || newX > 2048 || newY < 0 || newY > 2732) {
gameOver();
return;
}
// Check for collisions with self
for (var i = 1; i < snake.length; i++) {
if (snake[i].x === newX && snake[i].y === newY) {
gameOver();
return;
}
}
// Check for food collision
if (snake[0].intersects(food)) {
var newSegment = new SnakeSegment();
newSegment.move(snake[snake.length - 1].x, snake[snake.length - 1].y);
snake.push(newSegment);
game.addChild(newSegment);
updateScore();
placeFood();
}
// Move snake segments
for (var i = snake.length - 1; i > 0; i--) {
snake[i].move(snake[i - 1].x, snake[i - 1].y);
}
// Move head last
snake[0].move(newX, newY);
});
// Start the game
initGame();
a wooden brown chair. In-Game asset. Blank background. High contrast.
a blue apple with a gun. In-Game asset. Blank background. High contrast.
the 8 balls from pool. In-Game asset. Blank background. High contrast.
a table with a gun. In-Game asset. Blank background. High contrast.
a roundsaw. In-Game asset. Blank background. High contrast.