User prompt
Make the background black
User prompt
Make the border dark green color
User prompt
change the background color to white
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var localPos = game.toLocal(obj.parent.toGlobal(obj.position));' Line Number: 379
Code edit (1 edits merged)
Please save this source code
User prompt
Snake Survival
Initial prompt
🎮 Game Overview Snake Survival is a modern twist on the classic snake game with added strategy, health mechanics, and collectible power-ups. Your snake starts with 5 health points and must navigate a grid-based arena, collecting items and avoiding fatal mistakes. The controls are simple — use on-screen arrow buttons to guide the snake. But survival? That’s where your timing, planning, and decision-making come in. 🧠 Game Rules & Mechanics 🩺 Health System Start with 5 Hearts Losing all hearts results in Game Over Manage health wisely while aiming for high scores 🍎 Items Item Effect Red Apple 🍎 Snake grows by +1 segment. Standard score item. Green Apple 🍏 Poisoned! Lose -1 Heart when collected. Avoid when low on health! Gold Apple 🟡 Spawns more red apples on the field. A helpful bonus item. Egg 🥚 Rare spawn – Grants +5 Growth instantly. Worth hunting for! ⚠️ Game Over Triggers Hitting walls/edge of the screen Dashing into yourself Reaching 0 health 🎮 Controls Use on-screen arrow keys (Up, Down, Left, Right) to control the snake’s direction Game is click/tap-based, designed for mobile-friendly interaction Only one move is allowed at a time (like classic snake — no diagonal movement) 🧱 Gameplay Loop Spawn with a short snake (e.g. 3–4 segments) Navigate toward red apples to grow Avoid green apples unless you have enough health to tank them Use gold apples to increase red apple availability Rare eggs can be a game-changer — but appear infrequently The longer the snake, the harder the maneuvering — plan ahead!
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Apple = Container.expand(function (type) { var self = Container.call(this); var assetName = type + 'Apple'; var graphics = self.attachAsset(assetName, { anchorX: 0.5, anchorY: 0.5 }); self.type = type; self.gridX = 0; self.gridY = 0; return self; }); var ArrowButton = Container.expand(function (direction) { var self = Container.call(this); var graphics = self.attachAsset('arrowButton', { anchorX: 0.5, anchorY: 0.5 }); self.direction = direction; self.down = function (x, y, obj) { currentDirection = self.direction; graphics.alpha = 0.7; tween(graphics, { alpha: 1 }, { duration: 200 }); }; return self; }); var Egg = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('egg', { anchorX: 0.5, anchorY: 0.5 }); self.gridX = 0; self.gridY = 0; 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 ****/ var GRID_SIZE = 40; var GRID_WIDTH = 25; var GRID_HEIGHT = 35; var GAME_AREA_X = (2048 - GRID_WIDTH * GRID_SIZE) / 2; var GAME_AREA_Y = 200; var snake = []; var currentDirection = 'right'; var nextDirection = 'right'; var moveTimer = 0; var moveInterval = 15; var health = 5; var score = 0; var gameActive = true; var apples = []; var eggs = []; var hearts = []; // Initialize snake with 4 segments for (var i = 0; i < 4; i++) { var segment = new SnakeSegment(i === 0); segment.gridX = 12 - i; segment.gridY = 17; segment.x = GAME_AREA_X + segment.gridX * GRID_SIZE + GRID_SIZE / 2; segment.y = GAME_AREA_Y + segment.gridY * GRID_SIZE + GRID_SIZE / 2; snake.push(segment); game.addChild(segment); } // Create health hearts for (var i = 0; i < 5; i++) { var heart = LK.getAsset('heart', { anchorX: 0.5, anchorY: 0.5, x: 150 + i * 60, y: 100 }); hearts.push(heart); LK.gui.topLeft.addChild(heart); } // Create score display var scoreText = new Text2('Score: 0', { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); // Create arrow buttons var upButton = new ArrowButton('up'); upButton.x = 1700; upButton.y = 2200; game.addChild(upButton); var downButton = new ArrowButton('down'); downButton.x = 1700; downButton.y = 2400; game.addChild(downButton); var leftButton = new ArrowButton('left'); leftButton.x = 1580; leftButton.y = 2300; game.addChild(leftButton); var rightButton = new ArrowButton('right'); rightButton.x = 1820; rightButton.y = 2300; game.addChild(rightButton); function gridToWorldX(gridX) { return GAME_AREA_X + gridX * GRID_SIZE + GRID_SIZE / 2; } function gridToWorldY(gridY) { return GAME_AREA_Y + gridY * GRID_SIZE + GRID_SIZE / 2; } function isValidGridPosition(x, y) { return x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT; } function isSnakePosition(x, y) { for (var i = 0; i < snake.length; i++) { if (snake[i].gridX === x && snake[i].gridY === y) { return true; } } return false; } function isOccupiedPosition(x, y) { if (isSnakePosition(x, y)) return true; for (var i = 0; i < apples.length; i++) { if (apples[i].gridX === x && apples[i].gridY === y) { return true; } } for (var i = 0; i < eggs.length; i++) { if (eggs[i].gridX === x && eggs[i].gridY === y) { return true; } } return false; } function getRandomFreePosition() { var attempts = 0; var maxAttempts = 100; while (attempts < maxAttempts) { var x = Math.floor(Math.random() * GRID_WIDTH); var y = Math.floor(Math.random() * GRID_HEIGHT); if (!isOccupiedPosition(x, y)) { return { x: x, y: y }; } attempts++; } return null; } function spawnApple(type) { var pos = getRandomFreePosition(); if (!pos) return; var apple = new Apple(type); apple.gridX = pos.x; apple.gridY = pos.y; apple.x = gridToWorldX(pos.x); apple.y = gridToWorldY(pos.y); apples.push(apple); game.addChild(apple); } function spawnEgg() { var pos = getRandomFreePosition(); if (!pos) return; var egg = new Egg(); egg.gridX = pos.x; egg.gridY = pos.y; egg.x = gridToWorldX(pos.x); egg.y = gridToWorldY(pos.y); eggs.push(egg); game.addChild(egg); } function updateHealth(newHealth) { health = Math.max(0, Math.min(5, newHealth)); for (var i = 0; i < hearts.length; i++) { hearts[i].alpha = i < health ? 1 : 0.3; } if (health <= 0) { gameActive = false; LK.showGameOver(); } } function updateScore(points) { score += points; LK.setScore(score); scoreText.setText('Score: ' + score); } function moveSnake() { if (!gameActive) return; currentDirection = nextDirection; var head = snake[0]; var newX = head.gridX; var newY = head.gridY; switch (currentDirection) { case 'up': newY--; break; case 'down': newY++; break; case 'left': newX--; break; case 'right': newX++; break; } // Check wall collision if (!isValidGridPosition(newX, newY)) { gameActive = false; LK.showGameOver(); return; } // Check self collision if (isSnakePosition(newX, newY)) { gameActive = false; LK.showGameOver(); return; } var shouldGrow = false; var growthAmount = 1; // Check apple collision for (var i = apples.length - 1; i >= 0; i--) { var apple = apples[i]; if (apple.gridX === newX && apple.gridY === newY) { if (apple.type === 'red') { shouldGrow = true; updateScore(10); LK.getSound('eat').play(); } else if (apple.type === 'green') { updateHealth(health - 1); updateScore(5); LK.getSound('hurt').play(); } else if (apple.type === 'gold') { updateScore(25); LK.getSound('bonus').play(); // Spawn 3 red apples for (var j = 0; j < 3; j++) { spawnApple('red'); } } apple.destroy(); apples.splice(i, 1); break; } } // Check egg collision for (var i = eggs.length - 1; i >= 0; i--) { var egg = eggs[i]; if (egg.gridX === newX && egg.gridY === newY) { shouldGrow = true; growthAmount = 5; updateScore(50); LK.getSound('bonus').play(); egg.destroy(); eggs.splice(i, 1); break; } } // Move snake var newHead = new SnakeSegment(true); newHead.gridX = newX; newHead.gridY = newY; newHead.x = gridToWorldX(newX); newHead.y = gridToWorldY(newY); // Convert old head to body snake[0].destroy(); var oldHeadBody = new SnakeSegment(false); oldHeadBody.gridX = head.gridX; oldHeadBody.gridY = head.gridY; oldHeadBody.x = head.x; oldHeadBody.y = head.y; snake[0] = oldHeadBody; game.addChild(oldHeadBody); snake.unshift(newHead); game.addChild(newHead); if (!shouldGrow) { var tail = snake.pop(); tail.destroy(); } else { for (var i = 1; i < growthAmount; i++) { var extraSegment = new SnakeSegment(false); var lastSegment = snake[snake.length - 1]; extraSegment.gridX = lastSegment.gridX; extraSegment.gridY = lastSegment.gridY; extraSegment.x = lastSegment.x; extraSegment.y = lastSegment.y; snake.push(extraSegment); game.addChild(extraSegment); } } } // Create dark green border around game area var borderThickness = 10; var borderColor = 0x006400; // Dark green // Top border var topBorder = LK.getAsset('snakeBody', { anchorX: 0, anchorY: 0, scaleX: (GRID_WIDTH * GRID_SIZE + borderThickness * 2) / 40, scaleY: borderThickness / 40, x: GAME_AREA_X - borderThickness, y: GAME_AREA_Y - borderThickness }); topBorder.tint = borderColor; game.addChild(topBorder); // Bottom border var bottomBorder = LK.getAsset('snakeBody', { anchorX: 0, anchorY: 0, scaleX: (GRID_WIDTH * GRID_SIZE + borderThickness * 2) / 40, scaleY: borderThickness / 40, x: GAME_AREA_X - borderThickness, y: GAME_AREA_Y + GRID_HEIGHT * GRID_SIZE }); bottomBorder.tint = borderColor; game.addChild(bottomBorder); // Left border var leftBorder = LK.getAsset('snakeBody', { anchorX: 0, anchorY: 0, scaleX: borderThickness / 40, scaleY: GRID_HEIGHT * GRID_SIZE / 40, x: GAME_AREA_X - borderThickness, y: GAME_AREA_Y }); leftBorder.tint = borderColor; game.addChild(leftBorder); // Right border var rightBorder = LK.getAsset('snakeBody', { anchorX: 0, anchorY: 0, scaleX: borderThickness / 40, scaleY: GRID_HEIGHT * GRID_SIZE / 40, x: GAME_AREA_X + GRID_WIDTH * GRID_SIZE, y: GAME_AREA_Y }); rightBorder.tint = borderColor; game.addChild(rightBorder); // Initial spawns spawnApple('red'); spawnApple('red'); spawnApple('green'); game.update = function () { if (!gameActive) return; moveTimer++; if (moveTimer >= moveInterval) { moveTimer = 0; moveSnake(); } // Spawn new items occasionally if (LK.ticks % 300 === 0) { // Every 5 seconds if (Math.random() < 0.7) { spawnApple('red'); } else if (Math.random() < 0.8) { spawnApple('green'); } else if (Math.random() < 0.95) { spawnApple('gold'); } else { spawnEgg(); } } }; game.down = function (x, y, obj) { // Handle arrow button presses - use the x, y coordinates directly as they are already in game space // Check which direction was pressed based on position if (x > 1640 && x < 1760 && y > 2140 && y < 2260) { // Up button if (currentDirection !== 'down') { nextDirection = 'up'; } } else if (x > 1640 && x < 1760 && y > 2340 && y < 2460) { // Down button if (currentDirection !== 'up') { nextDirection = 'down'; } } else if (x > 1520 && x < 1640 && y > 2240 && y < 2360) { // Left button if (currentDirection !== 'right') { nextDirection = 'left'; } } else if (x > 1760 && x < 1880 && y > 2240 && y < 2360) { // Right button if (currentDirection !== 'left') { nextDirection = 'right'; } } };
===================================================================
--- original.js
+++ change.js
@@ -67,9 +67,9 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0xFFFFFF
+ backgroundColor: 0x000000
});
/****
* Game Code
Polygon. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
egg. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Golden apple. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Green apple. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Heart . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Green circle. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
green snake head. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat