/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('Coins', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.color = 0x78be61;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Define the 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 = 2000 / 60 * 2.5 * (5 / 3);
self.color = 0x0000ff; // Adding color property to Player class
self.update = function () {
// Player update logic
};
self.moveLeft = function () {
self.x -= self.speed;
};
self.moveRight = function () {
self.x += self.speed;
};
});
// Define the Rocket class
var Rocket = Container.expand(function () {
var self = Container.call(this);
var rocketGraphics = self.attachAsset('rocket', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 20;
self.color = 0xff0000; // Adding color property to Rocket class
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Function to spawn rockets
function spawnRocket() {
var rocket = new Rocket();
rocket.x = Math.random() * 2048;
rocket.y = -50;
rockets.push(rocket);
game.addChild(rocket);
}
// Set interval to spawn rockets
var rocketInterval = LK.setInterval(spawnRocket, 600);
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add background to the game
var background = LK.getAsset('Background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
// Initialize player
var player = new Player();
player.x = 2048 / 2;
player.y = 2732 - 200;
game.addChild(player);
// Initialize coins array
var coins = [];
// Initialize rockets array
var rockets = [];
// Function to spawn coins
function spawnCoin() {
var coin = new Coin();
coin.x = Math.random() * 2048;
coin.y = -50;
coins.push(coin);
game.addChild(coin);
}
// Set interval to spawn coins
var coinInterval = LK.setInterval(spawnCoin, 2000 / 3);
// Handle player movement
game.down = function (x, y, obj) {
if (x < 2048 / 2) {
player.moveLeft();
} else {
player.moveRight();
}
};
// Update game logic
game.update = function () {
// Update player
player.update();
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
coins[i].update();
if (player.intersects(coins[i])) {
// Update score
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText(LK.getScore());
coins[i].destroy();
coins.splice(i, 1);
if (coins[i] && coins[i].color === 0x78be61 && player.intersects(coins[i])) {
// Flash screen red for 1 second (1000ms) to show game over
LK.effects.flashScreen(0xff0000, 1000);
// Show game over
LK.showGameOver();
return; // End the game update loop
}
}
}
// Update rockets
for (var i = rockets.length - 1; i >= 0; i--) {
rockets[i].update();
if (player.intersects(rockets[i])) {
// Flash screen red for 1 second (1000ms) to show game over
LK.effects.flashScreen(0xff0000, 1000);
// Show game over
LK.showGameOver();
return; // End the game update loop
}
}
};