/****
* Classes
****/
// PowerUp class to represent power-ups in the game
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the power-up within the game area
self.randomizePosition = function () {
self.x = Math.random() * 2048;
self.y = Math.random() * 2732;
};
});
//<Assets used in the game will automatically appear here>
// Snake class to represent the snake in the game
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: moving up
self.speed = 5; // Speed of the snake
// Initialize the snake with a given 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 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;
};
// Change the direction of the snake
self.changeDirection = function (newDirection) {
self.direction = newDirection;
};
// 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
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.segments.push(newSegment);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var snake = new Snake();
var powerUp = new PowerUp();
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize the snake and power-up
game.addChild(snake);
snake.init(5); // Start with 5 segments
game.addChild(powerUp);
powerUp.randomizePosition();
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (game_position.x < snake.segments[0].x) {
snake.changeDirection({
x: -1,
y: 0
}); // Move left
} else if (game_position.x > snake.segments[0].x) {
snake.changeDirection({
x: 1,
y: 0
}); // Move right
} else if (game_position.y < snake.segments[0].y) {
snake.changeDirection({
x: 0,
y: -1
}); // Move up
} else {
snake.changeDirection({
x: 0,
y: 1
}); // Move down
}
};
// Update game logic
game.update = function () {
snake.update();
// Check for collision with power-up
if (snake.segments[0].intersects(powerUp)) {
snake.grow();
powerUp.randomizePosition();
score += 10;
scoreTxt.setText('Score: ' + score);
}
// Check for collision with walls
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();
}
// Check for collision with itself
for (var i = 1; i < snake.segments.length; i++) {
if (head.intersects(snake.segments[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}; /****
* Classes
****/
// PowerUp class to represent power-ups in the game
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the power-up within the game area
self.randomizePosition = function () {
self.x = Math.random() * 2048;
self.y = Math.random() * 2732;
};
});
//<Assets used in the game will automatically appear here>
// Snake class to represent the snake in the game
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: moving up
self.speed = 5; // Speed of the snake
// Initialize the snake with a given 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 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;
};
// Change the direction of the snake
self.changeDirection = function (newDirection) {
self.direction = newDirection;
};
// 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
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.segments.push(newSegment);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var snake = new Snake();
var powerUp = new PowerUp();
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize the snake and power-up
game.addChild(snake);
snake.init(5); // Start with 5 segments
game.addChild(powerUp);
powerUp.randomizePosition();
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (game_position.x < snake.segments[0].x) {
snake.changeDirection({
x: -1,
y: 0
}); // Move left
} else if (game_position.x > snake.segments[0].x) {
snake.changeDirection({
x: 1,
y: 0
}); // Move right
} else if (game_position.y < snake.segments[0].y) {
snake.changeDirection({
x: 0,
y: -1
}); // Move up
} else {
snake.changeDirection({
x: 0,
y: 1
}); // Move down
}
};
// Update game logic
game.update = function () {
snake.update();
// Check for collision with power-up
if (snake.segments[0].intersects(powerUp)) {
snake.grow();
powerUp.randomizePosition();
score += 10;
scoreTxt.setText('Score: ' + score);
}
// Check for collision with walls
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();
}
// Check for collision with itself
for (var i = 1; i < snake.segments.length; i++) {
if (head.intersects(snake.segments[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};