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.
User prompt
Make it ten times slower.
User prompt
Make the snake four times as fast.
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
===================================================================
--- original.js
+++ change.js
@@ -1,13 +1,22 @@
+/****
+* Classes
+****/
var Dot = Container.expand(function () {
var self = Container.call(this);
- var dotGraphics = self.createAsset('dot', 'Dot Graphics', .5, .5);
+ var dotGraphics = self.attachAsset('dot', {
+ anchorX: 0.5,
+ anchorY: 0.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);
+ var snakeGraphics = self.attachAsset('snake', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
self.length = 1;
self.direction = 'right';
self.body = [];
self.move = function () {
@@ -42,9 +51,12 @@
}
};
self.eat = function (dot) {
self.length++;
- var newSegment = self.createAsset('snakeBody', 'Snake Body Segment', .5, .5);
+ 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];
@@ -56,12 +68,20 @@
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;
+ 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;
@@ -77,87 +97,101 @@
self.currentLevel++;
self.targetScore += 10;
};
});
-var Game = Container.expand(function () {
- var gridSize = 100;
- var self = Container.call(this);
- var marginX = 2048 % gridSize / 2;
- var marginY = 2732 % gridSize / 2;
- 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);
- self.x = marginX;
- self.y = marginY;
- var dots = [];
- var snake = self.addChild(new Snake());
- var level = self.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.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 >= level.targetScore) {
- level.levelUp();
- moveFrequency = Math.max(1, moveFrequency - 1);
- spawnDot();
- score = 0;
- }
- scoreTxt.setText('Score: ' + score + ' Level: ' + level.currentLevel);
- scoreTxt.setText(score.toString());
- self.removeChild(dots[i]);
- dots.splice(i, 1);
+
+/****
+* 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 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.topCenter.addChild(scoreTxt);
+var isGameOver = false;
+var tickCounter = 0;
+var moveFrequency = 35;
+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 >= 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;
- 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';
+ }
+});
+var startSwipePos = null;
+game.on('down', function (obj) {
+ startSwipePos = obj.event.getLocalPosition(game);
+});
+game.on('up', function (obj) {
+ if (!startSwipePos) {
+ return;
+ }
+ var endSwipePos = obj.event.getLocalPosition(game);
+ 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;
}
- startSwipePos = null;
- });
- var gridSize = 100;
- var spawnDot = function () {
- 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);
- self.addChild(dot);
- };
- spawnDot();
+ 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() {
+ 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();
\ No newline at end of file