/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore()); // Update score text
self.y = 2732;
}
};
});
// Obstacle_2 class
var Obstacle_2 = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('Obstacle_2', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore()); // Update score text
self.y = 2732;
}
};
});
// Platform class
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('Plataform', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.y += self.speed;
if (self.y < 0) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore()); // Update score text
self.y = 2732;
}
};
});
// The assets will be automatically created and loaded by the LK engine
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Add background image
var background = LK.getAsset('Background', {
anchorX: 0.5,
anchorY: 0.5
});
background.x = 2048 / 2;
background.y = 2732 / 2;
game.addChild(background);
// Initialize and display the score text in the game.
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
shadow: {
color: "#000000",
blur: 10,
offsetX: 5,
offsetY: 5
}
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize platforms
var platforms = [];
for (var i = 0; i < 5; i++) {
var platform = game.addChild(new Platform());
platform.x = Math.random() * 2048;
platform.y = Math.random() * 2732;
platforms.push(platform);
}
// Variable to track platforms and obstacles that move upwards
var movingUpwards = [];
// Initialize player and obstacles
LK.setScore(0);
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
var obstacles = [];
for (var i = 0; i < 5; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
}
for (var i = 0; i < 5; i++) {
var obstacle2 = game.addChild(new Obstacle_2());
obstacle2.x = Math.random() * 2048;
obstacle2.y = Math.random() * 2732;
obstacles.push(obstacle2);
}
// Game update function
game.update = function () {
// Update player movement
player.speed += 1; // Gravity effect
player.y += player.speed;
if (player.y < 0) {
LK.getSound('Hit_sound').play(); // Play game over sound
LK.effects.flashScreen(0xff0000, 1000); // Flash screen red for 1 second
LK.showGameOver(); // Trigger game over when player moves upwards
}
// Check for collisions with obstacles
for (var i = 0; i < obstacles.length; i++) {
if (player.intersects(obstacles[i])) {
LK.getSound('Hit_sound').play(); // Play hit sound
LK.effects.flashScreen(0xff0000, 1000); // Flash screen red for 1 second
LK.showGameOver();
break;
}
}
// Check for collisions with platforms
for (var i = 0; i < platforms.length; i++) {
if (player.intersects(platforms[i])) {
player.speed = 0; // Stop player movement
player.y = platforms[i].y - player.height / 2; // Position player on top of the platform
}
}
};
// Player control
game.down = function (x, y, obj) {
player.x = x;
player.y = y;
player.speed = -15; // Make the player jump
LK.getSound('Jump_sound').play(); // Play jump sound
};
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};