User prompt
Decrease snake move speed by 5x , by tracking a tick counter and skipping them
User prompt
Decrease speed 5x
User prompt
Increase grid size to 128
User prompt
Ensure snake head snaps with the grid
User prompt
Ensure dot position aligns with the grid
User prompt
Change snake right movement to match the rest
User prompt
Ensure snake position is at 0,0 as it's just a container for the head and body
User prompt
Offset all the body elements by the heads X/Y so they don't move when the snake itself moves.
User prompt
Take into account that the snake body is added to the same parent, so we need to offset the body elements by the X/Y
User prompt
Fix Bug: 'Uncaught TypeError: this.children[e].setStageReference is not a function' in this line: 'dots.push(dot);' Line Number: 121
User prompt
Fix Bug: 'Uncaught TypeError: this.children[e].setStageReference is not a function' in this line: 'dots.push(dot);' Line Number: 120
User prompt
Fix Bug: 'Uncaught TypeError: this.children[e].setStageReference is not a function' in this line: 'dots.push(dot);' Line Number: 119
User prompt
Fix Bug: 'Uncaught TypeError: this.children[e].setStageReference is not a function' in this line: 'self.addChild(dot);' Line Number: 120
User prompt
Don't add body segments to the snake itself, otherwise they move with the head
User prompt
Ensure the snake head, body, and dots, are all aligned on the same grid, i.e. are always multiples of 32
User prompt
Make body also follow the same grid
User prompt
Make food disappear when eaten
User prompt
Make food aligned to grid position. Only spawn food after its eaten. Ensure snake tail follows the snake
User prompt
Ensure wrapping works for negative too
User prompt
Make movement change based on a swipe direction
User prompt
Fix Bug: 'ReferenceError: gridSize is not defined' in this line: 'if (self.x < 0) self.x = maxX - gridSize;' Line Number: 43
User prompt
Slow down the movement. Spawn food on the grid. Make the snake wrap when hitting the side
User prompt
Make it work on a grid like Nokia snake
User prompt
Keep implementing
Initial prompt
Snake
var Dot = Container.expand(function () { var self = Container.call(this); var dotGraphics = self.createAsset('dot', 'Dot Graphics', .5, .5); self.eaten = false; }); var Snake = Container.expand(function () { var self = Container.call(this); var gridSize = 32; var snakeGraphics = self.createAsset('snake', 'Snake Graphics', .5, .5); self.length = 1; self.direction = 'right'; self.body = []; self.move = function () { var gridSize = 32; var headX = self.x; var headY = self.y; switch (self.direction) { case 'right': self.x = Math.round((self.x + gridSize) / gridSize) * gridSize; break; case 'left': self.x -= gridSize; break; case 'down': self.y += gridSize; break; case 'up': self.y -= gridSize; break; } for (var i = self.body.length - 1; i >= 0; i--) { if (i === 0) { self.body[i].x = Math.round(headX / gridSize) * gridSize; self.body[i].y = Math.round(headY / gridSize) * gridSize; } else { self.body[i].x = self.body[i - 1].x; self.body[i].y = self.body[i - 1].y; } } }; self.eat = function (dot) { self.length++; var newSegment = self.createAsset('snakeBody', 'Snake Body Segment', .5, .5); self.body.push(newSegment); self.addChild(newSegment); if (self.body.length > 1) { var lastSegment = self.body[self.body.length - 2]; newSegment.x = lastSegment.x; newSegment.y = lastSegment.y; } }; self.checkCollision = function () { var maxX = 2048; var maxY = 2732; if (self.x < 0) self.x = maxX - gridSize; if (self.x >= maxX) self.x = 0; if (self.y < 0) self.y = maxY - gridSize; if (self.y >= maxY) self.y = 0; for (var i = 0; i < self.body.length; i++) { if (self.intersects(self.body[i])) { isGameOver = true; break; } } }; }); var Game = Container.expand(function () { var self = Container.call(this); LK.stageContainer.setBackgroundColor(0x000000); var dots = []; var snake = self.addChild(new Snake()); snake.x = 1024; snake.y = 1366; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var isGameOver = false; LK.on('tick', function () { snake.move(); snake.checkCollision(); if (isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } for (var i = 0; i < dots.length; i++) { if (snake.intersects(dots[i]) && !dots[i].eaten) { snake.eat(dots[i]); dots[i].eaten = true; scoreTxt.setText(parseInt(scoreTxt.text) + 1); self.removeChild(dots[i]); dots.splice(i, 1); spawnDot(); } } }); var startSwipePos = null; stage.on('down', function (obj) { startSwipePos = obj.event.getLocalPosition(self); }); stage.on('up', function (obj) { if (!startSwipePos) return; var endSwipePos = obj.event.getLocalPosition(self); var dx = endSwipePos.x - startSwipePos.x; var dy = endSwipePos.y - startSwipePos.y; if (Math.abs(dx) > Math.abs(dy)) { snake.direction = dx > 0 ? 'right' : 'left'; } else { snake.direction = dy > 0 ? 'down' : 'up'; } startSwipePos = null; }); var spawnDot = function () { var dot = new Dot(); var gridSize = 32; dot.x = Math.round(Math.random() * (2048 / gridSize)) * gridSize; dot.y = Math.round(Math.random() * (2732 / gridSize)) * gridSize; dots.push(dot); self.addChild(dot); }; spawnDot(); });
===================================================================
--- original.js
+++ change.js
@@ -15,9 +15,9 @@
var headX = self.x;
var headY = self.y;
switch (self.direction) {
case 'right':
- self.x += gridSize;
+ self.x = Math.round((self.x + gridSize) / gridSize) * gridSize;
break;
case 'left':
self.x -= gridSize;
break;
@@ -29,10 +29,10 @@
break;
}
for (var i = self.body.length - 1; i >= 0; i--) {
if (i === 0) {
- self.body[i].x = headX;
- self.body[i].y = headY;
+ self.body[i].x = Math.round(headX / gridSize) * gridSize;
+ self.body[i].y = Math.round(headY / gridSize) * gridSize;
} else {
self.body[i].x = self.body[i - 1].x;
self.body[i].y = self.body[i - 1].y;
}
@@ -114,10 +114,10 @@
});
var spawnDot = function () {
var dot = new Dot();
var gridSize = 32;
- dot.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize + gridSize / 2;
- dot.y = Math.floor(Math.random() * (2732 / gridSize)) * gridSize + gridSize / 2;
+ dot.x = Math.round(Math.random() * (2048 / gridSize)) * gridSize;
+ dot.y = Math.round(Math.random() * (2732 / gridSize)) * gridSize;
dots.push(dot);
self.addChild(dot);
};
spawnDot();