User prompt
Limit how quickly the snake can change direction, but putting a limit on how quickly the snake can turn
User prompt
you still use .angle on snake, it should be rotation everywhere
User prompt
You seem to be using .angle over .rotation on snake, please fix this
User prompt
also rotate the snake head
User prompt
when adding new segments to the snake, attach them with a z-index below the current elements
User prompt
attach new body segments to index 0
User prompt
when spawning a new body segment, spawn it at the position of the last element in the body.
User prompt
Make one more element be in he headPosition array, when cleaning the list
Code edit (1 edits merged)
Please save this source code
User prompt
Shift elements out of the headPositions array based on how many body parts the snake has
User prompt
The first body part should be 32 ticks behind the head, right now it's zero ticks behind
User prompt
when collecting food, add a segment to the snake
User prompt
Only allow snake to change direction if the mouse is pressed
User prompt
Add a method to game that spawns food at a random position on the board
User prompt
Add the ability to add more body segments to the snake, each body segment should have a position that is 32 ticks behind the head, using theheadPositions array.
User prompt
When storing head position also store head angle
User prompt
Add body segments to the snake, the snake should be able to have n amount of body segments, with each segment having the position of 32 positions behind the element in-front of it
User prompt
Keep an array of head positions and update this array each tick
User prompt
Fix Bug: 'TypeError: null is not an object (evaluating 'fingerIndicator.x = initialTouchPosition.x + dx')' in this line: 'fingerIndicator.x = initialTouchPosition.x + dx;' Line Number: 85
User prompt
Hide the touch indicator system when I release the screen
User prompt
Increase max distance of touch indicator by 100
User prompt
Add a finger indicator as well, which shows where my finger is in relation to the touch indicator. Limit it such that the finger indicator cannot get further from the ce ter
User prompt
Set touch indicator alpha to .3
User prompt
The snake should wrap around in the x and y direction
User prompt
Donโt change initial touch position in the move metgod
var SnakeBodySegment = Container.expand(function () { var self = Container.call(this); var bodySegmentGraphics = self.createAsset('snakeBodySegment', 'Snake Body Segment Graphics', .5, .5); }); var FingerIndicator = Container.expand(function () { var self = Container.call(this); var fingerIndicatorGraphics = self.createAsset('fingerIndicator', 'Finger Indicator Graphics', 0.5, 0.5); fingerIndicatorGraphics.alpha = .5; }); var TouchIndicator = Container.expand(function () { var self = Container.call(this); var touchIndicatorGraphics = self.createAsset('touchIndicator', 'Touch Indicator Graphics', 0.5, 0.5); touchIndicatorGraphics.alpha = .3; }); var Snake = Container.expand(function () { var self = Container.call(this); var snakeGraphics = self.createAsset('snake', 'Snake Graphics', .5, .5); self.direction = { x: 0, y: 0 }; self.speed = 5; self.angle = 0; self.headPositions = []; self.bodySegments = []; self.addBodySegment = function () { var segment = new SnakeBodySegment(); var lastSegmentPosition = this.bodySegments.length > 0 ? this.bodySegments[this.bodySegments.length - 1] : this; segment.x = lastSegmentPosition.x; segment.y = lastSegmentPosition.y; this.bodySegments.push(segment); LK.stage.addChildAt(segment, 0); }; self.move = function () { var targetRotation = Math.atan2(self.direction.y, self.direction.x); var rotationDifference = targetRotation - self.rotation; while (rotationDifference < -Math.PI) rotationDifference += 2 * Math.PI; while (rotationDifference > Math.PI) rotationDifference -= 2 * Math.PI; var turnRate = Math.PI / 32; self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate); self.x = (self.x + Math.cos(self.rotation) * self.speed + 2048) % 2048; self.y = (self.y + Math.sin(self.rotation) * self.speed + 2732) % 2732; self.headPositions.push({ x: self.x, y: self.y, rotation: self.rotation }); while (self.headPositions.length > self.bodySegments.length * 32 + 1) { self.headPositions.shift(); } for (var i = 0; i < self.bodySegments.length; i++) { var segmentIndex = (i + 1) * 32; if (self.headPositions.length > segmentIndex) { var pos = self.headPositions[self.headPositions.length - 1 - segmentIndex]; self.bodySegments[i].x = pos.x; self.bodySegments[i].y = pos.y; self.bodySegments[i].rotation = pos.rotation; } } }; }); var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.createAsset('food', 'Food Graphics', .5, .5); self.spawn = function () { this.x = Math.random() * 2048; this.y = Math.random() * 2732; }; }); var Game = Container.expand(function () { stage.on('up', function (obj) { if (touchIndicator) { touchIndicator.destroy(); touchIndicator = null; } isMouseDown = false; if (fingerIndicator) { fingerIndicator.destroy(); fingerIndicator = null; } }); var self = Container.call(this); var snake = self.addChild(new Snake()); var foods = []; snake.x = 2048 / 2; snake.y = 2732 / 2; var food = new Food(); food.spawn(); foods.push(food); self.addChild(food); var dragNode = null; var initialTouchPosition = { x: 0, y: 0 }; var touchIndicator = null; var fingerIndicator = null; var isMouseDown = false; var maxDistance = 200; stage.on('down', function (obj) { var event = obj.event; initialTouchPosition = event.getLocalPosition(self); if (touchIndicator) { touchIndicator.destroy(); } if (fingerIndicator) { fingerIndicator.destroy(); } touchIndicator = self.addChild(new TouchIndicator()); touchIndicator.x = initialTouchPosition.x; touchIndicator.y = initialTouchPosition.y; fingerIndicator = self.addChild(new FingerIndicator()); fingerIndicator.x = initialTouchPosition.x; fingerIndicator.y = initialTouchPosition.y; isMouseDown = true; }); function handleMove(obj) { var event = obj.event; var currentTouchPosition = event.getLocalPosition(self); var dx = currentTouchPosition.x - initialTouchPosition.x; var dy = currentTouchPosition.y - initialTouchPosition.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > maxDistance) { var angle = Math.atan2(dy, dx); dx = maxDistance * Math.cos(angle); dy = maxDistance * Math.sin(angle); } if (fingerIndicator) { fingerIndicator.x = initialTouchPosition.x + dx; fingerIndicator.y = initialTouchPosition.y + dy; } if (isMouseDown) { snake.direction.x = dx; snake.direction.y = dy; } } stage.on('move', handleMove); LK.on('tick', function () { snake.move(); for (var i = 0; i < foods.length; i++) { if (snake.intersects(foods[i])) { foods[i].destroy(); foods.splice(i, 1); snake.addBodySegment(); var newFood = new Food(); newFood.spawn(); foods.push(newFood); self.addChild(newFood); } } }); });
===================================================================
--- original.js
+++ change.js
@@ -31,8 +31,14 @@
this.bodySegments.push(segment);
LK.stage.addChildAt(segment, 0);
};
self.move = function () {
+ var targetRotation = Math.atan2(self.direction.y, self.direction.x);
+ var rotationDifference = targetRotation - self.rotation;
+ while (rotationDifference < -Math.PI) rotationDifference += 2 * Math.PI;
+ while (rotationDifference > Math.PI) rotationDifference -= 2 * Math.PI;
+ var turnRate = Math.PI / 32;
+ self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate);
self.x = (self.x + Math.cos(self.rotation) * self.speed + 2048) % 2048;
self.y = (self.y + Math.sin(self.rotation) * self.speed + 2732) % 2732;
self.headPositions.push({
x: self.x,
@@ -123,9 +129,10 @@
fingerIndicator.x = initialTouchPosition.x + dx;
fingerIndicator.y = initialTouchPosition.y + dy;
}
if (isMouseDown) {
- snake.rotation = Math.atan2(dy, dx);
+ snake.direction.x = dx;
+ snake.direction.y = dy;
}
}
stage.on('move', handleMove);
LK.on('tick', function () {
Single, Anaconda snake head segment. Seen from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Single snake head segment. Seen from above. Cartoon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Golden Apple Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Bright Amazing games background for modern snake game. It should be epic!
White circular touch indicator. White button like look. Game asset. No background. 2d. No shadow.