User prompt
make the turning speed to 0.2
User prompt
change enemy snake speed to 20
User prompt
The parameters of initialization of the enemy snake are not properly propagated
User prompt
Change the snake speeds to 20
User prompt
Show the score in the top middle of the screen
User prompt
Make the score go down every time the enemy eats food.
User prompt
Every time the player snake eats food, it should count 1 score.
User prompt
Allow all snakes to eat the food and grow
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting 'moveTowardsFood')' in this line: 'self.moveTowardsFood = function (foods) {' Line Number: 40
User prompt
Make the enemy snake go towards food.
Code edit (1 edits merged)
Please save this source code
User prompt
Add a tail that grows when the player eats food.
User prompt
Correctly calculate the tail in local space instead of global space.
User prompt
Make the tail of the snake grow when it eats food.
User prompt
When a snake eats food, it should grow. New segments should be added to the end of the snake, that follow each other at a short distance. The direction of the segments should be independent, but they should follow the general shape of the snake.
User prompt
The segments are still not properly added, fix them.
User prompt
They should be attached to the snake, but have some distance from it.
User prompt
Fix the snake segments, so they are in local space instead of global space.
User prompt
Fix the snake segments so it follows the snake right behind it.. and it grows.
User prompt
Fix the segments positioning, it should follow the snake closely behind.
User prompt
When a snake eats food, it should grow by attaching a segment right before.
User prompt
the player snake has not been initialized properly
User prompt
Make the snake an abstraction of all snakes, and allow the enemy snake to extend from it.
User prompt
Fix Bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'createAsset')' in this line: 'var enemySnakeGraphics = self.createAsset('enemySnake', 'Enemy Snake Graphics', .5, .5);' Line Number: 40
Code edit (1 edits merged)
Please save this source code
var Snake = Container.expand(function (assetId, speed, directionX, directionY) { var self = Container.call(this); var snakeGraphics = self.createAsset(assetId, 'Snake Graphics', .5, .5); self.speed = speed; self.direction = { x: directionX, y: directionY }; self.targetDirection = { x: directionX, y: directionY }; self.turningSpeed = 0.05; self.move = function () { var targetAngle = Math.atan2(self.targetDirection.y, self.targetDirection.x); var currentAngle = Math.atan2(self.direction.y, self.direction.x); var deltaAngle = targetAngle - currentAngle; if (deltaAngle > Math.PI) deltaAngle -= 2 * Math.PI; if (deltaAngle < -Math.PI) deltaAngle += 2 * Math.PI; if (Math.abs(deltaAngle) < self.turningSpeed) { currentAngle = targetAngle; } else { currentAngle += Math.sign(deltaAngle) * self.turningSpeed; } self.rotation = currentAngle; self.direction.x = Math.cos(currentAngle); self.direction.y = Math.sin(currentAngle); for (var i = self.tail.length - 1; i > 0; i--) { self.tail[i].x = self.tail[i - 1].x - self.x; self.tail[i].y = self.tail[i - 1].y - self.y; } if (self.tail.length > 0) { self.tail[0].x = 0; self.tail[0].y = 0; } self.x = (self.x + self.speed * self.direction.x + 2048) % 2048; self.y = (self.y + self.speed * self.direction.y + 2732) % 2732; }; self.tail = []; self.grow = function () { var tailSegment = new Container(); tailSegment.x = self.x; tailSegment.y = self.y; self.tail.push(tailSegment); self.addChild(tailSegment); }; }); var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.createAsset('food', 'Food Graphics', .5, .5); self.spawn = function () {}; }); var EnemySnake = Snake.expand(function () { var self = Snake.call(this, 'enemySnake', 5, -1, 0); }); var Game = Container.expand(function () { var self = Container.call(this); var snakes = []; var foods = []; var enemySnakes = []; var snake = self.addChild(new Snake('playerSnake', 5, 1, 0)); snake.x = 2048 / 2; snake.y = 2732 / 2; var food = self.addChild(new Food()); food.x = Math.random() * 2048; food.y = Math.random() * 2732; var enemySnake = self.addChild(new EnemySnake()); enemySnake.x = Math.random() * 2048; enemySnake.y = Math.random() * 2732; snakes.push(snake); foods.push(food); enemySnakes.push(enemySnake); var dragNode = null; stage.on('down', function (obj) { dragNode = snake; }); function handleMove(obj) { var event = obj.event; var pos = event.getLocalPosition(self); if (dragNode) { dragNode.targetDirection.x = pos.x - dragNode.x; dragNode.targetDirection.y = pos.y - dragNode.y; var magnitude = Math.sqrt(dragNode.targetDirection.x * dragNode.targetDirection.x + dragNode.targetDirection.y * dragNode.targetDirection.y); dragNode.targetDirection.x /= magnitude; dragNode.targetDirection.y /= magnitude; } } stage.on('move', handleMove); stage.on('up', function (obj) { dragNode = null; }); LK.on('tick', function () { for (var i = 0; i < snakes.length; i++) { snakes[i].move(); } for (var i = 0; i < enemySnakes.length; i++) { enemySnakes[i].move(); } for (var i = 0; i < foods.length; i++) { if (snake.intersects(foods[i])) { snake.grow(); foods[i].destroy(); foods.splice(i, 1); var newFood = new Food(); newFood.x = Math.random() * 2048; newFood.y = Math.random() * 2732; foods.push(newFood); self.addChild(newFood); } } for (var i = 0; i < enemySnakes.length; i++) { if (snake.intersects(enemySnakes[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } }); });
===================================================================
--- original.js
+++ change.js
@@ -25,14 +25,14 @@
self.rotation = currentAngle;
self.direction.x = Math.cos(currentAngle);
self.direction.y = Math.sin(currentAngle);
for (var i = self.tail.length - 1; i > 0; i--) {
- self.tail[i].x = self.tail[i - 1].x;
- self.tail[i].y = self.tail[i - 1].y;
+ self.tail[i].x = self.tail[i - 1].x - self.x;
+ self.tail[i].y = self.tail[i - 1].y - self.y;
}
if (self.tail.length > 0) {
- self.tail[0].x = self.x;
- self.tail[0].y = self.y;
+ self.tail[0].x = 0;
+ self.tail[0].y = 0;
}
self.x = (self.x + self.speed * self.direction.x + 2048) % 2048;
self.y = (self.y + self.speed * self.direction.y + 2732) % 2732;
};