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
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
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;
var snakeGraphics = self.createAsset('snake', 'Snake Graphics', .5, .5);
self.length = 1;
self.direction = 'right';
self.body = [];
self.move = function () {
var gridSize = 128;
var oldHeadPosition = {
x: snakeGraphics.x,
y: snakeGraphics.y
};
switch (self.direction) {
case 'right':
snakeGraphics.x = Math.round((snakeGraphics.x + gridSize) / gridSize) * gridSize;
break;
case 'left':
snakeGraphics.x = Math.round((snakeGraphics.x - gridSize) / gridSize) * gridSize;
break;
case 'down':
snakeGraphics.y = Math.round((snakeGraphics.y + gridSize) / gridSize) * gridSize;
break;
case 'up':
snakeGraphics.y = Math.round((snakeGraphics.y - gridSize) / gridSize) * gridSize;
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 = 2048;
var maxY = 2732;
if (snakeGraphics.x < 0) snakeGraphics.x = maxX - gridSize;
if (snakeGraphics.x >= maxX) snakeGraphics.x = 0;
if (snakeGraphics.y < 0) snakeGraphics.y = maxY - gridSize;
if (snakeGraphics.y >= maxY) snakeGraphics.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);
var background = self.createAsset('background', 'Game Background', 0, 0);
background.width = 2048;
background.height = 2732;
self.addChild(background);
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;
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;
score++;
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)) {
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();
});