/****
* Classes
****/
// Food class to manage the food's position
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphic = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the food on the screen
self.randomizePosition = function () {
self.x = Math.floor(Math.random() * 2048);
self.y = Math.floor(Math.random() * 2732);
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For example, the snake and food will be represented by shapes or images initialized dynamically.
// Snake class to manage the snake's body and movement
var Snake = Container.expand(function () {
var self = Container.call(this);
self.body = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: moving up
self.speed = 10; // Speed of the snake
// Initialize the snake with a starting length
self.init = function (length) {
for (var i = 0; i < length; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
segment.x = 1024; // Center of the screen
segment.y = 1366 + i * 20; // Stacked vertically
self.body.push(segment);
}
};
// Update the snake's position
self.update = function () {
// Move the body segments
for (var i = self.body.length - 1; i > 0; i--) {
self.body[i].x = self.body[i - 1].x;
self.body[i].y = self.body[i - 1].y;
}
// Move the head
self.body[0].x += self.direction.x * self.speed;
self.body[0].y += self.direction.y * self.speed;
};
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.body[self.body.length - 1];
var newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.body.push(newSegment);
};
// Check if the snake has collided with itself
self.checkSelfCollision = function () {
var head = self.body[0];
for (var i = 1; i < self.body.length; i++) {
if (head.x === self.body[i].x && head.y === self.body[i].y) {
return true;
}
}
return false;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var snake = new Snake();
var food = new Food();
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
var score = 0;
// Initialize the snake and food
snake.init(5);
food.randomizePosition();
// Add snake and food to the game
game.addChild(snake);
game.addChild(food);
// Add score text to the GUI
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
var head = snake.body[0];
var dx = x - head.x;
var dy = y - head.y;
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
snake.direction = {
x: 1,
y: 0
}; // Move right
} else {
snake.direction = {
x: -1,
y: 0
}; // Move left
}
} else {
if (dy > 0) {
snake.direction = {
x: 0,
y: 1
}; // Move down
} else {
snake.direction = {
x: 0,
y: -1
}; // Move up
}
}
};
// Update game logic
game.update = function () {
snake.update();
// Check for collision with food
if (Math.abs(snake.body[0].x - food.x) < 50 && Math.abs(snake.body[0].y - food.y) < 50) {
snake.grow();
food.randomizePosition();
score++;
scoreTxt.setText(score);
}
// Check for self-collision
if (snake.checkSelfCollision()) {
// Removed game over when snake collides with itself
}
// Check for collision with walls
var head = snake.body[0];
if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
// Removed game over when snake collides with walls
// Implement logic to wrap the snake around the screen
if (head.x < 0) {
head.x = 2048;
}
if (head.x > 2048) {
head.x = 0;
}
if (head.y < 0) {
head.y = 2732;
}
if (head.y > 2732) {
head.y = 0;
}
}
};