/**** * Classes ****/ var FingerIndicator = Container.expand(function () { var self = Container.call(this); var fingerIndicatorGraphics = self.attachAsset('fingerIndicator', { anchorX: 0.5, anchorY: 0.5 }); fingerIndicatorGraphics.alpha = .8; }); var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.attachAsset('food', { anchorX: 0.5, anchorY: 0.5 }); self.spawn = function () { this.x = Math.random() * (2048 - 60) + 30; this.y = Math.random() * (2732 - 60) + 30; }; }); var Snake = Container.expand(function () { var self = Container.call(this); var snakeGraphics = self.attachAsset('snake', { anchorX: 0.5, anchorY: 0.5 }); snakeGraphics.rotation = -Math.PI / 2; self.direction = { x: 0, y: 0 }; self.speed = 7.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; segment.rotation = lastSegmentPosition.rotation; this.bodySegments.push(segment); return segment; }; 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 / 96 * 1.625; self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate); self.x = self.x + Math.cos(self.rotation) * self.speed; self.y = self.y + Math.sin(self.rotation) * self.speed; if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { LK.showGameOver(); } var thresholdDistance = 50; for (var i = 1; i < self.bodySegments.length; i++) { var dx = self.x - self.bodySegments[i].x; var dy = self.y - self.bodySegments[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < thresholdDistance) { LK.showGameOver(); } } self.headPositions.push({ x: self.x, y: self.y, rotation: self.rotation }); var tailSegmentDelay = 20; while (self.headPositions.length > self.bodySegments.length * tailSegmentDelay + 1) { self.headPositions.shift(); } for (var i = 0; i < self.bodySegments.length; i++) { var segmentIndex = (i + 1) * tailSegmentDelay; 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 Snake2 = Container.expand(function () { var self = Container.call(this); var snakeGraphics = self.attachAsset('snake', { anchorX: 0.5, anchorY: 0.5 }); snakeGraphics.rotation = -Math.PI / 2; self.direction = { x: 0, y: 0 }; self.speed = 7.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; segment.rotation = lastSegmentPosition.rotation; this.bodySegments.push(segment); return segment; }; 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 / 96 * 1.625; self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate); self.x = self.x + Math.cos(self.rotation) * self.speed; self.y = self.y + Math.sin(self.rotation) * self.speed; if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { LK.showGameOver(); } var thresholdDistance = 50; for (var i = 1; i < self.bodySegments.length; i++) { var dx = self.x - self.bodySegments[i].x; var dy = self.y - self.bodySegments[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < thresholdDistance) { LK.showGameOver(); } } self.headPositions.push({ x: self.x, y: self.y, rotation: self.rotation }); var tailSegmentDelay = 20; while (self.headPositions.length > self.bodySegments.length * tailSegmentDelay + 1) { self.headPositions.shift(); } for (var i = 0; i < self.bodySegments.length; i++) { var segmentIndex = (i + 1) * tailSegmentDelay; 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 SnakeBodySegment = Container.expand(function () { var self = Container.call(this); var bodySegmentGraphics = self.attachAsset('snakeBodySegment', { anchorX: 0.5, anchorY: 0.5 }); bodySegmentGraphics.rotation = Math.PI / 2; bodySegmentGraphics.tint = 0xff0000; }); var TouchIndicator = Container.expand(function () { var self = Container.call(this); var touchIndicatorGraphics = self.attachAsset('touchIndicator', { anchorX: 0.5, anchorY: 0.5 }); touchIndicatorGraphics.alpha = .4; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ game.on('up', function (obj) { if (touchIndicator) { touchIndicator.destroy(); touchIndicator = null; } isMouseDown = false; if (fingerIndicator) { fingerIndicator.destroy(); fingerIndicator = null; } snake.direction.x = Math.cos(snake.rotation) * snake.speed; snake.direction.y = Math.sin(snake.rotation) * snake.speed; }); var score = 0; var background = game.attachAsset('background', { anchorX: 0.5, anchorY: 0.5 }); background.x = 2048 / 2; background.y = 2732 / 2; background.alpha = .5; game.setBackgroundColor(0xFFFFFF); var snakeContainer = game.addChild(new Container()); var snake = snakeContainer.addChild(new Snake()); var snake2 = snakeContainer.addChild(new Snake2()); var scoreTxt = new Text2('0', { size: 150, fill: '#ffffff', font: 'Impact', dropShadow: true, dropShadowColor: '#000000', dropShadowBlur: 4, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); scoreTxt.anchor.set(.5, 0); LK.gui.top.addChild(scoreTxt); var foods = []; snake.x = 2048 / 2; snake.y = 2732 / 2; var food = new Food(); food.spawn(); foods.push(food); game.addChild(food); var dragNode = null; var initialTouchPosition = { x: 0, y: 0 }; var touchIndicator = null; var fingerIndicator = null; var isMouseDown = false; var maxDistance = 200; game.on('down', function (obj) { var event = obj.event; initialTouchPosition = event.getLocalPosition(game); if (touchIndicator) { touchIndicator.destroy(); } if (fingerIndicator) { fingerIndicator.destroy(); } touchIndicator = game.addChild(new TouchIndicator()); touchIndicator.x = initialTouchPosition.x; touchIndicator.y = initialTouchPosition.y; fingerIndicator = game.addChild(new FingerIndicator()); fingerIndicator.x = initialTouchPosition.x; fingerIndicator.y = initialTouchPosition.y; isMouseDown = true; }); function handleMove(obj) { var event = obj.event; var currentTouchPosition = event.getLocalPosition(game); var dx = currentTouchPosition.x - initialTouchPosition.x; var dy = currentTouchPosition.y - initialTouchPosition.y; var distance = Math.sqrt(dx * dx + dy * dy) * 0.7; 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) { // Determine which snake is closer to the initial touch position and set direction for that snake var distanceToSnake1 = Math.sqrt(Math.pow(snake.x - initialTouchPosition.x, 2) + Math.pow(snake.y - initialTouchPosition.y, 2)); var distanceToSnake2 = Math.sqrt(Math.pow(snake2.x - initialTouchPosition.x, 2) + Math.pow(snake2.y - initialTouchPosition.y, 2)); if (distanceToSnake1 < distanceToSnake2) { snake.direction.x = dx; snake.direction.y = dy; } else { snake2.direction.x = dx; snake2.direction.y = dy; } } } game.on('move', handleMove); LK.on('tick', function () { snake.move(); snake2.move(); for (var i = 0; i < foods.length; i++) { var snakeHeadRadius = snake.width / 2; var foodRadius = foods[i].width / 2; var dx = snake.x - foods[i].x; var dy = snake.y - foods[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < snakeHeadRadius + foodRadius) { foods[i].destroy(); foods.splice(i, 1); score++; LK.setScore(score); scoreTxt.setText(LK.getScore()); var newSegment = snake.addBodySegment(); snakeContainer.addChildAt(newSegment, 0); var newFood = new Food(); newFood.spawn(); foods.push(newFood); game.addChild(newFood); } } });
===================================================================
--- original.js
+++ change.js
@@ -271,10 +271,18 @@
fingerIndicator.x = initialTouchPosition.x + dx;
fingerIndicator.y = initialTouchPosition.y + dy;
}
if (isMouseDown) {
- snake.direction.x = dx;
- snake.direction.y = dy;
+ // Determine which snake is closer to the initial touch position and set direction for that snake
+ var distanceToSnake1 = Math.sqrt(Math.pow(snake.x - initialTouchPosition.x, 2) + Math.pow(snake.y - initialTouchPosition.y, 2));
+ var distanceToSnake2 = Math.sqrt(Math.pow(snake2.x - initialTouchPosition.x, 2) + Math.pow(snake2.y - initialTouchPosition.y, 2));
+ if (distanceToSnake1 < distanceToSnake2) {
+ snake.direction.x = dx;
+ snake.direction.y = dy;
+ } else {
+ snake2.direction.x = dx;
+ snake2.direction.y = dy;
+ }
}
}
game.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.
White circular touch indicator. White button like look. Game asset. No background. 2d. No shadow.
Single snake head segment. Seen from above. Cartoon.. 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.