User prompt
No red screen
User prompt
No red screen
User prompt
If the snake touch the wall don't end game
User prompt
When the snake touch dot it size increase
User prompt
Snake can move Left Right as user wants
User prompt
Snake can move Left Right as user wants
User prompt
Snake move where the user wants
User prompt
No game over
Initial prompt
Snake
/****
* Classes
****/
// Food class to manage the food's position
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphic = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomly position the food on the screen
self.randomizePosition = function () {
self.x = Math.floor(Math.random() * 2048);
self.y = Math.floor(Math.random() * 2732);
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For example, the snake and food will be represented by shapes or images initialized dynamically.
// Snake class to manage the snake's body and movement
var Snake = Container.expand(function () {
var self = Container.call(this);
self.body = [];
self.direction = {
x: 0,
y: -1
}; // Initial direction: moving up
self.speed = 10; // Speed of the snake
// Initialize the snake with a starting length
self.init = function (length) {
for (var i = 0; i < length; i++) {
var segment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
segment.x = 1024; // Center of the screen
segment.y = 1366 + i * 20; // Stacked vertically
self.body.push(segment);
}
};
// Update the snake's position
self.update = function () {
// Move the body segments
for (var i = self.body.length - 1; i > 0; i--) {
self.body[i].x = self.body[i - 1].x;
self.body[i].y = self.body[i - 1].y;
}
// Move the head
self.body[0].x += self.direction.x * self.speed;
self.body[0].y += self.direction.y * self.speed;
};
// Grow the snake by adding a new segment
self.grow = function () {
var lastSegment = self.body[self.body.length - 1];
var newSegment = self.attachAsset('snakeSegment', {
anchorX: 0.5,
anchorY: 0.5
});
newSegment.x = lastSegment.x;
newSegment.y = lastSegment.y;
self.body.push(newSegment);
};
// Check if the snake has collided with itself
self.checkSelfCollision = function () {
var head = self.body[0];
for (var i = 1; i < self.body.length; i++) {
if (head.x === self.body[i].x && head.y === self.body[i].y) {
return true;
}
}
return false;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var snake = new Snake();
var food = new Food();
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
var score = 0;
// Initialize the snake and food
snake.init(5);
food.randomizePosition();
// Add snake and food to the game
game.addChild(snake);
game.addChild(food);
// Add score text to the GUI
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
var head = snake.body[0];
if (x < head.x) {
snake.direction = {
x: -1,
y: 0
}; // Move left
} else if (x > head.x) {
snake.direction = {
x: 1,
y: 0
}; // Move right
} else if (y < head.y) {
snake.direction = {
x: 0,
y: -1
}; // Move up
} else if (y > head.y) {
snake.direction = {
x: 0,
y: 1
}; // Move down
}
};
// Update game logic
game.update = function () {
snake.update();
// Check for collision with food
if (snake.body[0].x === food.x && snake.body[0].y === food.y) {
snake.grow();
food.randomizePosition();
score++;
scoreTxt.setText(score);
}
// Check for self-collision
if (snake.checkSelfCollision()) {
LK.effects.flashScreen(0xff0000, 1000);
// Removed game over when snake collides with itself
}
// Check for collision with walls
var head = snake.body[0];
if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
LK.effects.flashScreen(0xff0000, 1000);
// Removed game over when snake collides with walls
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,92 +1,92 @@
-/****
+/****
* Classes
-****/
+****/
// Food class to manage the food's position
var Food = Container.expand(function () {
- var self = Container.call(this);
- var foodGraphic = self.attachAsset('food', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- // Randomly position the food on the screen
- self.randomizePosition = function () {
- self.x = Math.floor(Math.random() * 2048);
- self.y = Math.floor(Math.random() * 2732);
- };
+ var self = Container.call(this);
+ var foodGraphic = self.attachAsset('food', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Randomly position the food on the screen
+ self.randomizePosition = function () {
+ self.x = Math.floor(Math.random() * 2048);
+ self.y = Math.floor(Math.random() * 2732);
+ };
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For example, the snake and food will be represented by shapes or images initialized dynamically.
// Snake class to manage the snake's body and movement
var Snake = Container.expand(function () {
- var self = Container.call(this);
- self.body = [];
- self.direction = {
- x: 0,
- y: -1
- }; // Initial direction: moving up
- self.speed = 10; // Speed of the snake
- // Initialize the snake with a starting length
- self.init = function (length) {
- for (var i = 0; i < length; i++) {
- var segment = self.attachAsset('snakeSegment', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- segment.x = 1024; // Center of the screen
- segment.y = 1366 + i * 20; // Stacked vertically
- self.body.push(segment);
- }
- };
- // Update the snake's position
- self.update = function () {
- // Move the body segments
- for (var i = self.body.length - 1; i > 0; i--) {
- self.body[i].x = self.body[i - 1].x;
- self.body[i].y = self.body[i - 1].y;
- }
- // Move the head
- self.body[0].x += self.direction.x * self.speed;
- self.body[0].y += self.direction.y * self.speed;
- };
- // Grow the snake by adding a new segment
- self.grow = function () {
- var lastSegment = self.body[self.body.length - 1];
- var newSegment = self.attachAsset('snakeSegment', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- newSegment.x = lastSegment.x;
- newSegment.y = lastSegment.y;
- self.body.push(newSegment);
- };
- // Check if the snake has collided with itself
- self.checkSelfCollision = function () {
- var head = self.body[0];
- for (var i = 1; i < self.body.length; i++) {
- if (head.x === self.body[i].x && head.y === self.body[i].y) {
- return true;
- }
- }
- return false;
- };
+ var self = Container.call(this);
+ self.body = [];
+ self.direction = {
+ x: 0,
+ y: -1
+ }; // Initial direction: moving up
+ self.speed = 10; // Speed of the snake
+ // Initialize the snake with a starting length
+ self.init = function (length) {
+ for (var i = 0; i < length; i++) {
+ var segment = self.attachAsset('snakeSegment', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ segment.x = 1024; // Center of the screen
+ segment.y = 1366 + i * 20; // Stacked vertically
+ self.body.push(segment);
+ }
+ };
+ // Update the snake's position
+ self.update = function () {
+ // Move the body segments
+ for (var i = self.body.length - 1; i > 0; i--) {
+ self.body[i].x = self.body[i - 1].x;
+ self.body[i].y = self.body[i - 1].y;
+ }
+ // Move the head
+ self.body[0].x += self.direction.x * self.speed;
+ self.body[0].y += self.direction.y * self.speed;
+ };
+ // Grow the snake by adding a new segment
+ self.grow = function () {
+ var lastSegment = self.body[self.body.length - 1];
+ var newSegment = self.attachAsset('snakeSegment', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ newSegment.x = lastSegment.x;
+ newSegment.y = lastSegment.y;
+ self.body.push(newSegment);
+ };
+ // Check if the snake has collided with itself
+ self.checkSelfCollision = function () {
+ var head = self.body[0];
+ for (var i = 1; i < self.body.length; i++) {
+ if (head.x === self.body[i].x && head.y === self.body[i].y) {
+ return true;
+ }
+ }
+ return false;
+ };
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
var snake = new Snake();
var food = new Food();
var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
var score = 0;
// Initialize the snake and food
snake.init(5);
@@ -98,49 +98,49 @@
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle touch events to change snake direction
game.down = function (x, y, obj) {
- var head = snake.body[0];
- if (x < head.x) {
- snake.direction = {
- x: -1,
- y: 0
- }; // Move left
- } else if (x > head.x) {
- snake.direction = {
- x: 1,
- y: 0
- }; // Move right
- } else if (y < head.y) {
- snake.direction = {
- x: 0,
- y: -1
- }; // Move up
- } else if (y > head.y) {
- snake.direction = {
- x: 0,
- y: 1
- }; // Move down
- }
+ var head = snake.body[0];
+ if (x < head.x) {
+ snake.direction = {
+ x: -1,
+ y: 0
+ }; // Move left
+ } else if (x > head.x) {
+ snake.direction = {
+ x: 1,
+ y: 0
+ }; // Move right
+ } else if (y < head.y) {
+ snake.direction = {
+ x: 0,
+ y: -1
+ }; // Move up
+ } else if (y > head.y) {
+ snake.direction = {
+ x: 0,
+ y: 1
+ }; // Move down
+ }
};
// Update game logic
game.update = function () {
- snake.update();
- // Check for collision with food
- if (snake.body[0].x === food.x && snake.body[0].y === food.y) {
- snake.grow();
- food.randomizePosition();
- score++;
- scoreTxt.setText(score);
- }
- // Check for self-collision
- if (snake.checkSelfCollision()) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- // Check for collision with walls
- var head = snake.body[0];
- if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
+ snake.update();
+ // Check for collision with food
+ if (snake.body[0].x === food.x && snake.body[0].y === food.y) {
+ snake.grow();
+ food.randomizePosition();
+ score++;
+ scoreTxt.setText(score);
+ }
+ // Check for self-collision
+ if (snake.checkSelfCollision()) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ // Removed game over when snake collides with itself
+ }
+ // Check for collision with walls
+ var head = snake.body[0];
+ if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ // Removed game over when snake collides with walls
+ }
};
\ No newline at end of file