/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
// Add a random number and operator to each obstacle
var operators = ['+', '-', '*', '/', '^', '+', '+', '+', '-', '-', '-', '*', '*', '*', '/'];
do {
self.number = Math.floor(Math.random() * 10);
self.operator = operators[Math.floor(Math.random() * operators.length)];
if (self.operator == '^') {
self.number = Math.random() < 0.9 ? 1 : 2;
}
if (self.operator == '/') {
self.number = 1 + Math.floor(Math.random() * 9);
}
if (self.operator == '*') {
// Track the count of consecutive multiplication by 0 obstacles
// Moved to global scope for broader accessibility
if (Math.random() < 0.7 && consecutiveMultiplicationByZero < 2) {
self.number = 0;
consecutiveMultiplicationByZero++; // Increment the count
} else {
// Ensure the number is not 0 to break the sequence
self.number = Math.random() < 0.5 ? 1 : 2;
consecutiveMultiplicationByZero = 0; // Reset the count
}
} else {
consecutiveMultiplicationByZero = 0; // Reset the count for non-multiplication operators
}
} while (self.operator == '/' && score % self.number != 0);
var numberText = new Text2(self.operator + self.number.toString(), {
size: 100,
fill: "#ffffff"
});
self.addChild(numberText);
numberText.anchor.set(0.5, 0.5);
numberText.x = 0;
numberText.y = 0;
self.speed = 10;
self.move = function () {
self.y += self.speed;
};
});
// Assets will be automatically created based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.moveLeft = function () {
if (self.x > leftRightSpace + railingWidth) {
self.x -= railingWidth;
}
};
self.moveRight = function () {
if (self.x < screenWidth - leftRightSpace - railingWidth) {
self.x += railingWidth;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var consecutiveMultiplicationByZero = 0; // Global counter for consecutive multiplication by 0 obstacles
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2500; // Position near the bottom
var obstacles = [];
var score = 10;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.topLeft.addChild(scoreTxt);
// Calculate the number of railings that can fit on the screen
var railingWidth = 600; // The width of a railing
var screenWidth = 2048; // The width of the screen
var numRailings = Math.floor(screenWidth / railingWidth);
// Calculate the remaining space
var remainingSpace = screenWidth - numRailings * railingWidth;
// Calculate the space to add on the left and right sides of the screen
var leftRightSpace = remainingSpace / 2;
// Draw lines at the left and right boundaries
var leftLine = LK.getAsset('leftLine', {
anchorX: 0.5,
anchorY: 0.0,
x: leftRightSpace,
y: 0
});
game.addChild(leftLine);
var rightLine = LK.getAsset('rightLine', {
anchorX: 0.5,
anchorY: 0.0,
x: screenWidth - leftRightSpace,
y: 0
});
game.addChild(rightLine);
// Create an array to store the possible x positions for the obstacles
var obstaclePositions = [];
for (var i = 0; i < numRailings; i++) {
obstaclePositions.push(leftRightSpace + i * railingWidth + railingWidth / 2);
}
// Create obstacles at intervals
var obstacleTimer;
function createObstacle() {
// Create an obstacle in each grid simultaneously
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = new Obstacle();
obstacle.x = obstaclePositions[i];
obstacle.y = 0; // Start from the top
obstacles.push(obstacle);
game.addChild(obstacle);
game.consecutiveMultiplicationByZero = 0;
}
// Calculate the interval based on the score
var interval;
// For scores less than or equal to 10, keep the original generation interval
if (score < 10) {
interval = 8000; // Original generation interval
} else {
// Adjusted interval calculation for scores greater than or equal to 10
interval = Math.max(1000, 8000 / Math.log(score));
}
// Set the next obstacle creation
obstacleTimer = LK.setTimeout(createObstacle, interval);
}
// Create the first obstacle
createObstacle();
// Handle touch events for moving the player
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
if (pos.x < 1024) {
player.moveLeft();
} else {
player.moveRight();
}
});
// Update game state every tick
LK.on('tick', function () {
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
// Check for collision with player and calculate score
if (obstacles[i].intersects(player)) {
if (!obstacles[i].scoreCalculated) {
obstacles[i].scoreCalculated = true;
switch (obstacles[i].operator) {
case '+':
score += obstacles[i].number;
break;
case '-':
score -= obstacles[i].number;
break;
case '*':
score *= obstacles[i].number;
break;
case '/':
score /= obstacles[i].number;
break;
case '^':
score = Math.pow(score, obstacles[i].number);
break;
}
scoreTxt.setText(score.toString());
if (score <= 0 || score >= 10000000) {
LK.showGameOver();
}
}
obstacles[i].destroy();
obstacles.splice(i, 1);
}
// Remove off-screen obstacles
if (obstacles[i] && obstacles[i].y > 2732) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
}); /****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
// Add a random number and operator to each obstacle
var operators = ['+', '-', '*', '/', '^', '+', '+', '+', '-', '-', '-', '*', '*', '*', '/'];
do {
self.number = Math.floor(Math.random() * 10);
self.operator = operators[Math.floor(Math.random() * operators.length)];
if (self.operator == '^') {
self.number = Math.random() < 0.9 ? 1 : 2;
}
if (self.operator == '/') {
self.number = 1 + Math.floor(Math.random() * 9);
}
if (self.operator == '*') {
// Track the count of consecutive multiplication by 0 obstacles
// Moved to global scope for broader accessibility
if (Math.random() < 0.7 && consecutiveMultiplicationByZero < 2) {
self.number = 0;
consecutiveMultiplicationByZero++; // Increment the count
} else {
// Ensure the number is not 0 to break the sequence
self.number = Math.random() < 0.5 ? 1 : 2;
consecutiveMultiplicationByZero = 0; // Reset the count
}
} else {
consecutiveMultiplicationByZero = 0; // Reset the count for non-multiplication operators
}
} while (self.operator == '/' && score % self.number != 0);
var numberText = new Text2(self.operator + self.number.toString(), {
size: 100,
fill: "#ffffff"
});
self.addChild(numberText);
numberText.anchor.set(0.5, 0.5);
numberText.x = 0;
numberText.y = 0;
self.speed = 10;
self.move = function () {
self.y += self.speed;
};
});
// Assets will be automatically created based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.moveLeft = function () {
if (self.x > leftRightSpace + railingWidth) {
self.x -= railingWidth;
}
};
self.moveRight = function () {
if (self.x < screenWidth - leftRightSpace - railingWidth) {
self.x += railingWidth;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var consecutiveMultiplicationByZero = 0; // Global counter for consecutive multiplication by 0 obstacles
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2500; // Position near the bottom
var obstacles = [];
var score = 10;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.topLeft.addChild(scoreTxt);
// Calculate the number of railings that can fit on the screen
var railingWidth = 600; // The width of a railing
var screenWidth = 2048; // The width of the screen
var numRailings = Math.floor(screenWidth / railingWidth);
// Calculate the remaining space
var remainingSpace = screenWidth - numRailings * railingWidth;
// Calculate the space to add on the left and right sides of the screen
var leftRightSpace = remainingSpace / 2;
// Draw lines at the left and right boundaries
var leftLine = LK.getAsset('leftLine', {
anchorX: 0.5,
anchorY: 0.0,
x: leftRightSpace,
y: 0
});
game.addChild(leftLine);
var rightLine = LK.getAsset('rightLine', {
anchorX: 0.5,
anchorY: 0.0,
x: screenWidth - leftRightSpace,
y: 0
});
game.addChild(rightLine);
// Create an array to store the possible x positions for the obstacles
var obstaclePositions = [];
for (var i = 0; i < numRailings; i++) {
obstaclePositions.push(leftRightSpace + i * railingWidth + railingWidth / 2);
}
// Create obstacles at intervals
var obstacleTimer;
function createObstacle() {
// Create an obstacle in each grid simultaneously
for (var i = 0; i < obstaclePositions.length; i++) {
var obstacle = new Obstacle();
obstacle.x = obstaclePositions[i];
obstacle.y = 0; // Start from the top
obstacles.push(obstacle);
game.addChild(obstacle);
game.consecutiveMultiplicationByZero = 0;
}
// Calculate the interval based on the score
var interval;
// For scores less than or equal to 10, keep the original generation interval
if (score < 10) {
interval = 8000; // Original generation interval
} else {
// Adjusted interval calculation for scores greater than or equal to 10
interval = Math.max(1000, 8000 / Math.log(score));
}
// Set the next obstacle creation
obstacleTimer = LK.setTimeout(createObstacle, interval);
}
// Create the first obstacle
createObstacle();
// Handle touch events for moving the player
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
if (pos.x < 1024) {
player.moveLeft();
} else {
player.moveRight();
}
});
// Update game state every tick
LK.on('tick', function () {
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
// Check for collision with player and calculate score
if (obstacles[i].intersects(player)) {
if (!obstacles[i].scoreCalculated) {
obstacles[i].scoreCalculated = true;
switch (obstacles[i].operator) {
case '+':
score += obstacles[i].number;
break;
case '-':
score -= obstacles[i].number;
break;
case '*':
score *= obstacles[i].number;
break;
case '/':
score /= obstacles[i].number;
break;
case '^':
score = Math.pow(score, obstacles[i].number);
break;
}
scoreTxt.setText(score.toString());
if (score <= 0 || score >= 10000000) {
LK.showGameOver();
}
}
obstacles[i].destroy();
obstacles.splice(i, 1);
}
// Remove off-screen obstacles
if (obstacles[i] && obstacles[i].y > 2732) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
});