/**** * Classes ****/ // Snake segment class var SnakeSegment = Container.expand(function () { var self = Container.call(this); var segmentGraphic = self.createAsset('snakeSegment', 'Snake Segment', 0.5, 0.5); }); // Food class var Food = Container.expand(function () { var self = Container.call(this); var foodGraphic = self.createAsset('food', 'Food', 0.5, 0.5); self.x = Math.random() * (2048 - foodGraphic.width) + foodGraphic.width / 2; self.y = Math.random() * (2732 - foodGraphic.height) + foodGraphic.height / 2; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Game variables var snake = []; var food; var direction = { x: 0, y: 0 }; var nextDirection = { x: 0, y: 0 }; var snakeSpeed = 5; var updateInterval = 1000 / 60; var lastUpdateTime = 0; var isGameOver = false; // Initialize snake function initSnake() { var initialLength = 5; for (var i = 0; i < initialLength; i++) { var segment = new SnakeSegment(); segment.x = 1024; segment.y = 1366 + i * segment.height; snake.push(segment); game.addChild(segment); } direction = { x: 0, y: -1 }; } // Initialize food function initFood() { food = new Food(); game.addChild(food); } // Check collision with food function checkFoodCollision() { var head = snake[0]; if (head.intersects(food)) { food.destroy(); addSnakeSegment(); initFood(); } } // Add new segment to snake function addSnakeSegment() { var tail = snake[snake.length - 1]; var newSegment = new SnakeSegment(); newSegment.x = tail.x; newSegment.y = tail.y; snake.push(newSegment); game.addChild(newSegment); } // Check self collision function checkSelfCollision() { var head = snake[0]; for (var i = 1; i < snake.length; i++) { if (head.intersects(snake[i])) { isGameOver = true; break; } } } // Update snake position function updateSnakePosition() { var head = snake[0]; var newHead = new SnakeSegment(); newHead.x = head.x + direction.x * head.width; newHead.y = head.y + direction.y * head.height; snake.unshift(newHead); game.addChild(newHead); var oldTail = snake.pop(); oldTail.destroy(); } // Handle touch input for direction change function handleTouchInput(obj) { var touchPos = obj.event.getLocalPosition(game); var head = snake[0]; var diffX = touchPos.x - head.x; var diffY = touchPos.y - head.y; if (Math.abs(diffX) > Math.abs(diffY)) { nextDirection = { x: diffX > 0 ? 1 : -1, y: 0 }; } else { nextDirection = { x: 0, y: diffY > 0 ? 1 : -1 }; } } // Game tick update LK.on('tick', function () { if (isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } var currentTime = Date.now(); if (currentTime - lastUpdateTime > updateInterval) { if (direction.x !== -nextDirection.x || direction.y !== -nextDirection.y) { direction = nextDirection; } updateSnakePosition(); checkFoodCollision(); checkSelfCollision(); lastUpdateTime = currentTime; } }); // Touch input listener game.on('down', handleTouchInput); // Initialize game elements initSnake(); initFood();
/****
* Classes
****/
// Snake segment class
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var segmentGraphic = self.createAsset('snakeSegment', 'Snake Segment', 0.5, 0.5);
});
// Food class
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphic = self.createAsset('food', 'Food', 0.5, 0.5);
self.x = Math.random() * (2048 - foodGraphic.width) + foodGraphic.width / 2;
self.y = Math.random() * (2732 - foodGraphic.height) + foodGraphic.height / 2;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Game variables
var snake = [];
var food;
var direction = {
x: 0,
y: 0
};
var nextDirection = {
x: 0,
y: 0
};
var snakeSpeed = 5;
var updateInterval = 1000 / 60;
var lastUpdateTime = 0;
var isGameOver = false;
// Initialize snake
function initSnake() {
var initialLength = 5;
for (var i = 0; i < initialLength; i++) {
var segment = new SnakeSegment();
segment.x = 1024;
segment.y = 1366 + i * segment.height;
snake.push(segment);
game.addChild(segment);
}
direction = {
x: 0,
y: -1
};
}
// Initialize food
function initFood() {
food = new Food();
game.addChild(food);
}
// Check collision with food
function checkFoodCollision() {
var head = snake[0];
if (head.intersects(food)) {
food.destroy();
addSnakeSegment();
initFood();
}
}
// Add new segment to snake
function addSnakeSegment() {
var tail = snake[snake.length - 1];
var newSegment = new SnakeSegment();
newSegment.x = tail.x;
newSegment.y = tail.y;
snake.push(newSegment);
game.addChild(newSegment);
}
// Check self collision
function checkSelfCollision() {
var head = snake[0];
for (var i = 1; i < snake.length; i++) {
if (head.intersects(snake[i])) {
isGameOver = true;
break;
}
}
}
// Update snake position
function updateSnakePosition() {
var head = snake[0];
var newHead = new SnakeSegment();
newHead.x = head.x + direction.x * head.width;
newHead.y = head.y + direction.y * head.height;
snake.unshift(newHead);
game.addChild(newHead);
var oldTail = snake.pop();
oldTail.destroy();
}
// Handle touch input for direction change
function handleTouchInput(obj) {
var touchPos = obj.event.getLocalPosition(game);
var head = snake[0];
var diffX = touchPos.x - head.x;
var diffY = touchPos.y - head.y;
if (Math.abs(diffX) > Math.abs(diffY)) {
nextDirection = {
x: diffX > 0 ? 1 : -1,
y: 0
};
} else {
nextDirection = {
x: 0,
y: diffY > 0 ? 1 : -1
};
}
}
// Game tick update
LK.on('tick', function () {
if (isGameOver) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
var currentTime = Date.now();
if (currentTime - lastUpdateTime > updateInterval) {
if (direction.x !== -nextDirection.x || direction.y !== -nextDirection.y) {
direction = nextDirection;
}
updateSnakePosition();
checkFoodCollision();
checkSelfCollision();
lastUpdateTime = currentTime;
}
});
// Touch input listener
game.on('down', handleTouchInput);
// Initialize game elements
initSnake();
initFood();