/****
* Classes
****/
// Food class for items the snake can eat
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the food on the screen
self.randomizePosition = function () {
var lastPosition = {
x: self.x,
y: self.y
};
do {
self.x = Math.floor(Math.random() * (game.width - 24 * self.width)) + game.x + 12 * self.width;
self.y = Math.floor(Math.random() * (game.height - 24 * self.height)) + game.y + 12 * self.height;
} while (!game.intersects(self) || self.y < game.y + game.height / 2 || Math.abs(self.x - lastPosition.x) < 200 && Math.abs(self.y - lastPosition.y) < 200);
};
return self;
});
// Food Counter class for displaying the number of food items consumed
var FoodCounter = Container.expand(function () {
var self = Container.call(this);
var counter = 0;
var counterText = new Text2(counter.toString(), {
size: 150,
fill: 0x000000
});
counterText.anchor.set(0.5, 0);
self.addChild(counterText);
self.updateCounter = function (newCount) {
counter = newCount;
counterText.setText(counter.toString());
};
self.update = function () {
counterText.x = 2048 / 2;
counterText.y = 2732 / 2 - 1000;
};
return self;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Snake class for both player and computer-controlled snakes
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 1,
y: 0
}; // Initial direction to the right
self.speed = 1.25; // Speed of the snake
// Initialize snake with a given length
self.init = function (length, startX, startY) {
for (var i = 0; i < length; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
x: startX - i * 20,
y: startY
});
segment.lastX = segment.x;
segment.lastY = segment.y;
self.segments.push(segment);
}
};
// Update snake position
self.update = function () {
// Store the previous position of the head
if (self.lastX === undefined) {
self.lastX = self.segments[0].x;
}
if (self.lastY === undefined) {
self.lastY = self.segments[0].y;
}
var prevX = self.lastX;
var prevY = self.lastY;
self.lastX = self.segments[0].x;
self.lastY = self.segments[0].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;
// Move each segment to the position of the previous one
for (var i = 1; i < self.segments.length; i++) {
var tempX = self.segments[i].x;
var tempY = self.segments[i].y;
self.segments[i].x = Math.max(0, Math.min(2048, prevX));
self.segments[i].y = Math.max(0, Math.min(2732, prevY));
self.segments[i].lastX = self.segments[i].x;
self.segments[i].lastY = self.segments[i].y;
prevX = tempX;
prevY = tempY;
}
// Check if the last segment is off the screen
var lastSegment = self.segments[self.segments.length - 1];
if (lastSegment.x < 0 || lastSegment.x > 2048 || lastSegment.y < 0 || lastSegment.y > 2732) {
lastSegment.x = Math.max(0, Math.min(2048, lastSegment.x)); // Ensure x is within bounds
lastSegment.y = Math.max(0, Math.min(2732, lastSegment.y)); // Ensure y is within bounds
// Remove the last segment from the snake and return it to the pool
var removedSegment = self.segments.pop();
self.removeChild(removedSegment); // Remove from display
self.segmentPool.push(removedSegment);
}
};
// Initialize an object pool for the snake segments
self.segmentPool = [];
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.segments[self.segments.length - 1];
var newSegment;
// Check if there are any segments in the pool
if (self.segmentPool.length > 0) {
// Reuse a segment from the pool
newSegment = self.segmentPool.pop();
newSegment.x = Math.max(0, Math.min(2048, lastSegment.x));
newSegment.y = Math.max(0, Math.min(2732, lastSegment.y));
// Initialize last positions for pooled segment
newSegment.lastX = newSegment.x;
newSegment.lastY = newSegment.y;
// Re-add the pooled segment to the display
self.addChild(newSegment);
} else {
// Create a new segment
newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
x: lastSegment.x,
y: lastSegment.y
});
// Initialize last positions for new segment
newSegment.lastX = newSegment.x;
newSegment.lastY = newSegment.y;
}
self.segments.push(newSegment);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Change background color to black
});
/****
* Game Code
****/
// Initialize screen
var screen = game.attachAsset('screen', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 - 200,
color: 0x8FBC8F // Beige green color
});
game.addChild(screen);
// Initialize phone
var phone = game.attachAsset('phone', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 + 900
});
game.addChild(phone);
// Initialize transparent square
var fence = game.attachAsset('fence', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 - 200
});
game.addChild(fence);
// Define fence coordinates
var fenceCoordinates = {
topLeft: {
x: fence.x - fence.width / 2,
y: fence.y - fence.height / 2
},
topRight: {
x: fence.x + fence.width / 2,
y: fence.y - fence.height / 2
},
bottomLeft: {
x: fence.x - fence.width / 2,
y: fence.y + fence.height / 2
},
bottomRight: {
x: fence.x + fence.width / 2,
y: fence.y + fence.height / 2
}
};
// Initialize player snake
var playerSnake = new Snake();
playerSnake.init(5, fence.x, fence.y);
game.addChild(playerSnake);
// Initialize food
var food = new Food();
food.x = fence.x;
food.y = fence.y;
game.addChild(food);
// Initialize food counter
var foodCounter = new FoodCounter();
foodCounter.updateCounter(0); // Start the counter from 0
game.addChild(foodCounter);
// Ensure that every game start and restart contains minimum 1 food
LK.on('restart', function () {
food = new Food();
food.x = fence.x;
food.y = fence.y;
game.addChild(food);
});
// Handle game updates
game.update = function () {
if (LK.ticks % 1 == 0) {
// Update every 60Hz
playerSnake.update();
// Check if player snake is at the edge of the fence
for (var i = 0; i < playerSnake.segments.length; i++) {
if (!fence.intersects(playerSnake.segments[i])) {
// If the snake is at the right edge of the fence, load it to the left edge
if (playerSnake.segments[i].x > fence.x + fence.width / 2) {
playerSnake.segments[i].x = fence.x - fence.width / 2;
}
// If the snake is at the left edge of the fence, load it to the right edge
else if (playerSnake.segments[i].x < fence.x - fence.width / 2) {
playerSnake.segments[i].x = fence.x + fence.width / 2;
}
// If the snake is at the top edge of the fence, load it to the bottom edge
else if (playerSnake.segments[i].y < fence.y - fence.height / 2) {
playerSnake.segments[i].y = fence.y + fence.height / 2;
}
// If the snake is at the bottom edge of the fence, load it to the top edge
else if (playerSnake.segments[i].y > fence.y + fence.height / 2) {
playerSnake.segments[i].y = fence.y - fence.height / 2;
}
}
}
// Check for collision with food
if (playerSnake.segments[0].intersects(food)) {
// Grow snake by 5 segments (one by one counting)
for (var g = 0; g < 5; g++) {
playerSnake.grow();
}
food.randomizePosition();
foodCounter.updateCounter(playerSnake.segments.length - 5); // Update the food counter
}
}
};
// Handle player input for snake direction
game.down = function (x, y, obj) {
var head = playerSnake.segments[0];
if (Math.abs(x - head.x) > Math.abs(y - head.y)) {
if (x > head.x) {
playerSnake.direction = {
x: 1,
y: 0
};
} else if (x < head.x) {
playerSnake.direction = {
x: -1,
y: 0
};
}
} else {
if (y > head.y) {
playerSnake.direction = {
x: 0,
y: 1
};
} else if (y < head.y) {
playerSnake.direction = {
x: 0,
y: -1
};
}
}
console.log("Game was clicked at", x, y);
};
; /****
* Classes
****/
// Food class for items the snake can eat
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the food on the screen
self.randomizePosition = function () {
var lastPosition = {
x: self.x,
y: self.y
};
do {
self.x = Math.floor(Math.random() * (game.width - 24 * self.width)) + game.x + 12 * self.width;
self.y = Math.floor(Math.random() * (game.height - 24 * self.height)) + game.y + 12 * self.height;
} while (!game.intersects(self) || self.y < game.y + game.height / 2 || Math.abs(self.x - lastPosition.x) < 200 && Math.abs(self.y - lastPosition.y) < 200);
};
return self;
});
// Food Counter class for displaying the number of food items consumed
var FoodCounter = Container.expand(function () {
var self = Container.call(this);
var counter = 0;
var counterText = new Text2(counter.toString(), {
size: 150,
fill: 0x000000
});
counterText.anchor.set(0.5, 0);
self.addChild(counterText);
self.updateCounter = function (newCount) {
counter = newCount;
counterText.setText(counter.toString());
};
self.update = function () {
counterText.x = 2048 / 2;
counterText.y = 2732 / 2 - 1000;
};
return self;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Snake class for both player and computer-controlled snakes
var Snake = Container.expand(function () {
var self = Container.call(this);
self.segments = [];
self.direction = {
x: 1,
y: 0
}; // Initial direction to the right
self.speed = 1.25; // Speed of the snake
// Initialize snake with a given length
self.init = function (length, startX, startY) {
for (var i = 0; i < length; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
x: startX - i * 20,
y: startY
});
segment.lastX = segment.x;
segment.lastY = segment.y;
self.segments.push(segment);
}
};
// Update snake position
self.update = function () {
// Store the previous position of the head
if (self.lastX === undefined) {
self.lastX = self.segments[0].x;
}
if (self.lastY === undefined) {
self.lastY = self.segments[0].y;
}
var prevX = self.lastX;
var prevY = self.lastY;
self.lastX = self.segments[0].x;
self.lastY = self.segments[0].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;
// Move each segment to the position of the previous one
for (var i = 1; i < self.segments.length; i++) {
var tempX = self.segments[i].x;
var tempY = self.segments[i].y;
self.segments[i].x = Math.max(0, Math.min(2048, prevX));
self.segments[i].y = Math.max(0, Math.min(2732, prevY));
self.segments[i].lastX = self.segments[i].x;
self.segments[i].lastY = self.segments[i].y;
prevX = tempX;
prevY = tempY;
}
// Check if the last segment is off the screen
var lastSegment = self.segments[self.segments.length - 1];
if (lastSegment.x < 0 || lastSegment.x > 2048 || lastSegment.y < 0 || lastSegment.y > 2732) {
lastSegment.x = Math.max(0, Math.min(2048, lastSegment.x)); // Ensure x is within bounds
lastSegment.y = Math.max(0, Math.min(2732, lastSegment.y)); // Ensure y is within bounds
// Remove the last segment from the snake and return it to the pool
var removedSegment = self.segments.pop();
self.removeChild(removedSegment); // Remove from display
self.segmentPool.push(removedSegment);
}
};
// Initialize an object pool for the snake segments
self.segmentPool = [];
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.segments[self.segments.length - 1];
var newSegment;
// Check if there are any segments in the pool
if (self.segmentPool.length > 0) {
// Reuse a segment from the pool
newSegment = self.segmentPool.pop();
newSegment.x = Math.max(0, Math.min(2048, lastSegment.x));
newSegment.y = Math.max(0, Math.min(2732, lastSegment.y));
// Initialize last positions for pooled segment
newSegment.lastX = newSegment.x;
newSegment.lastY = newSegment.y;
// Re-add the pooled segment to the display
self.addChild(newSegment);
} else {
// Create a new segment
newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5,
x: lastSegment.x,
y: lastSegment.y
});
// Initialize last positions for new segment
newSegment.lastX = newSegment.x;
newSegment.lastY = newSegment.y;
}
self.segments.push(newSegment);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Change background color to black
});
/****
* Game Code
****/
// Initialize screen
var screen = game.attachAsset('screen', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 - 200,
color: 0x8FBC8F // Beige green color
});
game.addChild(screen);
// Initialize phone
var phone = game.attachAsset('phone', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 + 900
});
game.addChild(phone);
// Initialize transparent square
var fence = game.attachAsset('fence', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 - 200
});
game.addChild(fence);
// Define fence coordinates
var fenceCoordinates = {
topLeft: {
x: fence.x - fence.width / 2,
y: fence.y - fence.height / 2
},
topRight: {
x: fence.x + fence.width / 2,
y: fence.y - fence.height / 2
},
bottomLeft: {
x: fence.x - fence.width / 2,
y: fence.y + fence.height / 2
},
bottomRight: {
x: fence.x + fence.width / 2,
y: fence.y + fence.height / 2
}
};
// Initialize player snake
var playerSnake = new Snake();
playerSnake.init(5, fence.x, fence.y);
game.addChild(playerSnake);
// Initialize food
var food = new Food();
food.x = fence.x;
food.y = fence.y;
game.addChild(food);
// Initialize food counter
var foodCounter = new FoodCounter();
foodCounter.updateCounter(0); // Start the counter from 0
game.addChild(foodCounter);
// Ensure that every game start and restart contains minimum 1 food
LK.on('restart', function () {
food = new Food();
food.x = fence.x;
food.y = fence.y;
game.addChild(food);
});
// Handle game updates
game.update = function () {
if (LK.ticks % 1 == 0) {
// Update every 60Hz
playerSnake.update();
// Check if player snake is at the edge of the fence
for (var i = 0; i < playerSnake.segments.length; i++) {
if (!fence.intersects(playerSnake.segments[i])) {
// If the snake is at the right edge of the fence, load it to the left edge
if (playerSnake.segments[i].x > fence.x + fence.width / 2) {
playerSnake.segments[i].x = fence.x - fence.width / 2;
}
// If the snake is at the left edge of the fence, load it to the right edge
else if (playerSnake.segments[i].x < fence.x - fence.width / 2) {
playerSnake.segments[i].x = fence.x + fence.width / 2;
}
// If the snake is at the top edge of the fence, load it to the bottom edge
else if (playerSnake.segments[i].y < fence.y - fence.height / 2) {
playerSnake.segments[i].y = fence.y + fence.height / 2;
}
// If the snake is at the bottom edge of the fence, load it to the top edge
else if (playerSnake.segments[i].y > fence.y + fence.height / 2) {
playerSnake.segments[i].y = fence.y - fence.height / 2;
}
}
}
// Check for collision with food
if (playerSnake.segments[0].intersects(food)) {
// Grow snake by 5 segments (one by one counting)
for (var g = 0; g < 5; g++) {
playerSnake.grow();
}
food.randomizePosition();
foodCounter.updateCounter(playerSnake.segments.length - 5); // Update the food counter
}
}
};
// Handle player input for snake direction
game.down = function (x, y, obj) {
var head = playerSnake.segments[0];
if (Math.abs(x - head.x) > Math.abs(y - head.y)) {
if (x > head.x) {
playerSnake.direction = {
x: 1,
y: 0
};
} else if (x < head.x) {
playerSnake.direction = {
x: -1,
y: 0
};
}
} else {
if (y > head.y) {
playerSnake.direction = {
x: 0,
y: 1
};
} else if (y < head.y) {
playerSnake.direction = {
x: 0,
y: -1
};
}
}
console.log("Game was clicked at", x, y);
};
;