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
User prompt
Make the enemy snake extend from the normal snake.
User prompt
Make the enemy snake also wrap around the screen.
User prompt
The snake should show on the other side of the screen if it exits the screen.
User prompt
It should gracefully turn to the target direction.
User prompt
Rotate the snake correctly towards the target direction. It is not turning correctly.
Code edit (1 edits merged)
Please save this source code
User prompt
Change turning speed to 0.3
User prompt
Increase the turning speed to 0.7
User prompt
The snake should not be allowed to turn so much. It should have some target direction that it should slowly move towards that. Like some sort of turning speed.
User prompt
It should not be possible to turn the snake around, implement some sort of turning radius
User prompt
The rotation of the snake should the direction of movement.
User prompt
the segments are not attached correctly to the head of the snake
User prompt
The segments should follow the snake right behind it, and everything should rotate and move in the same direction.
User prompt
Fix Bug: 'ReferenceError: segmentGraphics is not defined' in this line: 'newSegment.x = self.x - self.direction.x * segmentGraphics.width;' Line Number: 19
User prompt
The segment should be immediately attached behind the current snake.
User prompt
When a snake eats the food it should grow
User prompt
Dragging my finger on the screen should not change the position of the snake but instead change the direction.
User prompt
The snakes should have a direction of movement and continuously move in that direction.
Initial prompt
Rival Snake
var Snake = Container.expand(function () { var self = Container.call(this); var snakeGraphics = self.createAsset('snake', 'Snake Graphics', .5, .5); self.speed = 5; self.direction = { x: 1, y: 0 }; self.targetDirection = { x: 1, y: 0 }; 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); self.x = (self.x + self.speed * self.direction.x + 2048) % 2048; self.y = (self.y + self.speed * self.direction.y + 2732) % 2732; }; self.grow = function () {}; }); 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); self.speed = 5; self.direction = { x: -1, y: 0 }; self.move = function () { self.x = (self.x + self.speed * self.direction.x + 2048) % 2048; self.y = (self.y + self.speed * self.direction.y + 2732) % 2732; }; }); var Game = Container.expand(function () { var self = Container.call(this); var snakes = []; var foods = []; var enemySnakes = []; var snake = self.addChild(new Snake()); 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
@@ -36,9 +36,8 @@
self.spawn = function () {};
});
var EnemySnake = Snake.expand(function () {
var self = Snake.call(this);
- var enemySnakeGraphics = self.createAsset('enemySnake', 'Enemy Snake Graphics', .5, .5);
self.speed = 5;
self.direction = {
x: -1,
y: 0