User prompt
- When the head consumes the first food then a body is added to the edge (the opposite direction of the snake head movement) of the head - When the head consumes more than one food then the food consumed will be added to the edge of the body (the opposite direction of the snake head & body movement) - The food consumed by the snake will increase the length of the snake (head - body - body - so on) - The representation of the head & body will not be over lapped, it should be head leading the snake and the body. Further, the body follows the path of the head and as the food consumed then the snake body covers one area of the environment (dimension of snakeh head)
User prompt
When a head consume a food, a new element is created which is body and that will be connected to the edge of the snake head. The more number of food consumed the body will be connected to the edge of the other body. Note: Only first body will be connected to the edge of head and then followed by the rest of the body being connected to the edge of the body. Further, the arrangement of snake will be sideways and not overlapped, which is like head - body-body-body-and so on...
User prompt
snake should not leave the screen
User prompt
consider the following format for the snake to be arranged in the right order (head followed by body connected to the edge, then a new body is connected to the edge of the previous body, and so on..) and the arrangement of the snake will be sideways and not overlapped
User prompt
consider the following format for the snake to be arranged in the right order (head followed by body connected to the edge, then a new body is connected to the edge of the previous body, and so on..) and the arrangement of the snake will be sideways and not overlapped
User prompt
When the head consumes the food then add a body to the edge of the head which is continued to increase the length of the snake by one box which is subsequently connected to the edge of the body
User prompt
never generate/create food, life, bonusFood, poison in the corners
User prompt
The head and the body are overlapped, instead increase the length of body by one box whose dimension are same as head
User prompt
When the head consumes the food then add a body to the edge of an head which is continued to increase the length of snake
User prompt
When the head consumes the food then add a body to the edge of the head
User prompt
When the head consumes the food, then a body element is added to the edge of head
User prompt
connect the snakeSegment to the head's edge
User prompt
connect the body to the edge of the head
User prompt
Introduce a body element for head, where the body will only act when the head consumes a food or the bonusFood - food adds one equal dimension of the head - bonusFood adds two equal dimension of the head - the body element is connected to the edge of the snakes head followed by the continuous chain of body element
/**** * Classes ****/ var BodyPart = Container.expand(function () { var self = Container.call(this); var bodyPart = self.attachAsset('head', { anchorX: 0.5, anchorY: 0.5 }); }); var BonusFood = Container.expand(function () { var self = Container.call(this); // Create the bonus food var bonusFood = self.attachAsset('bonusFood', { anchorX: 0.5, anchorY: 0.5 }); }); var Food = Container.expand(function () { var self = Container.call(this); // Create the food var food = self.attachAsset('food', { anchorX: 0.5, anchorY: 0.5 }); }); var Life = Container.expand(function () { var self = Container.call(this); // Create the life var life = self.attachAsset('life', { anchorX: 0.5, anchorY: 0.5 }); }); var Pause = Container.expand(function () { var self = Container.call(this); // Create the pause button var pause = self.attachAsset('pause', { anchorX: 0.5, anchorY: 0.5 }); // Event handler called when a press happens on element. self.down = function (x, y, obj) { if (!game.paused) { game.paused = true; pause.id = 'resume'; } else { game.paused = false; pause.id = 'pause'; } }; }); var Poison = Container.expand(function () { var self = Container.call(this); // Create the poison var poison = self.attachAsset('poison', { anchorX: 0.5, anchorY: 0.5 }); }); var Snake = Container.expand(function () { var self = Container.call(this); // Create the head of the snake var head = self.attachAsset('head', { anchorX: 0.5, anchorY: 0.5 }); // Initialize the snake's direction and speed self.direction = 'up'; self.speed = 5; self.moveHead = function (newDirection) { if (newDirection === 'up' && this.direction !== 'down') { this.direction = 'up'; } else if (newDirection === 'down' && this.direction !== 'up') { this.direction = 'down'; } else if (newDirection === 'left' && this.direction !== 'right') { this.direction = 'left'; } else if (newDirection === 'right' && this.direction !== 'left') { this.direction = 'right'; } }; }); /**** * Initialize Game ****/ //<Assets used in the game will automatically appear here> var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Add a background to the game var background = game.addChild(LK.getAsset('newBackground', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Initialize pause button var pause = game.addChild(new Pause()); // Position the pause button a little to the left from the top right corner of the game pause.x = 2048 - pause.width; pause.y = 50; // Move the pause button a little down var snake = game.addChild(new Snake()); snake.body = []; // Position the snake at the center of the game snake.x = 2048 / 2; snake.y = 2732 / 2; // Initialize food var food = game.addChild(new Food()); // Position the food at a random location within the game food.x = Math.random() * 2048; food.y = Math.random() * 2732; // Initialize poison var bonusFood = game.addChild(new BonusFood()); // Hide the bonus food when the game starts bonusFood.x = -100; bonusFood.y = -100; var poison = game.addChild(new Poison()); // Initialize life var life = game.addChild(new Life()); // Hide the life when the game starts life.x = -100; life.y = -100; // Hide the poison when the game starts poison.x = -100; poison.y = -100; game.update = function () { if (game.paused) { return; } // Update the snake's position based on its direction and speed if (snake.direction === 'up') { snake.y -= snake.speed * 2.25; } else if (snake.direction === 'down') { snake.y += snake.speed * 2.25; } else if (snake.direction === 'left') { snake.x -= snake.speed * 2.25; } else if (snake.direction === 'right') { snake.x += snake.speed * 2.25; } // Update the position of the body parts to follow the head for (var i = snake.body.length - 1; i > 0; i--) { snake.body[i].x = snake.body[i - 1].x; snake.body[i].y = snake.body[i - 1].y; } if (snake.body.length > 0) { snake.body[0].x = snake.x; snake.body[0].y = snake.y; } // Check if the snake's head intersects with the food if (snake.intersects(food)) { // Increase the score score += 10; // Update the score text scoreTxt.setText('Score: ' + score); // Increase the food consumed count foodConsumed += 1; // Add a new body part to the snake var newBodyPart = new BodyPart(); if (snake.body.length === 0) { // Add the first body part to the edge of the head if (snake.direction === 'up') { newBodyPart.x = snake.x; newBodyPart.y = snake.y + snake.height; } else if (snake.direction === 'down') { newBodyPart.x = snake.x; newBodyPart.y = snake.y - snake.height; } else if (snake.direction === 'left') { newBodyPart.x = snake.x + snake.width; newBodyPart.y = snake.y; } else if (snake.direction === 'right') { newBodyPart.x = snake.x - snake.width; newBodyPart.y = snake.y; } } else { // Add subsequent body parts to the edge of the last body part var lastBodyPart = snake.body[snake.body.length - 1]; if (snake.direction === 'up') { newBodyPart.x = lastBodyPart.x; newBodyPart.y = lastBodyPart.y + lastBodyPart.height; } else if (snake.direction === 'down') { newBodyPart.x = lastBodyPart.x; newBodyPart.y = lastBodyPart.y - lastBodyPart.height; } else if (snake.direction === 'left') { newBodyPart.x = lastBodyPart.x + lastBodyPart.width; newBodyPart.y = lastBodyPart.y; } else if (snake.direction === 'right') { newBodyPart.x = lastBodyPart.x - lastBodyPart.width; newBodyPart.y = lastBodyPart.y; } } snake.body.push(newBodyPart); game.addChild(newBodyPart); // Check if it's time to show the bonus food if (score % 150 === 0) { // Position the bonus food at a new random location within the game, ensuring it does not appear on the border do { bonusFood.x = Math.random() * (2048 - bonusFood.width) + bonusFood.width / 2; bonusFood.y = Math.random() * (2732 - bonusFood.height) + bonusFood.height / 2; } while ((bonusFood.x < 100 || bonusFood.x > 1948) && (bonusFood.y < 100 || bonusFood.y > 2632)); // Show the bonus food for 8 seconds LK.setTimeout(function () { bonusFood.x = -100; bonusFood.y = -100; }, 8000); } // Check if it's time to show the life if (score !== 0 && score % 100 === 0 && score != previousScore) { previousScore = score; // Position the life at a new random location within the game, ensuring it does not appear on the border do { life.x = Math.random() * (2048 - life.width) + life.width / 2; life.y = Math.random() * (2732 - life.height) + life.height / 2; } while ((life.x < 100 || life.x > 1948) && (life.y < 100 || life.y > 2632)); // Show the life for 10 seconds LK.setTimeout(function () { life.x = -100; life.y = -100; }, 10000); } // Check if it's time to show the poison if (foodConsumed === 8 && !poisonGenerated) { // Position the poison at a new random location within the game, ensuring it does not appear on the border do { poison.x = Math.random() * (2048 - poison.width) + poison.width / 2; poison.y = Math.random() * (2732 - poison.height) + poison.height / 2; } while ((poison.x < 100 || poison.x > 1948) && (poison.y < 100 || poison.y > 2632)); // Show the poison for 8 seconds LK.setTimeout(function () { poison.x = -100; poison.y = -100; }, 8000); poisonGenerated = true; foodConsumed = 0; // Reset the food consumed count after generating poison } // Position the food at a new random location within the game, ensuring it does not appear on the border do { food.x = Math.random() * (2048 - food.width) + food.width / 2; food.y = Math.random() * (2732 - food.height) + food.height / 2; } while ((food.x < 100 || food.x > 1948) && (food.y < 100 || food.y > 2632)); } // Check if the snake's head intersects with the bonus food if (snake.intersects(bonusFood)) { // Increase the score by 50 score += 50; // Update the score text scoreTxt.setText('Score: ' + score); // Play the bonus food sound LK.getSound('bonusfood').play(); // Hide the bonus food bonusFood.x = -100; bonusFood.y = -100; } // Check if the snake's head intersects with the life if (snake.intersects(life)) { // Increase the lives by 1, up to a maximum of 8 if (lives < 8) { lives += 1; // Update the lives text livesTxt.setText('Lives: ' + lives); // Play the life sound LK.getSound('life').play(); } // Hide the life life.x = -100; life.y = -100; } // Check if the snake's head intersects with the poison if (snake.intersects(poison)) { // Decrease the lives lives -= 1; // Update the lives text livesTxt.setText('Lives: ' + lives); // Play the poison sound LK.getSound('poison').play(); // Hide the poison poison.x = -100; poison.y = -100; // Reset the food consumed count after consuming poison foodConsumed = 0; // Check if the lives have hit 0 if (lives === 0) { // Show game over LK.getSound('GameOver').play(); LK.showGameOver(); } } // Check if the snake's head has moved out of the screen if (snake.x < 0) { snake.x = 2048; } else if (snake.x > 2048) { snake.x = 0; } else if (snake.y < 0) { snake.y = 2732; } else if (snake.y > 2732) { snake.y = 0; } }; // Initialize lives, score, food consumed and poison generated var lives = 3; var score = 0; var previousScore = 0; var foodConsumed = 0; var poisonGenerated = false; // Create lives text var livesTxt = new Text2('Lives: ' + lives, { size: 32, fill: "#ffffff" }); livesTxt.anchor.set(0, 0); LK.gui.topLeft.addChild(livesTxt); // Create score text var scoreTxt = new Text2('Score: ' + score, { size: 32, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.down = function (x, y, obj) { if (game.paused) { return; } // Change the snake's direction to always move in the direction of the front area of the snake head which is not connected to the snake body if (x > snake.x && snake.direction !== 'left' && snake.direction !== 'right') { snake.moveHead('right'); } else if (x < snake.x && snake.direction !== 'right' && snake.direction !== 'left') { snake.moveHead('left'); } else if (y > snake.y && snake.direction !== 'up' && snake.direction !== 'down') { snake.moveHead('down'); } else if (y < snake.y && snake.direction !== 'down' && snake.direction !== 'up') { snake.moveHead('up'); } };
===================================================================
--- original.js
+++ change.js
@@ -1,11 +1,10 @@
/****
* Classes
****/
-var Body = Container.expand(function () {
+var BodyPart = Container.expand(function () {
var self = Container.call(this);
- // Create the body of the snake
- var body = self.attachAsset('body', {
+ var bodyPart = self.attachAsset('head', {
anchorX: 0.5,
anchorY: 0.5
});
});
@@ -105,8 +104,9 @@
// Position the pause button a little to the left from the top right corner of the game
pause.x = 2048 - pause.width;
pause.y = 50; // Move the pause button a little down
var snake = game.addChild(new Snake());
+snake.body = [];
// Position the snake at the center of the game
snake.x = 2048 / 2;
snake.y = 2732 / 2;
// Initialize food
@@ -141,42 +141,61 @@
snake.x -= snake.speed * 2.25;
} else if (snake.direction === 'right') {
snake.x += snake.speed * 2.25;
}
- // Update the position of the snake's body
- for (var i = snake.children.length - 1; i >= 0; i--) {
- var body = snake.children[i];
- if (i === 0) {
- // The first body is connected to the edge of the snake head
- body.x = snake.x;
- body.y = snake.y + snake.height;
- } else {
- // The rest of the body is connected to the edge of the other body
- var previousBody = snake.children[i - 1];
- body.x = previousBody.x;
- body.y = previousBody.y + previousBody.height;
- }
+ // Update the position of the body parts to follow the head
+ for (var i = snake.body.length - 1; i > 0; i--) {
+ snake.body[i].x = snake.body[i - 1].x;
+ snake.body[i].y = snake.body[i - 1].y;
}
+ if (snake.body.length > 0) {
+ snake.body[0].x = snake.x;
+ snake.body[0].y = snake.y;
+ }
// Check if the snake's head intersects with the food
if (snake.intersects(food)) {
// Increase the score
score += 10;
// Update the score text
scoreTxt.setText('Score: ' + score);
// Increase the food consumed count
foodConsumed += 1;
- // Add a body to the snake
- var body = game.addChild(new Body());
- // Position the body at the edge of the snake head
- body.x = snake.x;
- body.y = snake.y + snake.height;
- // Connect the body to the edge of the other body
- if (snake.children.length > 0) {
- var lastBody = snake.children[snake.children.length - 1];
- body.x = lastBody.x;
- body.y = lastBody.y + lastBody.height;
+ // Add a new body part to the snake
+ var newBodyPart = new BodyPart();
+ if (snake.body.length === 0) {
+ // Add the first body part to the edge of the head
+ if (snake.direction === 'up') {
+ newBodyPart.x = snake.x;
+ newBodyPart.y = snake.y + snake.height;
+ } else if (snake.direction === 'down') {
+ newBodyPart.x = snake.x;
+ newBodyPart.y = snake.y - snake.height;
+ } else if (snake.direction === 'left') {
+ newBodyPart.x = snake.x + snake.width;
+ newBodyPart.y = snake.y;
+ } else if (snake.direction === 'right') {
+ newBodyPart.x = snake.x - snake.width;
+ newBodyPart.y = snake.y;
+ }
+ } else {
+ // Add subsequent body parts to the edge of the last body part
+ var lastBodyPart = snake.body[snake.body.length - 1];
+ if (snake.direction === 'up') {
+ newBodyPart.x = lastBodyPart.x;
+ newBodyPart.y = lastBodyPart.y + lastBodyPart.height;
+ } else if (snake.direction === 'down') {
+ newBodyPart.x = lastBodyPart.x;
+ newBodyPart.y = lastBodyPart.y - lastBodyPart.height;
+ } else if (snake.direction === 'left') {
+ newBodyPart.x = lastBodyPart.x + lastBodyPart.width;
+ newBodyPart.y = lastBodyPart.y;
+ } else if (snake.direction === 'right') {
+ newBodyPart.x = lastBodyPart.x - lastBodyPart.width;
+ newBodyPart.y = lastBodyPart.y;
+ }
}
- snake.addChild(body);
+ snake.body.push(newBodyPart);
+ game.addChild(newBodyPart);
// Check if it's time to show the bonus food
if (score % 150 === 0) {
// Position the bonus food at a new random location within the game, ensuring it does not appear on the border
do {
Apple. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Poisonous Skull. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Pause icon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
heart icon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Snake Face. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Ostrich egg. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.