User prompt
Make the snake 2x as fast
User prompt
Make the snake 5x slower
User prompt
Do number 3
User prompt
Ensure food never spawns on the margin
Code edit (1 edits merged)
Please save this source code
User prompt
Wrap the snake when it touches the margins rather than just going beyond them
User prompt
Wrap the snake when it goes beyond greater than or equal to the margin
User prompt
Wrap the snake when it's less than or equal to the margin
User prompt
Calculate marginX/Y before setting the background position
User prompt
Declare grid size at the start of the game method so it's defined before setting background width/height
User prompt
Background starting position should be adjusted too
User prompt
The background size should match the grid given to the snake
User prompt
Ensure the margin is added to the snake coordinates too
User prompt
Fix Bug: 'ReferenceError: marginX is not defined' in this line: 'if (snakeGraphics.x < marginX) snakeGraphics.x = maxX - gridSize + marginX;' Line Number: 56
User prompt
Move the game's x/y so that it has margin , so that the grid available to the snake which is divisible by 100 is centred
User prompt
Ensure maxX/y used by the snake is divisible by 100.
User prompt
Make grid size 100 instead of 128*5
User prompt
Fix Bug: 'Uncaught ReferenceError: gridSize is not defined' in this line: 'dot.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize;' Line Number: 131
User prompt
Change snake grid Size to 100 and only declare it once
User prompt
Only allow turning, i.e. from down direction you can only go to left/right, etc.
User prompt
Prevent going in the reverse direction to where you are facing
User prompt
Do self collision by checking the head against each body piece
User prompt
Do LK gameover inside snake, as the game doesn't know about that variable
User prompt
Call the LK gameover when it's gameover
User prompt
You lose if you collide with yourself
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 = 100; var snakeGraphics = self.createAsset('snake', 'Snake Graphics', .5, .5); self.length = 1; self.direction = 'right'; self.body = []; self.move = function () { var gridSize = 100; var oldHeadPosition = { x: snakeGraphics.x, y: snakeGraphics.y }; var marginX = 2048 % gridSize / 2; var marginY = 2732 % gridSize / 2; switch (self.direction) { case 'right': snakeGraphics.x = Math.round((snakeGraphics.x + gridSize - marginX) / gridSize) * gridSize + marginX; break; case 'left': snakeGraphics.x = Math.round((snakeGraphics.x - gridSize - marginX) / gridSize) * gridSize + marginX; break; case 'down': snakeGraphics.y = Math.round((snakeGraphics.y + gridSize - marginY) / gridSize) * gridSize + marginY; break; case 'up': snakeGraphics.y = Math.round((snakeGraphics.y - gridSize - marginY) / gridSize) * gridSize + marginY; break; } 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; } if (self.body.length > 0) { self.body[0].x = oldHeadPosition.x; self.body[0].y = oldHeadPosition.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 = 2000; var maxY = 2700; var marginX = 2048 % gridSize / 2; var marginY = 2732 % gridSize / 2; if (snakeGraphics.x < marginX) snakeGraphics.x = maxX - gridSize + marginX; if (snakeGraphics.x >= maxX + marginX) snakeGraphics.x = marginX; if (snakeGraphics.y < marginY) snakeGraphics.y = maxY - gridSize + marginY; if (snakeGraphics.y >= maxY + marginY) snakeGraphics.y = marginY; for (var i = 0; i < self.body.length; i++) { if (snakeGraphics.x === self.body[i].x && snakeGraphics.y === self.body[i].y) { LK.showGameOver(); break; } } }; }); var Game = Container.expand(function () { var self = Container.call(this); var background = self.createAsset('background', 'Game Background', 0, 0); background.width = 2048 - 2048 % gridSize; background.height = 2732 - 2732 % gridSize; background.x = marginX; background.y = marginY; self.addChild(background); var gridSize = 100; var marginX = 2048 % gridSize / 2; var marginY = 2732 % gridSize / 2; self.x = marginX; self.y = marginY; var dots = []; var snake = self.addChild(new Snake()); var score = 0; var scoreTxt = new Text2(score.toString(), { size: 150, fill: "#1a2314", font: "'Courier New', monospace" }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var isGameOver = false; var tickCounter = 0; var moveFrequency = 7; LK.on('tick', function () { tickCounter++; if (tickCounter % moveFrequency == 0) { snake.move(); snake.checkCollision(); } if (isGameOver) { 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; score++; if (score % 10 == 0 && moveFrequency > 1) { moveFrequency = Math.max(1, moveFrequency - 1); } scoreTxt.setText(score.toString()); 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)) { if (snake.direction !== 'up' && snake.direction !== 'down') return; snake.direction = dx > 0 ? 'right' : 'left'; } else { if (snake.direction !== 'right' && snake.direction !== 'left') return; snake.direction = dy > 0 ? 'down' : 'up'; } startSwipePos = null; }); var gridSize = 100; var spawnDot = function () { var dot = new Dot(); dot.x = Math.floor(Math.random() * ((2048 - 2 * marginX) / gridSize)) * gridSize + marginX; dot.y = Math.floor(Math.random() * ((2732 - 2 * marginY) / gridSize)) * gridSize + marginY; dots.push(dot); self.addChild(dot); }; spawnDot(); });
===================================================================
--- original.js
+++ change.js
@@ -73,8 +73,10 @@
var self = Container.call(this);
var background = self.createAsset('background', 'Game Background', 0, 0);
background.width = 2048 - 2048 % gridSize;
background.height = 2732 - 2732 % gridSize;
+ background.x = marginX;
+ background.y = marginY;
self.addChild(background);
var gridSize = 100;
var marginX = 2048 % gridSize / 2;
var marginY = 2732 % gridSize / 2;