/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1", { snakeColor: 65280, foodType: "apple" }); /**** * Classes ****/ var CustomizeScreen = Container.expand(function () { var self = Container.call(this); var titleText = new Text2('Costumes', { size: 80, fill: '#ffffff' }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 400; self.addChild(titleText); // Snake color options var colorLabel = new Text2('Snake Color:', { size: 50, fill: '#ffffff' }); colorLabel.anchor.set(0.5, 0.5); colorLabel.x = 2048 / 2; colorLabel.y = 600; self.addChild(colorLabel); var colors = [0xff0000, 0xffc0cb, 0xff8c00, 0x0000ff]; // red, pink, orange, blue var colorButtons = []; for (var i = 0; i < colors.length; i++) { var colorButton = LK.getAsset('colorButton' + i, { width: 80, height: 80, color: colors[i], shape: 'box' }); colorButton.x = 800 + i * 120; colorButton.y = 700; colorButton.anchor.set(0.5, 0.5); colorButton.colorValue = colors[i]; self.addChild(colorButton); colorButtons.push(colorButton); colorButton.down = function () { storage.snakeColor = this.colorValue; // Update existing snake segments if in game if (gameState === 'playing' && snake.length > 0) { for (var j = 0; j < snake.length; j++) { snake[j].setColor(this.colorValue); } } }; } // Food type options var foodLabel = new Text2('Food Type:', { size: 50, fill: '#ffffff' }); foodLabel.anchor.set(0.5, 0.5); foodLabel.x = 2048 / 2; foodLabel.y = 900; self.addChild(foodLabel); var foodTypes = ['apple', 'pineapple', 'watermelon', 'orange', 'grape', 'banana', 'cherry']; var foodButtons = []; var foodRows = Math.ceil(foodTypes.length / 4); for (var j = 0; j < foodTypes.length; j++) { var row = Math.floor(j / 4); var col = j % 4; var foodButton = new Food(foodTypes[j]); foodButton.x = 700 + col * 120; foodButton.y = 1000 + row * 80; foodButton.foodType = foodTypes[j]; foodButton.scaleX = 1.5; foodButton.scaleY = 1.5; self.addChild(foodButton); foodButtons.push(foodButton); foodButton.down = function () { storage.foodType = this.foodType; }; } var backButton = LK.getAsset('backButton', { width: 300, height: 100, color: 0x666666, shape: 'box' }); backButton.x = 2048 / 2; backButton.y = 1400; backButton.anchor.set(0.5, 0.5); self.addChild(backButton); var backText = new Text2('Back', { size: 50, fill: '#ffffff' }); backText.anchor.set(0.5, 0.5); backText.x = 2048 / 2; backText.y = 1400; self.addChild(backText); backButton.down = function () { gameState = 'menu'; showStartScreen(); }; return self; }); var Food = Container.expand(function (type) { var self = Container.call(this); var foodGraphics = self.attachAsset(type || 'apple', { 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 }); self.setColor = function (color) { segmentGraphics.tint = color; }; return self; }); var StartScreen = Container.expand(function () { var self = Container.call(this); var titleText = new Text2('Snake.TE', { size: 120, fill: '#ffffff' }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 800; self.addChild(titleText); var playButton = LK.getAsset('button', { width: 400, height: 100, color: 0x4CAF50, shape: 'box' }); playButton.x = 2048 / 2; playButton.y = 1200; playButton.anchor.set(0.5, 0.5); self.addChild(playButton); var playText = new Text2('Oyunu Başlat', { size: 50, fill: '#ffffff' }); playText.anchor.set(0.5, 0.5); playText.x = 2048 / 2; playText.y = 1200; self.addChild(playText); var customizeButton = LK.getAsset('customizeButton', { width: 400, height: 100, color: 0x2196F3, shape: 'box' }); customizeButton.x = 2048 / 2; customizeButton.y = 1340; customizeButton.anchor.set(0.5, 0.5); self.addChild(customizeButton); var customizeText = new Text2('Costumes', { size: 50, fill: '#ffffff' }); customizeText.anchor.set(0.5, 0.5); customizeText.x = 2048 / 2; customizeText.y = 1340; self.addChild(customizeText); playButton.down = function () { gameState = 'playing'; startGame(); }; customizeButton.down = function () { gameState = 'customize'; showCustomizeScreen(); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1a1a1a }); /**** * Game Code ****/ var gameState = 'menu'; // 'menu', 'customize', 'playing', 'gameOver' var snake = []; var food = null; var direction = { x: 1, y: 0 }; var nextDirection = { x: 1, y: 0 }; var score = 0; var gameSpeed = 8; var lastMoveTime = 0; var gridSize = 40; var gameWidth = 2048; var gameHeight = 2732; var startScreen = null; var customizeScreen = null; var scoreTxt = null; // Initialize UI elements with stylish score box var scoreBox = LK.getAsset('scoreBox', { width: 400, height: 120, color: 0x333333, shape: 'box' }); scoreBox.x = 2048 / 2; scoreBox.y = 150; LK.gui.top.addChild(scoreBox); scoreTxt = new Text2('Score: 0', { size: 70, fill: '#00ff00' }); scoreTxt.anchor.set(0.5, 0.5); scoreTxt.x = 2048 / 2; scoreTxt.y = 150; LK.gui.top.addChild(scoreTxt); function showStartScreen() { clearGame(); if (customizeScreen) { game.removeChild(customizeScreen); customizeScreen = null; } startScreen = game.addChild(new StartScreen()); scoreTxt.alpha = 0; } function showCustomizeScreen() { if (startScreen) { game.removeChild(startScreen); startScreen = null; } customizeScreen = game.addChild(new CustomizeScreen()); scoreTxt.alpha = 0; } function startGame() { clearGame(); if (startScreen) { game.removeChild(startScreen); startScreen = null; } // Initialize snake snake = []; for (var i = 0; i < 3; i++) { var segment = game.addChild(new SnakeSegment()); segment.x = gameWidth / 2 - i * gridSize; segment.y = gameHeight / 2; segment.setColor(storage.snakeColor); snake.push(segment); } // Reset game variables direction = { x: 1, y: 0 }; nextDirection = { x: 1, y: 0 }; score = 0; lastMoveTime = 0; // Create first food spawnFood(); // Show score scoreTxt.alpha = 1; updateScoreDisplay(); } function clearGame() { // Remove all snake segments for (var i = 0; i < snake.length; i++) { if (snake[i]) { snake[i].destroy(); } } snake = []; // Remove food if (food) { food.destroy(); food = null; } } function spawnFood() { if (food) { food.destroy(); } var validPosition = false; var foodX, foodY; while (!validPosition) { foodX = Math.floor(Math.random() * (gameWidth / gridSize)) * gridSize + gridSize / 2; foodY = Math.floor(Math.random() * (gameHeight / gridSize)) * gridSize + gridSize / 2; // Make sure food doesn't spawn on snake validPosition = true; for (var i = 0; i < snake.length; i++) { if (snake[i].x === foodX && snake[i].y === foodY) { validPosition = false; break; } } // Keep food within bounds if (foodX < gridSize / 2 || foodX > gameWidth - gridSize / 2 || foodY < gridSize / 2 || foodY > gameHeight - gridSize / 2) { validPosition = false; } } food = game.addChild(new Food(storage.foodType)); food.x = foodX; food.y = foodY; } function moveSnake() { direction = nextDirection; var head = snake[0]; var newX = head.x + direction.x * gridSize; var newY = head.y + direction.y * gridSize; // Check wall collision if (newX < 0 || newX >= gameWidth || newY < 0 || newY >= gameHeight) { gameOver(); return; } // Check self collision for (var i = 0; i < snake.length; i++) { if (snake[i].x === newX && snake[i].y === newY) { gameOver(); return; } } // Check food collision var ateFood = false; if (food && Math.abs(newX - food.x) < gridSize / 2 && Math.abs(newY - food.y) < gridSize / 2) { ateFood = true; score++; LK.setScore(score); updateScoreDisplay(); LK.getSound('eat').play(); spawnFood(); } // Create new head var newHead = game.addChild(new SnakeSegment()); newHead.x = newX; newHead.y = newY; newHead.setColor(storage.snakeColor); snake.unshift(newHead); // Remove tail if didn't eat food if (!ateFood) { var tail = snake.pop(); tail.destroy(); } } function gameOver() { LK.getSound('gameOver').play(); LK.setScore(score); LK.showGameOver(); } function updateScoreDisplay() { scoreTxt.setText('Score: ' + score); } // Touch controls var touchStartX = 0; var touchStartY = 0; var minSwipeDistance = 50; game.down = function (x, y, obj) { if (gameState !== 'playing') return; touchStartX = x; touchStartY = y; }; game.up = function (x, y, obj) { if (gameState !== 'playing') return; var deltaX = x - touchStartX; var deltaY = y - touchStartY; var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); if (distance < minSwipeDistance) return; if (Math.abs(deltaX) > Math.abs(deltaY)) { // Horizontal swipe if (deltaX > 0 && direction.x !== -1) { nextDirection = { x: 1, y: 0 }; } else if (deltaX < 0 && direction.x !== 1) { nextDirection = { x: -1, y: 0 }; } } else { // Vertical swipe if (deltaY > 0 && direction.y !== -1) { nextDirection = { x: 0, y: 1 }; } else if (deltaY < 0 && direction.y !== 1) { nextDirection = { x: 0, y: -1 }; } } }; game.update = function () { if (gameState === 'playing') { // Move snake at controlled speed if (LK.ticks - lastMoveTime >= gameSpeed) { moveSnake(); lastMoveTime = LK.ticks; } } }; // Start with menu screen showStartScreen();
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1", {
snakeColor: 65280,
foodType: "apple"
});
/****
* Classes
****/
var CustomizeScreen = Container.expand(function () {
var self = Container.call(this);
var titleText = new Text2('Costumes', {
size: 80,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 400;
self.addChild(titleText);
// Snake color options
var colorLabel = new Text2('Snake Color:', {
size: 50,
fill: '#ffffff'
});
colorLabel.anchor.set(0.5, 0.5);
colorLabel.x = 2048 / 2;
colorLabel.y = 600;
self.addChild(colorLabel);
var colors = [0xff0000, 0xffc0cb, 0xff8c00, 0x0000ff]; // red, pink, orange, blue
var colorButtons = [];
for (var i = 0; i < colors.length; i++) {
var colorButton = LK.getAsset('colorButton' + i, {
width: 80,
height: 80,
color: colors[i],
shape: 'box'
});
colorButton.x = 800 + i * 120;
colorButton.y = 700;
colorButton.anchor.set(0.5, 0.5);
colorButton.colorValue = colors[i];
self.addChild(colorButton);
colorButtons.push(colorButton);
colorButton.down = function () {
storage.snakeColor = this.colorValue;
// Update existing snake segments if in game
if (gameState === 'playing' && snake.length > 0) {
for (var j = 0; j < snake.length; j++) {
snake[j].setColor(this.colorValue);
}
}
};
}
// Food type options
var foodLabel = new Text2('Food Type:', {
size: 50,
fill: '#ffffff'
});
foodLabel.anchor.set(0.5, 0.5);
foodLabel.x = 2048 / 2;
foodLabel.y = 900;
self.addChild(foodLabel);
var foodTypes = ['apple', 'pineapple', 'watermelon', 'orange', 'grape', 'banana', 'cherry'];
var foodButtons = [];
var foodRows = Math.ceil(foodTypes.length / 4);
for (var j = 0; j < foodTypes.length; j++) {
var row = Math.floor(j / 4);
var col = j % 4;
var foodButton = new Food(foodTypes[j]);
foodButton.x = 700 + col * 120;
foodButton.y = 1000 + row * 80;
foodButton.foodType = foodTypes[j];
foodButton.scaleX = 1.5;
foodButton.scaleY = 1.5;
self.addChild(foodButton);
foodButtons.push(foodButton);
foodButton.down = function () {
storage.foodType = this.foodType;
};
}
var backButton = LK.getAsset('backButton', {
width: 300,
height: 100,
color: 0x666666,
shape: 'box'
});
backButton.x = 2048 / 2;
backButton.y = 1400;
backButton.anchor.set(0.5, 0.5);
self.addChild(backButton);
var backText = new Text2('Back', {
size: 50,
fill: '#ffffff'
});
backText.anchor.set(0.5, 0.5);
backText.x = 2048 / 2;
backText.y = 1400;
self.addChild(backText);
backButton.down = function () {
gameState = 'menu';
showStartScreen();
};
return self;
});
var Food = Container.expand(function (type) {
var self = Container.call(this);
var foodGraphics = self.attachAsset(type || 'apple', {
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
});
self.setColor = function (color) {
segmentGraphics.tint = color;
};
return self;
});
var StartScreen = Container.expand(function () {
var self = Container.call(this);
var titleText = new Text2('Snake.TE', {
size: 120,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 800;
self.addChild(titleText);
var playButton = LK.getAsset('button', {
width: 400,
height: 100,
color: 0x4CAF50,
shape: 'box'
});
playButton.x = 2048 / 2;
playButton.y = 1200;
playButton.anchor.set(0.5, 0.5);
self.addChild(playButton);
var playText = new Text2('Oyunu Başlat', {
size: 50,
fill: '#ffffff'
});
playText.anchor.set(0.5, 0.5);
playText.x = 2048 / 2;
playText.y = 1200;
self.addChild(playText);
var customizeButton = LK.getAsset('customizeButton', {
width: 400,
height: 100,
color: 0x2196F3,
shape: 'box'
});
customizeButton.x = 2048 / 2;
customizeButton.y = 1340;
customizeButton.anchor.set(0.5, 0.5);
self.addChild(customizeButton);
var customizeText = new Text2('Costumes', {
size: 50,
fill: '#ffffff'
});
customizeText.anchor.set(0.5, 0.5);
customizeText.x = 2048 / 2;
customizeText.y = 1340;
self.addChild(customizeText);
playButton.down = function () {
gameState = 'playing';
startGame();
};
customizeButton.down = function () {
gameState = 'customize';
showCustomizeScreen();
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var gameState = 'menu'; // 'menu', 'customize', 'playing', 'gameOver'
var snake = [];
var food = null;
var direction = {
x: 1,
y: 0
};
var nextDirection = {
x: 1,
y: 0
};
var score = 0;
var gameSpeed = 8;
var lastMoveTime = 0;
var gridSize = 40;
var gameWidth = 2048;
var gameHeight = 2732;
var startScreen = null;
var customizeScreen = null;
var scoreTxt = null;
// Initialize UI elements with stylish score box
var scoreBox = LK.getAsset('scoreBox', {
width: 400,
height: 120,
color: 0x333333,
shape: 'box'
});
scoreBox.x = 2048 / 2;
scoreBox.y = 150;
LK.gui.top.addChild(scoreBox);
scoreTxt = new Text2('Score: 0', {
size: 70,
fill: '#00ff00'
});
scoreTxt.anchor.set(0.5, 0.5);
scoreTxt.x = 2048 / 2;
scoreTxt.y = 150;
LK.gui.top.addChild(scoreTxt);
function showStartScreen() {
clearGame();
if (customizeScreen) {
game.removeChild(customizeScreen);
customizeScreen = null;
}
startScreen = game.addChild(new StartScreen());
scoreTxt.alpha = 0;
}
function showCustomizeScreen() {
if (startScreen) {
game.removeChild(startScreen);
startScreen = null;
}
customizeScreen = game.addChild(new CustomizeScreen());
scoreTxt.alpha = 0;
}
function startGame() {
clearGame();
if (startScreen) {
game.removeChild(startScreen);
startScreen = null;
}
// Initialize snake
snake = [];
for (var i = 0; i < 3; i++) {
var segment = game.addChild(new SnakeSegment());
segment.x = gameWidth / 2 - i * gridSize;
segment.y = gameHeight / 2;
segment.setColor(storage.snakeColor);
snake.push(segment);
}
// Reset game variables
direction = {
x: 1,
y: 0
};
nextDirection = {
x: 1,
y: 0
};
score = 0;
lastMoveTime = 0;
// Create first food
spawnFood();
// Show score
scoreTxt.alpha = 1;
updateScoreDisplay();
}
function clearGame() {
// Remove all snake segments
for (var i = 0; i < snake.length; i++) {
if (snake[i]) {
snake[i].destroy();
}
}
snake = [];
// Remove food
if (food) {
food.destroy();
food = null;
}
}
function spawnFood() {
if (food) {
food.destroy();
}
var validPosition = false;
var foodX, foodY;
while (!validPosition) {
foodX = Math.floor(Math.random() * (gameWidth / gridSize)) * gridSize + gridSize / 2;
foodY = Math.floor(Math.random() * (gameHeight / gridSize)) * gridSize + gridSize / 2;
// Make sure food doesn't spawn on snake
validPosition = true;
for (var i = 0; i < snake.length; i++) {
if (snake[i].x === foodX && snake[i].y === foodY) {
validPosition = false;
break;
}
}
// Keep food within bounds
if (foodX < gridSize / 2 || foodX > gameWidth - gridSize / 2 || foodY < gridSize / 2 || foodY > gameHeight - gridSize / 2) {
validPosition = false;
}
}
food = game.addChild(new Food(storage.foodType));
food.x = foodX;
food.y = foodY;
}
function moveSnake() {
direction = nextDirection;
var head = snake[0];
var newX = head.x + direction.x * gridSize;
var newY = head.y + direction.y * gridSize;
// Check wall collision
if (newX < 0 || newX >= gameWidth || newY < 0 || newY >= gameHeight) {
gameOver();
return;
}
// Check self collision
for (var i = 0; i < snake.length; i++) {
if (snake[i].x === newX && snake[i].y === newY) {
gameOver();
return;
}
}
// Check food collision
var ateFood = false;
if (food && Math.abs(newX - food.x) < gridSize / 2 && Math.abs(newY - food.y) < gridSize / 2) {
ateFood = true;
score++;
LK.setScore(score);
updateScoreDisplay();
LK.getSound('eat').play();
spawnFood();
}
// Create new head
var newHead = game.addChild(new SnakeSegment());
newHead.x = newX;
newHead.y = newY;
newHead.setColor(storage.snakeColor);
snake.unshift(newHead);
// Remove tail if didn't eat food
if (!ateFood) {
var tail = snake.pop();
tail.destroy();
}
}
function gameOver() {
LK.getSound('gameOver').play();
LK.setScore(score);
LK.showGameOver();
}
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
}
// Touch controls
var touchStartX = 0;
var touchStartY = 0;
var minSwipeDistance = 50;
game.down = function (x, y, obj) {
if (gameState !== 'playing') return;
touchStartX = x;
touchStartY = y;
};
game.up = function (x, y, obj) {
if (gameState !== 'playing') return;
var deltaX = x - touchStartX;
var deltaY = y - touchStartY;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
if (distance < minSwipeDistance) return;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
// Horizontal swipe
if (deltaX > 0 && direction.x !== -1) {
nextDirection = {
x: 1,
y: 0
};
} else if (deltaX < 0 && direction.x !== 1) {
nextDirection = {
x: -1,
y: 0
};
}
} else {
// Vertical swipe
if (deltaY > 0 && direction.y !== -1) {
nextDirection = {
x: 0,
y: 1
};
} else if (deltaY < 0 && direction.y !== 1) {
nextDirection = {
x: 0,
y: -1
};
}
}
};
game.update = function () {
if (gameState === 'playing') {
// Move snake at controlled speed
if (LK.ticks - lastMoveTime >= gameSpeed) {
moveSnake();
lastMoveTime = LK.ticks;
}
}
};
// Start with menu screen
showStartScreen();