/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Food = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
self.gridX = 0;
self.gridY = 0;
// Pulse animation
self.pulseDirection = 1;
self.pulseSpeed = 0.02;
self.update = function () {
self.scaleX += self.pulseSpeed * self.pulseDirection;
self.scaleY += self.pulseSpeed * self.pulseDirection;
if (self.scaleX >= 1.2) {
self.pulseDirection = -1;
} else if (self.scaleX <= 0.8) {
self.pulseDirection = 1;
}
};
return self;
});
var SnakeSegment = Container.expand(function (isHead) {
var self = Container.call(this);
if (isHead) {
var graphics = self.attachAsset('snakeHead', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
var graphics = self.attachAsset('snakeBody', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.gridX = 0;
self.gridY = 0;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Game constants
var GRID_SIZE = 40;
var GRID_WIDTH = Math.floor(2048 / GRID_SIZE);
var GRID_HEIGHT = Math.floor(2732 / GRID_SIZE);
var GAME_SPEED = 8; // Lower number = faster game
// Game variables
var snake = [];
var direction = 'right';
var nextDirection = 'right';
var food = null;
var gameRunning = true;
var moveCounter = 0;
var gameStarted = false;
// UI
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var instructionTxt = new Text2('Swipe to move the snake', {
size: 60,
fill: 0x888888
});
instructionTxt.anchor.set(0.5, 0.5);
instructionTxt.y = 200;
LK.gui.center.addChild(instructionTxt);
// Initialize snake
function initializeSnake() {
// Clear existing snake
for (var i = 0; i < snake.length; i++) {
snake[i].destroy();
}
snake = [];
// Create initial snake with 3 segments
var startX = Math.floor(GRID_WIDTH / 2);
var startY = Math.floor(GRID_HEIGHT / 2);
for (var i = 0; i < 3; i++) {
var segment = new SnakeSegment(i === 0);
segment.gridX = startX - i;
segment.gridY = startY;
segment.x = segment.gridX * GRID_SIZE + GRID_SIZE / 2;
segment.y = segment.gridY * GRID_SIZE + GRID_SIZE / 2;
snake.push(segment);
game.addChild(segment);
}
}
// Create food
function createFood() {
if (food) {
food.destroy();
}
var validPositions = [];
// Find valid positions (not occupied by snake)
for (var x = 1; x < GRID_WIDTH - 1; x++) {
for (var y = 1; y < GRID_HEIGHT - 1; y++) {
var isOccupied = false;
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === x && snake[i].gridY === y) {
isOccupied = true;
break;
}
}
if (!isOccupied) {
validPositions.push({
x: x,
y: y
});
}
}
}
if (validPositions.length > 0) {
var randomPos = validPositions[Math.floor(Math.random() * validPositions.length)];
food = new Food();
food.gridX = randomPos.x;
food.gridY = randomPos.y;
food.x = food.gridX * GRID_SIZE + GRID_SIZE / 2;
food.y = food.gridY * GRID_SIZE + GRID_SIZE / 2;
game.addChild(food);
}
}
// Move snake
function moveSnake() {
if (!gameRunning) return;
direction = nextDirection;
var head = snake[0];
var newX = head.gridX;
var newY = head.gridY;
// Calculate new head position
switch (direction) {
case 'up':
newY--;
break;
case 'down':
newY++;
break;
case 'left':
newX--;
break;
case 'right':
newX++;
break;
}
// Check wall collision
if (newX < 0 || newX >= GRID_WIDTH || newY < 0 || newY >= GRID_HEIGHT) {
gameOver();
return;
}
// Check self collision
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === newX && snake[i].gridY === newY) {
gameOver();
return;
}
}
// Check food collision
var ateFood = false;
if (food && food.gridX === newX && food.gridY === newY) {
ateFood = true;
LK.getSound('eat').play();
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
food.destroy();
food = null;
// Check winning condition
if (LK.getScore() >= 1000) {
gameRunning = false;
LK.effects.flashScreen(0x00ff00, 1000);
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
return;
}
}
// Create new head
var newHead = new SnakeSegment(true);
newHead.gridX = newX;
newHead.gridY = newY;
newHead.x = newHead.gridX * GRID_SIZE + GRID_SIZE / 2;
newHead.y = newHead.gridY * GRID_SIZE + GRID_SIZE / 2;
snake.unshift(newHead);
game.addChild(newHead);
// Convert old head to body
if (snake.length > 1) {
var oldHead = snake[1];
oldHead.removeChildren();
oldHead.attachAsset('snakeBody', {
anchorX: 0.5,
anchorY: 0.5
});
}
// Remove tail if no food was eaten
if (!ateFood) {
var tail = snake.pop();
tail.destroy();
}
// Create new food if needed
if (!food) {
createFood();
}
}
function gameOver() {
gameRunning = false;
LK.getSound('gameOver').play();
LK.effects.flashScreen(0xff0000, 1000);
// Show custom "Failed" message
var failedTxt = new Text2('Failed', {
size: 120,
fill: 0xff0000
});
failedTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(failedTxt);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
// Touch controls
var startTouchX = 0;
var startTouchY = 0;
var minSwipeDistance = 50;
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
instructionTxt.visible = false;
}
startTouchX = x;
startTouchY = y;
};
game.up = function (x, y, obj) {
if (!gameRunning || !gameStarted) return;
var deltaX = x - startTouchX;
var deltaY = y - startTouchY;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance < minSwipeDistance) return;
var absX = Math.abs(deltaX);
var absY = Math.abs(deltaY);
if (absX > absY) {
// Horizontal swipe
if (deltaX > 0 && direction !== 'left') {
nextDirection = 'right';
} else if (deltaX < 0 && direction !== 'right') {
nextDirection = 'left';
}
} else {
// Vertical swipe
if (deltaY > 0 && direction !== 'up') {
nextDirection = 'down';
} else if (deltaY < 0 && direction !== 'down') {
nextDirection = 'up';
}
}
};
// Create border walls
function createBorders() {
// Top and bottom borders
for (var x = 0; x < GRID_WIDTH; x++) {
// Top border
var topWall = LK.getAsset('borderWall', {
anchorX: 0.5,
anchorY: 0.5
});
topWall.x = x * GRID_SIZE + GRID_SIZE / 2;
topWall.y = GRID_SIZE / 2;
game.addChild(topWall);
// Bottom border
var bottomWall = LK.getAsset('borderWall', {
anchorX: 0.5,
anchorY: 0.5
});
bottomWall.x = x * GRID_SIZE + GRID_SIZE / 2;
bottomWall.y = (GRID_HEIGHT - 1) * GRID_SIZE + GRID_SIZE / 2;
game.addChild(bottomWall);
}
// Left and right borders (excluding corners to avoid overlap)
for (var y = 1; y < GRID_HEIGHT - 1; y++) {
// Left border
var leftWall = LK.getAsset('borderWall', {
anchorX: 0.5,
anchorY: 0.5
});
leftWall.x = GRID_SIZE / 2;
leftWall.y = y * GRID_SIZE + GRID_SIZE / 2;
game.addChild(leftWall);
// Right border
var rightWall = LK.getAsset('borderWall', {
anchorX: 0.5,
anchorY: 0.5
});
rightWall.x = (GRID_WIDTH - 1) * GRID_SIZE + GRID_SIZE / 2;
rightWall.y = y * GRID_SIZE + GRID_SIZE / 2;
game.addChild(rightWall);
}
}
// Initialize game
createBorders();
initializeSnake();
createFood();
game.update = function () {
if (!gameRunning || !gameStarted) return;
moveCounter++;
if (moveCounter >= GAME_SPEED) {
moveCounter = 0;
moveSnake();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -179,8 +179,17 @@
LK.setScore(LK.getScore() + 10);
scoreTxt.setText('Score: ' + LK.getScore());
food.destroy();
food = null;
+ // Check winning condition
+ if (LK.getScore() >= 1000) {
+ gameRunning = false;
+ LK.effects.flashScreen(0x00ff00, 1000);
+ LK.setTimeout(function () {
+ LK.showYouWin();
+ }, 1000);
+ return;
+ }
}
// Create new head
var newHead = new SnakeSegment(true);
newHead.gridX = newX;
@@ -211,8 +220,15 @@
function gameOver() {
gameRunning = false;
LK.getSound('gameOver').play();
LK.effects.flashScreen(0xff0000, 1000);
+ // Show custom "Failed" message
+ var failedTxt = new Text2('Failed', {
+ size: 120,
+ fill: 0xff0000
+ });
+ failedTxt.anchor.set(0.5, 0.5);
+ LK.gui.center.addChild(failedTxt);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}