User prompt
You lose if you collide with yourself
User prompt
Make it every 10 score that the ticks reduce
User prompt
Every 5 score decrease the ticks per move by 1, to a minimum of q
User prompt
Make snake move faster every 5 food you get, where by faster it's decreasing the number of ticks needed by 1 (keep minimum of 1 move per tick). Also slow the initial speed to 1 move per 7 ticks.
User prompt
Make score text a monospace font
User prompt
Use a variable to store the score
User prompt
Add 1 score each time you get a food
User prompt
Make the text colour #1a2314
User prompt
Add a full screen background
User prompt
Calculate old head position before adding the direction vectors
User prompt
The first piece of body should move to where the head was, but not is
User prompt
Bug: the first body piece has the same position as the head
User prompt
Instead of using virtual X/Y , just use the snakeGraphics X/y
User prompt
Never change the snakes self x/y , only change the coordinates of the snake graphics within it
User prompt
Ensure the snake wraps
User prompt
Bug: Snake gets stuck after collecting food
User prompt
Ensure in self.directuon that virtual X is used for calculating the delta, not self.x/y
User prompt
Put the head into the body as it's the first element
User prompt
Don't set self.x/y to the virtual position, only set snake graphics X/y, as well as the first element in the body
User prompt
Move the snake graphics to the virtual X/y as well. And don't reset virtual X/y to the snake x/y every move
User prompt
When calculating direction, don't move the snake x/y , only keep a virtual X/y coord
User prompt
Iterate over the body in reverse to get the correct positions
User prompt
Keep a tick counter and only update movement every 5th
User prompt
Calculate body 0's position before looping over the body (and just loop till I=1)
User prompt
Set the head position first before doing the body.
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 = 128 * 5; self.length = 1; self.direction = 'right'; self.body = [self.createAsset('snake', 'Snake Graphics', .5, .5)]; self.move = function () { var gridSize = 128; var virtualX = self.virtualX || self.x; var virtualY = self.virtualY || self.y; switch (self.direction) { case 'right': virtualX = Math.round((virtualX + gridSize) / gridSize) * gridSize; break; case 'left': virtualX = Math.round((virtualX - gridSize) / gridSize) * gridSize; break; case 'down': virtualY = Math.round((virtualY + gridSize) / gridSize) * gridSize; break; case 'up': virtualY = Math.round((virtualY - gridSize) / gridSize) * gridSize; break; } self.virtualX = virtualX; self.virtualY = virtualY; for (var i = self.body.length - 1; i > 0; i--) { self.body[i].x = self.body[i - 1].x; self.body[i].y = self.body[i - 1].y; } self.body[0].x = self.virtualX; self.body[0].y = self.virtualY; }; 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.body[0].x < 0) self.body[0].x = maxX - gridSize; if (self.body[0].x >= maxX) self.body[0].x = 0; if (self.body[0].y < 0) self.body[0].y = maxY - gridSize; if (self.body[0].y >= maxY) self.body[0].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()); var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var isGameOver = false; var tickCounter = 0; LK.on('tick', function () { tickCounter++; if (tickCounter % 5 == 0) { 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 = 128; dot.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize; dot.y = Math.floor(Math.random() * (2732 / gridSize)) * gridSize; dots.push(dot); self.addChild(dot); }; spawnDot(); });
===================================================================
--- original.js
+++ change.js