Code edit (2 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Allow spawning multiple food at once, configure a global variable which stores the default number to spawn
User prompt
Migrate to the latest version of LK
User prompt
Make the snake five times as fast.
/**** 
* Classes
****/ 
var Dot = Container.expand(function () {
	var self = Container.call(this);
	var dotGraphics = self.attachAsset('dot', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.eaten = false;
});
var Level = Container.expand(function () {
	var self = Container.call(this);
	self.targetScore = 10;
	self.currentLevel = 1;
	self.levelUp = function () {
		self.currentLevel++;
		self.targetScore += 10;
	};
});
var Snake = Container.expand(function () {
	var self = Container.call(this);
	var gridSize = 100;
	var snakeGraphics = self.attachAsset('snake', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.length = 1;
	self.direction = 'right';
	self.body = [];
	self._move_migrated = 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.attachAsset('snakeBody', {
			anchorX: 0.5,
			anchorY: 0.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;
			}
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var gridSize = 100;
var marginX = 2048 % gridSize / 2;
var marginY = 2732 % gridSize / 2;
var background = game.attachAsset('background', {});
background.width = 2048 - 2048 % gridSize;
background.height = 2732 - 2732 % gridSize;
background.x = marginX;
background.y = marginY;
game.addChild(background);
game.x = marginX;
game.y = marginY;
var dots = [];
var spawnFoodCount = 1;
var snake = game.addChild(new Snake());
var level = game.addChild(new Level());
var score = 0;
var scoreTxt = new Text2(score.toString(), {
	size: 150,
	fill: "#1a2314",
	font: "'Courier New', monospace"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.top.addChild(scoreTxt);
var isGameOver = false;
var tickCounter = 0;
var moveFrequency = 8;
LK.on('tick', function () {
	tickCounter++;
	if (tickCounter % moveFrequency == 0) {
		snake._move_migrated();
		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 >= level.targetScore) {
				level.levelUp();
				moveFrequency = Math.max(1, moveFrequency - 1);
				spawnDot();
				score = 0;
			}
			scoreTxt.setText('Score: ' + score + ' Level: ' + level.currentLevel);
			scoreTxt.setText(score.toString());
			game.removeChild(dots[i]);
			dots.splice(i, 1);
			spawnDot();
		}
	}
});
var startSwipePos = null;
game.on('down', function (x, y, obj) {
	startSwipePos = game.toLocal(obj.global);
});
game.on('up', function (x, y, obj) {
	if (!startSwipePos) {
		return;
	}
	var endSwipePos = game.toLocal(obj.global);
	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 spawnDot() {
	for (var i = 0; i < spawnFoodCount; i++) {
		var dot = new Dot();
		dot.x = Math.floor(Math.random() * ((2048 - 2 * marginX - gridSize) / gridSize)) * gridSize + marginX + gridSize;
		dot.y = Math.floor(Math.random() * ((2732 - 2 * marginY - gridSize) / gridSize)) * gridSize + marginY + gridSize;
		dots.push(dot);
		game.addChild(dot);
	}
};
spawnDot(); ===================================================================
--- original.js
+++ change.js
@@ -120,9 +120,9 @@
 game.addChild(background);
 game.x = marginX;
 game.y = marginY;
 var dots = [];
-var defaultDotCount = 3;
+var spawnFoodCount = 1;
 var snake = game.addChild(new Snake());
 var level = game.addChild(new Level());
 var score = 0;
 var scoreTxt = new Text2(score.toString(), {
@@ -188,9 +188,9 @@
 	startSwipePos = null;
 });
 var gridSize = 100;
 var spawnDot = function spawnDot() {
-	for (var i = 0; i < defaultDotCount; i++) {
+	for (var i = 0; i < spawnFoodCount; i++) {
 		var dot = new Dot();
 		dot.x = Math.floor(Math.random() * ((2048 - 2 * marginX - gridSize) / gridSize)) * gridSize + marginX + gridSize;
 		dot.y = Math.floor(Math.random() * ((2732 - 2 * marginY - gridSize) / gridSize)) * gridSize + marginY + gridSize;
 		dots.push(dot);