/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var segmentGraphics = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var GRID_SIZE = 40;
var GRID_WIDTH = Math.floor(2048 / GRID_SIZE);
var GRID_HEIGHT = Math.floor(2732 / GRID_SIZE);
var INITIAL_SPEED = 8;
var MAX_SPEED = 15;
var snake = [];
var snakeDirection = {
x: 1,
y: 0
};
var nextDirection = {
x: 1,
y: 0
};
var food = null;
var score = 0;
var moveCounter = 0;
var currentSpeed = INITIAL_SPEED;
var gameActive = true;
var lastTouchX = null;
var lastTouchY = null;
// Initialize score display
var scoreTxt = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize snake with 3 segments
function initializeSnake() {
snake = [];
var startX = Math.floor(GRID_WIDTH / 2);
var startY = Math.floor(GRID_HEIGHT / 2);
for (var i = 0; i < 3; i++) {
var segment = game.addChild(new SnakeSegment());
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);
}
}
// Spawn food at random location
function spawnFood() {
if (food) {
food.destroy();
}
var foodGridX, foodGridY, isValid;
do {
isValid = true;
foodGridX = Math.floor(Math.random() * GRID_WIDTH);
foodGridY = Math.floor(Math.random() * GRID_HEIGHT);
// Check if food spawns on snake
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === foodGridX && snake[i].gridY === foodGridY) {
isValid = false;
break;
}
}
} while (!isValid);
food = game.addChild(new Food());
food.gridX = foodGridX;
food.gridY = foodGridY;
food.x = food.gridX * GRID_SIZE + GRID_SIZE / 2;
food.y = food.gridY * GRID_SIZE + GRID_SIZE / 2;
}
// Update score display
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
}
// Increase difficulty based on snake length
function updateDifficulty() {
currentSpeed = INITIAL_SPEED + Math.floor(snake.length / 5);
if (currentSpeed > MAX_SPEED) {
currentSpeed = MAX_SPEED;
}
}
// Handle swipe direction changes
game.down = function (x, y, obj) {
lastTouchX = x;
lastTouchY = y;
};
game.up = function (x, y, obj) {
if (lastTouchX === null || lastTouchY === null) {
return;
}
var deltaX = x - lastTouchX;
var deltaY = y - lastTouchY;
var absDeltaX = Math.abs(deltaX);
var absDeltaY = Math.abs(deltaY);
// Require minimum swipe distance
if (absDeltaX < 50 && absDeltaY < 50) {
lastTouchX = null;
lastTouchY = null;
return;
}
// Determine swipe direction
if (absDeltaX > absDeltaY) {
// Horizontal swipe
if (deltaX > 0 && snakeDirection.x !== -1) {
nextDirection = {
x: 1,
y: 0
};
} else if (deltaX < 0 && snakeDirection.x !== 1) {
nextDirection = {
x: -1,
y: 0
};
}
} else {
// Vertical swipe
if (deltaY > 0 && snakeDirection.y !== -1) {
nextDirection = {
x: 0,
y: 1
};
} else if (deltaY < 0 && snakeDirection.y !== 1) {
nextDirection = {
x: 0,
y: -1
};
}
}
lastTouchX = null;
lastTouchY = null;
};
// Main game update loop
game.update = function () {
if (!gameActive) {
return;
}
moveCounter++;
// Move snake at current speed
if (moveCounter >= 60 / currentSpeed) {
moveCounter = 0;
// Update direction
snakeDirection = nextDirection;
// Calculate new head position
var headGridX = snake[0].gridX + snakeDirection.x;
var headGridY = snake[0].gridY + snakeDirection.y;
// Check wall collision
if (headGridX < 0 || headGridX >= GRID_WIDTH || headGridY < 0 || headGridY >= GRID_HEIGHT) {
LK.getSound('crash').play();
gameActive = false;
LK.showGameOver();
return;
}
// Check self collision
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === headGridX && snake[i].gridY === headGridY) {
LK.getSound('crash').play();
gameActive = false;
LK.showGameOver();
return;
}
}
// Create new head segment
var newHead = game.addChild(new SnakeSegment());
newHead.gridX = headGridX;
newHead.gridY = headGridY;
newHead.x = newHead.gridX * GRID_SIZE + GRID_SIZE / 2;
newHead.y = newHead.gridY * GRID_SIZE + GRID_SIZE / 2;
snake.unshift(newHead);
// Check food collision
if (food && food.gridX === headGridX && food.gridY === headGridY) {
LK.getSound('eat').play();
score += 10;
LK.setScore(score);
updateScoreDisplay();
updateDifficulty();
spawnFood();
} else {
// Remove tail if not eating
var tail = snake.pop();
tail.destroy();
}
}
};
// Initialize game
initializeSnake();
spawnFood();
updateScoreDisplay(); /****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var segmentGraphics = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var GRID_SIZE = 40;
var GRID_WIDTH = Math.floor(2048 / GRID_SIZE);
var GRID_HEIGHT = Math.floor(2732 / GRID_SIZE);
var INITIAL_SPEED = 8;
var MAX_SPEED = 15;
var snake = [];
var snakeDirection = {
x: 1,
y: 0
};
var nextDirection = {
x: 1,
y: 0
};
var food = null;
var score = 0;
var moveCounter = 0;
var currentSpeed = INITIAL_SPEED;
var gameActive = true;
var lastTouchX = null;
var lastTouchY = null;
// Initialize score display
var scoreTxt = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize snake with 3 segments
function initializeSnake() {
snake = [];
var startX = Math.floor(GRID_WIDTH / 2);
var startY = Math.floor(GRID_HEIGHT / 2);
for (var i = 0; i < 3; i++) {
var segment = game.addChild(new SnakeSegment());
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);
}
}
// Spawn food at random location
function spawnFood() {
if (food) {
food.destroy();
}
var foodGridX, foodGridY, isValid;
do {
isValid = true;
foodGridX = Math.floor(Math.random() * GRID_WIDTH);
foodGridY = Math.floor(Math.random() * GRID_HEIGHT);
// Check if food spawns on snake
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === foodGridX && snake[i].gridY === foodGridY) {
isValid = false;
break;
}
}
} while (!isValid);
food = game.addChild(new Food());
food.gridX = foodGridX;
food.gridY = foodGridY;
food.x = food.gridX * GRID_SIZE + GRID_SIZE / 2;
food.y = food.gridY * GRID_SIZE + GRID_SIZE / 2;
}
// Update score display
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
}
// Increase difficulty based on snake length
function updateDifficulty() {
currentSpeed = INITIAL_SPEED + Math.floor(snake.length / 5);
if (currentSpeed > MAX_SPEED) {
currentSpeed = MAX_SPEED;
}
}
// Handle swipe direction changes
game.down = function (x, y, obj) {
lastTouchX = x;
lastTouchY = y;
};
game.up = function (x, y, obj) {
if (lastTouchX === null || lastTouchY === null) {
return;
}
var deltaX = x - lastTouchX;
var deltaY = y - lastTouchY;
var absDeltaX = Math.abs(deltaX);
var absDeltaY = Math.abs(deltaY);
// Require minimum swipe distance
if (absDeltaX < 50 && absDeltaY < 50) {
lastTouchX = null;
lastTouchY = null;
return;
}
// Determine swipe direction
if (absDeltaX > absDeltaY) {
// Horizontal swipe
if (deltaX > 0 && snakeDirection.x !== -1) {
nextDirection = {
x: 1,
y: 0
};
} else if (deltaX < 0 && snakeDirection.x !== 1) {
nextDirection = {
x: -1,
y: 0
};
}
} else {
// Vertical swipe
if (deltaY > 0 && snakeDirection.y !== -1) {
nextDirection = {
x: 0,
y: 1
};
} else if (deltaY < 0 && snakeDirection.y !== 1) {
nextDirection = {
x: 0,
y: -1
};
}
}
lastTouchX = null;
lastTouchY = null;
};
// Main game update loop
game.update = function () {
if (!gameActive) {
return;
}
moveCounter++;
// Move snake at current speed
if (moveCounter >= 60 / currentSpeed) {
moveCounter = 0;
// Update direction
snakeDirection = nextDirection;
// Calculate new head position
var headGridX = snake[0].gridX + snakeDirection.x;
var headGridY = snake[0].gridY + snakeDirection.y;
// Check wall collision
if (headGridX < 0 || headGridX >= GRID_WIDTH || headGridY < 0 || headGridY >= GRID_HEIGHT) {
LK.getSound('crash').play();
gameActive = false;
LK.showGameOver();
return;
}
// Check self collision
for (var i = 0; i < snake.length; i++) {
if (snake[i].gridX === headGridX && snake[i].gridY === headGridY) {
LK.getSound('crash').play();
gameActive = false;
LK.showGameOver();
return;
}
}
// Create new head segment
var newHead = game.addChild(new SnakeSegment());
newHead.gridX = headGridX;
newHead.gridY = headGridY;
newHead.x = newHead.gridX * GRID_SIZE + GRID_SIZE / 2;
newHead.y = newHead.gridY * GRID_SIZE + GRID_SIZE / 2;
snake.unshift(newHead);
// Check food collision
if (food && food.gridX === headGridX && food.gridY === headGridY) {
LK.getSound('eat').play();
score += 10;
LK.setScore(score);
updateScoreDisplay();
updateDifficulty();
spawnFood();
} else {
// Remove tail if not eating
var tail = snake.pop();
tail.destroy();
}
}
};
// Initialize game
initializeSnake();
spawnFood();
updateScoreDisplay();