/****
* Classes
****/
// LongObstacle class representing long obstacles in the game
var LongObstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.5,
scaleY: 20.0 // Longer height for the obstacle
});
self.speedX = -5;
// Update function for obstacle movement
self.update = function () {
self.x += self.speedX;
// Remove obstacle if it goes off screen
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Player class representing the main character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpStrength = -10;
// Update function for player movement
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
// Prevent player from falling below the ground
if (self.y > 2732 - playerGraphics.height / 2) {
self.y = 2732 - playerGraphics.height / 2;
self.speedY = 0;
}
// End the game if the player touches the top of the screen
if (self.y < playerGraphics.height / 2) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
// Jump function for the player
self.jump = function () {
self.speedY = self.jumpStrength * 2; // Increase jump strength to make the player jump longer
};
});
// Pole class representing poles in the game
var Pole = Container.expand(function () {
var self = Container.call(this);
var poleGraphics = self.attachAsset('pole', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.5,
scaleY: 15.0 // Increased height of the pole
});
self.speedX = -5;
// Update function for pole movement
self.update = function () {
self.x += self.speedX;
// Remove pole if it goes off screen
if (self.x < -poleGraphics.width / 2) {
self.destroy();
}
};
});
// ShortObstacle class representing short obstacles in the game
var ShortObstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.5,
scaleY: 5.0 // Shorter height for the obstacle
});
self.speedX = -5;
// Update function for obstacle movement
self.update = function () {
self.x += self.speedX;
// Remove obstacle if it goes off screen
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 2048 / 4;
player.y = 2732 / 2;
game.addChild(player);
// Array to keep track of obstacles
var obstacles = [];
// Array to keep track of poles
var poles = [];
// Function to spawn poles
function spawnPole() {
var obstacle;
if (Math.random() > 0.5) {
obstacle = new ShortObstacle();
} else {
obstacle = new LongObstacle();
}
obstacle.x = 2048 + obstacle.width / 2;
obstacle.y = 2732 - obstacle.height / 2;
poles.push(obstacle);
game.addChild(obstacle);
}
// Set interval to spawn poles periodically
var poleInterval = LK.setInterval(spawnPole, 2000);
// Initialize score
var score = 0;
// Create a total score counter and position it at the top of the screen
var totalScoreCounter = new Text2('Total Score: ' + score.toString(), {
size: 100,
fill: "#ffffff"
});
totalScoreCounter.anchor.set(0.5, 0);
totalScoreCounter.x = 2048 / 2;
totalScoreCounter.y = 100; // Position it below the current score counter
LK.gui.top.addChild(totalScoreCounter);
// Initialize an array to store all point records
var pointRecords = [];
// Create a score counter and position it at the top of the screen
var scoreCounter = new Text2(score.toString(), {
size: 100,
fill: "#ffffff"
});
scoreCounter.anchor.set(0.5, 0);
scoreCounter.x = 2048 / 2;
scoreCounter.y = 0;
LK.gui.top.addChild(scoreCounter);
// Handle game updates
game.update = function () {
player.update();
// Update all poles
for (var i = poles.length - 1; i >= 0; i--) {
poles[i].update();
// Check for collision with player
if (player.intersects(poles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Check if player has crossed a pole
if (player.x > poles[i].x + poles[i].width / 2 && !poles[i].crossed) {
score++;
poles[i].crossed = true; // Mark the pole as crossed
// Update the score counter
scoreCounter.setText(score.toString());
// Update the total score counter
totalScoreCounter.setText('Total Score: ' + score.toString());
// Create a point counter that appears at the pole's position
var pointCounter = new Text2('+1', {
size: 50,
fill: "#ffffff"
});
pointCounter.x = poles[i].x;
pointCounter.y = poles[i].y;
game.addChild(pointCounter);
// Make the point counter move up and fade out
var counterInterval = LK.setInterval(function () {
pointCounter.y -= 2;
pointCounter.alpha -= 0.02;
if (pointCounter.alpha <= 0) {
LK.clearInterval(counterInterval);
game.removeChild(pointCounter);
}
}, 20);
// Push the point record into the array
pointRecords.push(score);
}
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
player.jump();
};
// Clear obstacle interval on game over
game.on('gameOver', function () {
LK.clearInterval(obstacleInterval);
});