User prompt
add a background looking like grass
User prompt
make snake faster after every big food
User prompt
add 3 body parts to snake when every time after big food eaten
User prompt
add a big food after every five food. and gain 30 score for it
Code edit (1 edits merged)
Please save this source code
User prompt
Snake Feast
Initial prompt
make a snake game
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var BigFood = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('bigFood', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Food = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeHead = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('snakeHead', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('snakeBody', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x001100
});
/****
* Game Code
****/
// Add grass background
var grassBackground = game.addChild(LK.getAsset('grass', {
x: 0,
y: 0
}));
// Game constants
var GRID_SIZE = 40;
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var MOVE_SPEED = 8; // pixels per frame
var FOOD_SPAWN_INTERVAL = 180; // frames (3 seconds at 60fps)
// Game boundaries
var BOUNDARY_LEFT = 100;
var BOUNDARY_RIGHT = GAME_WIDTH - 100;
var BOUNDARY_TOP = 200;
var BOUNDARY_BOTTOM = GAME_HEIGHT - 200;
// Create walls
var topWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT,
y: BOUNDARY_TOP - 20,
width: BOUNDARY_RIGHT - BOUNDARY_LEFT,
height: 40
}));
var bottomWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT,
y: BOUNDARY_BOTTOM - 20,
width: BOUNDARY_RIGHT - BOUNDARY_LEFT,
height: 40
}));
var leftWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT - 20,
y: BOUNDARY_TOP,
width: 40,
height: BOUNDARY_BOTTOM - BOUNDARY_TOP
}));
var rightWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_RIGHT - 20,
y: BOUNDARY_TOP,
width: 40,
height: BOUNDARY_BOTTOM - BOUNDARY_TOP
}));
// Snake variables
var snake = [];
var snakeDirection = {
x: MOVE_SPEED,
y: 0
};
var nextDirection = {
x: MOVE_SPEED,
y: 0
};
var moveAccumulator = 0;
var segmentsToAdd = 0;
// Food variables
var currentFood = null;
var foodSpawnTimer = 0;
var foodsEaten = 0;
var isBigFood = false;
// Game state
var gameRunning = true;
// UI
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
// Initialize snake
function initializeSnake() {
// Clear existing snake
for (var i = 0; i < snake.length; i++) {
snake[i].destroy();
}
snake = [];
// Create initial snake with head and 2 body segments
var startX = GAME_WIDTH / 2;
var startY = GAME_HEIGHT / 2;
// Head
var head = game.addChild(new SnakeHead());
head.x = startX;
head.y = startY;
snake.push(head);
// Body segments
for (var i = 1; i < 3; i++) {
var segment = game.addChild(new SnakeSegment());
segment.x = startX - i * GRID_SIZE;
segment.y = startY;
snake.push(segment);
}
// Reset direction
snakeDirection = {
x: MOVE_SPEED,
y: 0
};
nextDirection = {
x: MOVE_SPEED,
y: 0
};
moveAccumulator = 0;
segmentsToAdd = 0;
}
// Spawn food at random location
function spawnFood() {
if (currentFood) {
currentFood.destroy();
}
var attempts = 0;
var validPosition = false;
var foodX, foodY;
while (!validPosition && attempts < 100) {
foodX = BOUNDARY_LEFT + Math.floor(Math.random() * ((BOUNDARY_RIGHT - BOUNDARY_LEFT) / GRID_SIZE)) * GRID_SIZE + GRID_SIZE / 2;
foodY = BOUNDARY_TOP + Math.floor(Math.random() * ((BOUNDARY_BOTTOM - BOUNDARY_TOP) / GRID_SIZE)) * GRID_SIZE + GRID_SIZE / 2;
validPosition = true;
// Check if food spawns on snake
for (var i = 0; i < snake.length; i++) {
if (Math.abs(snake[i].x - foodX) < GRID_SIZE && Math.abs(snake[i].y - foodY) < GRID_SIZE) {
validPosition = false;
break;
}
}
attempts++;
}
if (validPosition) {
// Check if we should spawn big food (every 5th food)
if (foodsEaten > 0 && foodsEaten % 5 === 0) {
currentFood = game.addChild(new BigFood());
isBigFood = true;
} else {
currentFood = game.addChild(new Food());
isBigFood = false;
}
currentFood.x = foodX;
currentFood.y = foodY;
}
}
// Check collision with food
function checkFoodCollision() {
if (!currentFood || snake.length === 0) return false;
var head = snake[0];
var distance = Math.sqrt(Math.pow(head.x - currentFood.x, 2) + Math.pow(head.y - currentFood.y, 2));
if (distance < GRID_SIZE * 0.7) {
// Eat food
if (isBigFood) {
LK.setScore(LK.getScore() + 30);
// Add 3 segments for big food
segmentsToAdd += 3;
// Increase snake speed
MOVE_SPEED += 1;
} else {
LK.setScore(LK.getScore() + 10);
// Add 1 segment for regular food
segmentsToAdd++;
}
scoreTxt.setText('Score: ' + LK.getScore());
// Flash screen green
LK.effects.flashScreen(0x00ff00, 300);
// Play eat sound
LK.getSound('eat').play();
// Increment foods eaten counter
foodsEaten++;
// Remove food
currentFood.destroy();
currentFood = null;
isBigFood = false;
// Reset food spawn timer
foodSpawnTimer = 0;
return true;
}
return false;
}
// Check wall collision
function checkWallCollision() {
if (snake.length === 0) return false;
var head = snake[0];
return head.x <= BOUNDARY_LEFT || head.x >= BOUNDARY_RIGHT || head.y <= BOUNDARY_TOP || head.y >= BOUNDARY_BOTTOM;
}
// Check self collision
function checkSelfCollision() {
if (snake.length < 4) return false;
var head = snake[0];
for (var i = 3; i < snake.length; i++) {
var distance = Math.sqrt(Math.pow(head.x - snake[i].x, 2) + Math.pow(head.y - snake[i].y, 2));
if (distance < GRID_SIZE * 0.8) {
return true;
}
}
return false;
}
// Handle turn input
function turnSnake(direction) {
if (!gameRunning) return;
if (direction === 'left') {
// Turn left relative to current direction
if (snakeDirection.x > 0) {
// Moving right
nextDirection = {
x: 0,
y: -MOVE_SPEED
};
} else if (snakeDirection.x < 0) {
// Moving left
nextDirection = {
x: 0,
y: MOVE_SPEED
};
} else if (snakeDirection.y > 0) {
// Moving down
nextDirection = {
x: MOVE_SPEED,
y: 0
};
} else if (snakeDirection.y < 0) {
// Moving up
nextDirection = {
x: -MOVE_SPEED,
y: 0
};
}
} else if (direction === 'right') {
// Turn right relative to current direction
if (snakeDirection.x > 0) {
// Moving right
nextDirection = {
x: 0,
y: MOVE_SPEED
};
} else if (snakeDirection.x < 0) {
// Moving left
nextDirection = {
x: 0,
y: -MOVE_SPEED
};
} else if (snakeDirection.y > 0) {
// Moving down
nextDirection = {
x: -MOVE_SPEED,
y: 0
};
} else if (snakeDirection.y < 0) {
// Moving up
nextDirection = {
x: MOVE_SPEED,
y: 0
};
}
}
// Update current direction vectors to match new speed
if (snakeDirection.x > 0) {
snakeDirection.x = MOVE_SPEED;
} else if (snakeDirection.x < 0) {
snakeDirection.x = -MOVE_SPEED;
}
if (snakeDirection.y > 0) {
snakeDirection.y = MOVE_SPEED;
} else if (snakeDirection.y < 0) {
snakeDirection.y = -MOVE_SPEED;
}
}
// Touch controls
game.down = function (x, y, obj) {
if (!gameRunning) return;
// Determine if touch is on left or right side of screen
if (x < GAME_WIDTH / 2) {
turnSnake('left');
} else {
turnSnake('right');
}
};
// Initialize game
initializeSnake();
spawnFood();
// Main game loop
game.update = function () {
if (!gameRunning) return;
// Update direction
snakeDirection = nextDirection;
// Move snake
moveAccumulator += 1;
if (moveAccumulator >= 5) {
// Move every 5 frames for smoother movement
moveAccumulator = 0;
if (snake.length > 0) {
// Store previous head position
var prevX = snake[0].x;
var prevY = snake[0].y;
// Move head
snake[0].x += snakeDirection.x * 5;
snake[0].y += snakeDirection.y * 5;
// Move body segments
for (var i = 1; i < snake.length; i++) {
var tempX = snake[i].x;
var tempY = snake[i].y;
snake[i].x = prevX;
snake[i].y = prevY;
prevX = tempX;
prevY = tempY;
}
// Add new segments if needed
if (segmentsToAdd > 0) {
var newSegment = game.addChild(new SnakeSegment());
newSegment.x = prevX;
newSegment.y = prevY;
snake.push(newSegment);
segmentsToAdd--;
}
}
}
// Check collisions
if (checkWallCollision() || checkSelfCollision()) {
gameRunning = false;
LK.getSound('death').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
checkFoodCollision();
// Spawn food
foodSpawnTimer++;
if (foodSpawnTimer >= FOOD_SPAWN_INTERVAL && !currentFood) {
spawnFood();
foodSpawnTimer = 0;
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var BigFood = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('bigFood', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Food = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeHead = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('snakeHead', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var SnakeSegment = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('snakeBody', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x001100
});
/****
* Game Code
****/
// Add grass background
var grassBackground = game.addChild(LK.getAsset('grass', {
x: 0,
y: 0
}));
// Game constants
var GRID_SIZE = 40;
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var MOVE_SPEED = 8; // pixels per frame
var FOOD_SPAWN_INTERVAL = 180; // frames (3 seconds at 60fps)
// Game boundaries
var BOUNDARY_LEFT = 100;
var BOUNDARY_RIGHT = GAME_WIDTH - 100;
var BOUNDARY_TOP = 200;
var BOUNDARY_BOTTOM = GAME_HEIGHT - 200;
// Create walls
var topWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT,
y: BOUNDARY_TOP - 20,
width: BOUNDARY_RIGHT - BOUNDARY_LEFT,
height: 40
}));
var bottomWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT,
y: BOUNDARY_BOTTOM - 20,
width: BOUNDARY_RIGHT - BOUNDARY_LEFT,
height: 40
}));
var leftWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_LEFT - 20,
y: BOUNDARY_TOP,
width: 40,
height: BOUNDARY_BOTTOM - BOUNDARY_TOP
}));
var rightWall = game.addChild(LK.getAsset('wall', {
x: BOUNDARY_RIGHT - 20,
y: BOUNDARY_TOP,
width: 40,
height: BOUNDARY_BOTTOM - BOUNDARY_TOP
}));
// Snake variables
var snake = [];
var snakeDirection = {
x: MOVE_SPEED,
y: 0
};
var nextDirection = {
x: MOVE_SPEED,
y: 0
};
var moveAccumulator = 0;
var segmentsToAdd = 0;
// Food variables
var currentFood = null;
var foodSpawnTimer = 0;
var foodsEaten = 0;
var isBigFood = false;
// Game state
var gameRunning = true;
// UI
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
// Initialize snake
function initializeSnake() {
// Clear existing snake
for (var i = 0; i < snake.length; i++) {
snake[i].destroy();
}
snake = [];
// Create initial snake with head and 2 body segments
var startX = GAME_WIDTH / 2;
var startY = GAME_HEIGHT / 2;
// Head
var head = game.addChild(new SnakeHead());
head.x = startX;
head.y = startY;
snake.push(head);
// Body segments
for (var i = 1; i < 3; i++) {
var segment = game.addChild(new SnakeSegment());
segment.x = startX - i * GRID_SIZE;
segment.y = startY;
snake.push(segment);
}
// Reset direction
snakeDirection = {
x: MOVE_SPEED,
y: 0
};
nextDirection = {
x: MOVE_SPEED,
y: 0
};
moveAccumulator = 0;
segmentsToAdd = 0;
}
// Spawn food at random location
function spawnFood() {
if (currentFood) {
currentFood.destroy();
}
var attempts = 0;
var validPosition = false;
var foodX, foodY;
while (!validPosition && attempts < 100) {
foodX = BOUNDARY_LEFT + Math.floor(Math.random() * ((BOUNDARY_RIGHT - BOUNDARY_LEFT) / GRID_SIZE)) * GRID_SIZE + GRID_SIZE / 2;
foodY = BOUNDARY_TOP + Math.floor(Math.random() * ((BOUNDARY_BOTTOM - BOUNDARY_TOP) / GRID_SIZE)) * GRID_SIZE + GRID_SIZE / 2;
validPosition = true;
// Check if food spawns on snake
for (var i = 0; i < snake.length; i++) {
if (Math.abs(snake[i].x - foodX) < GRID_SIZE && Math.abs(snake[i].y - foodY) < GRID_SIZE) {
validPosition = false;
break;
}
}
attempts++;
}
if (validPosition) {
// Check if we should spawn big food (every 5th food)
if (foodsEaten > 0 && foodsEaten % 5 === 0) {
currentFood = game.addChild(new BigFood());
isBigFood = true;
} else {
currentFood = game.addChild(new Food());
isBigFood = false;
}
currentFood.x = foodX;
currentFood.y = foodY;
}
}
// Check collision with food
function checkFoodCollision() {
if (!currentFood || snake.length === 0) return false;
var head = snake[0];
var distance = Math.sqrt(Math.pow(head.x - currentFood.x, 2) + Math.pow(head.y - currentFood.y, 2));
if (distance < GRID_SIZE * 0.7) {
// Eat food
if (isBigFood) {
LK.setScore(LK.getScore() + 30);
// Add 3 segments for big food
segmentsToAdd += 3;
// Increase snake speed
MOVE_SPEED += 1;
} else {
LK.setScore(LK.getScore() + 10);
// Add 1 segment for regular food
segmentsToAdd++;
}
scoreTxt.setText('Score: ' + LK.getScore());
// Flash screen green
LK.effects.flashScreen(0x00ff00, 300);
// Play eat sound
LK.getSound('eat').play();
// Increment foods eaten counter
foodsEaten++;
// Remove food
currentFood.destroy();
currentFood = null;
isBigFood = false;
// Reset food spawn timer
foodSpawnTimer = 0;
return true;
}
return false;
}
// Check wall collision
function checkWallCollision() {
if (snake.length === 0) return false;
var head = snake[0];
return head.x <= BOUNDARY_LEFT || head.x >= BOUNDARY_RIGHT || head.y <= BOUNDARY_TOP || head.y >= BOUNDARY_BOTTOM;
}
// Check self collision
function checkSelfCollision() {
if (snake.length < 4) return false;
var head = snake[0];
for (var i = 3; i < snake.length; i++) {
var distance = Math.sqrt(Math.pow(head.x - snake[i].x, 2) + Math.pow(head.y - snake[i].y, 2));
if (distance < GRID_SIZE * 0.8) {
return true;
}
}
return false;
}
// Handle turn input
function turnSnake(direction) {
if (!gameRunning) return;
if (direction === 'left') {
// Turn left relative to current direction
if (snakeDirection.x > 0) {
// Moving right
nextDirection = {
x: 0,
y: -MOVE_SPEED
};
} else if (snakeDirection.x < 0) {
// Moving left
nextDirection = {
x: 0,
y: MOVE_SPEED
};
} else if (snakeDirection.y > 0) {
// Moving down
nextDirection = {
x: MOVE_SPEED,
y: 0
};
} else if (snakeDirection.y < 0) {
// Moving up
nextDirection = {
x: -MOVE_SPEED,
y: 0
};
}
} else if (direction === 'right') {
// Turn right relative to current direction
if (snakeDirection.x > 0) {
// Moving right
nextDirection = {
x: 0,
y: MOVE_SPEED
};
} else if (snakeDirection.x < 0) {
// Moving left
nextDirection = {
x: 0,
y: -MOVE_SPEED
};
} else if (snakeDirection.y > 0) {
// Moving down
nextDirection = {
x: -MOVE_SPEED,
y: 0
};
} else if (snakeDirection.y < 0) {
// Moving up
nextDirection = {
x: MOVE_SPEED,
y: 0
};
}
}
// Update current direction vectors to match new speed
if (snakeDirection.x > 0) {
snakeDirection.x = MOVE_SPEED;
} else if (snakeDirection.x < 0) {
snakeDirection.x = -MOVE_SPEED;
}
if (snakeDirection.y > 0) {
snakeDirection.y = MOVE_SPEED;
} else if (snakeDirection.y < 0) {
snakeDirection.y = -MOVE_SPEED;
}
}
// Touch controls
game.down = function (x, y, obj) {
if (!gameRunning) return;
// Determine if touch is on left or right side of screen
if (x < GAME_WIDTH / 2) {
turnSnake('left');
} else {
turnSnake('right');
}
};
// Initialize game
initializeSnake();
spawnFood();
// Main game loop
game.update = function () {
if (!gameRunning) return;
// Update direction
snakeDirection = nextDirection;
// Move snake
moveAccumulator += 1;
if (moveAccumulator >= 5) {
// Move every 5 frames for smoother movement
moveAccumulator = 0;
if (snake.length > 0) {
// Store previous head position
var prevX = snake[0].x;
var prevY = snake[0].y;
// Move head
snake[0].x += snakeDirection.x * 5;
snake[0].y += snakeDirection.y * 5;
// Move body segments
for (var i = 1; i < snake.length; i++) {
var tempX = snake[i].x;
var tempY = snake[i].y;
snake[i].x = prevX;
snake[i].y = prevY;
prevX = tempX;
prevY = tempY;
}
// Add new segments if needed
if (segmentsToAdd > 0) {
var newSegment = game.addChild(new SnakeSegment());
newSegment.x = prevX;
newSegment.y = prevY;
snake.push(newSegment);
segmentsToAdd--;
}
}
}
// Check collisions
if (checkWallCollision() || checkSelfCollision()) {
gameRunning = false;
LK.getSound('death').play();
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
checkFoodCollision();
// Spawn food
foodSpawnTimer++;
if (foodSpawnTimer >= FOOD_SPAWN_INTERVAL && !currentFood) {
spawnFood();
foodSpawnTimer = 0;
}
};
draw a mouse. In-Game asset. 2d. High contrast. No shadows
a brick . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
snake head. In-Game asset. 2d. High contrast. No shadows
a brick. In-Game asset. 2d. High contrast. No shadows
draw 3d grass background. In-Game asset. 2d. High contrast. No shadows