/****
* Classes
****/
// Candy class to manage the candy's behavior
var Candy = Container.expand(function () {
var self = Container.call(this);
var candyGraphics = self.attachAsset('candy', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the candy on the screen
self.randomizePosition = function () {
self.x = Math.floor(Math.random() * 2048);
self.y = Math.floor(Math.random() * 2732);
};
return self;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Snake class to manage the snake's behavior
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: up
self.speed = 5; // Speed of the snake
// Initialize the snake with 3 segments
for (var i = 0; i < 3; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
segment.x = 1024; // Center horizontally
segment.y = 1366 + i * 20; // Stack segments vertically
self.segments.push(segment);
}
// Update the snake's position
self.update = function () {
// Move each segment to the position of the previous one
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;
}
// Move the head in the current direction
self.segments[0].x += self.direction.x * self.speed;
self.segments[0].y += self.direction.y * self.speed;
};
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.segments[self.segments.length - 1];
var newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.1,
// Increase the size of the new segment slightly
scaleY: 1.1 // Increase the size of the new segment slightly
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.segments.push(newSegment);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
snake = new Snake();
game.addChild(snake);
candy = new Candy();
candy.randomizePosition();
game.addChild(candy);
score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle swipe events to change the snake's direction
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (Math.abs(game_position.x - snake.segments[0].x) > Math.abs(game_position.y - snake.segments[0].y)) {
snake.direction = {
x: game_position.x > snake.segments[0].x ? 1 : -1,
y: 0
};
} else {
snake.direction = {
x: 0,
y: game_position.y > snake.segments[0].y ? 1 : -1
};
}
};
// Update game logic
game.update = function () {
snake.update();
// Check collision with candy
if (snake.segments[0].intersects(candy)) {
snake.grow();
candy.randomizePosition();
score += 1;
scoreTxt.setText('Score: ' + score);
// Increase the size of the snake slightly each time it scores
for (var i = 0; i < snake.segments.length; i++) {
snake.segments[i].scaleX += 0.01;
snake.segments[i].scaleY += 0.01;
}
}
// Check collision with walls
if (snake.segments[0].x < 0 || snake.segments[0].x > 2048 || snake.segments[0].y < 0 || snake.segments[0].y > 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Check if the player has won
if (score >= 70) {
LK.showGameOver();
}
};
var score = 0;
var snake;
var candy;
if (!snake) {
snake = new Snake();
game.addChild(snake);
} /****
* Classes
****/
// Candy class to manage the candy's behavior
var Candy = Container.expand(function () {
var self = Container.call(this);
var candyGraphics = self.attachAsset('candy', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the candy on the screen
self.randomizePosition = function () {
self.x = Math.floor(Math.random() * 2048);
self.y = Math.floor(Math.random() * 2732);
};
return self;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Snake class to manage the snake's behavior
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: up
self.speed = 5; // Speed of the snake
// Initialize the snake with 3 segments
for (var i = 0; i < 3; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
segment.x = 1024; // Center horizontally
segment.y = 1366 + i * 20; // Stack segments vertically
self.segments.push(segment);
}
// Update the snake's position
self.update = function () {
// Move each segment to the position of the previous one
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;
}
// Move the head in the current direction
self.segments[0].x += self.direction.x * self.speed;
self.segments[0].y += self.direction.y * self.speed;
};
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.segments[self.segments.length - 1];
var newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.1,
// Increase the size of the new segment slightly
scaleY: 1.1 // Increase the size of the new segment slightly
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.segments.push(newSegment);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
snake = new Snake();
game.addChild(snake);
candy = new Candy();
candy.randomizePosition();
game.addChild(candy);
score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle swipe events to change the snake's direction
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (Math.abs(game_position.x - snake.segments[0].x) > Math.abs(game_position.y - snake.segments[0].y)) {
snake.direction = {
x: game_position.x > snake.segments[0].x ? 1 : -1,
y: 0
};
} else {
snake.direction = {
x: 0,
y: game_position.y > snake.segments[0].y ? 1 : -1
};
}
};
// Update game logic
game.update = function () {
snake.update();
// Check collision with candy
if (snake.segments[0].intersects(candy)) {
snake.grow();
candy.randomizePosition();
score += 1;
scoreTxt.setText('Score: ' + score);
// Increase the size of the snake slightly each time it scores
for (var i = 0; i < snake.segments.length; i++) {
snake.segments[i].scaleX += 0.01;
snake.segments[i].scaleY += 0.01;
}
}
// Check collision with walls
if (snake.segments[0].x < 0 || snake.segments[0].x > 2048 || snake.segments[0].y < 0 || snake.segments[0].y > 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Check if the player has won
if (score >= 70) {
LK.showGameOver();
}
};
var score = 0;
var snake;
var candy;
if (!snake) {
snake = new Snake();
game.addChild(snake);
}