/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Coin class var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { if (game.difficulty) { self.y += 5; // Move coin downwards if (self.y > 2732) { self.destroy(); } } }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { if (game.difficulty) { self.y += 7; // Move obstacle downwards if (self.y > 2732) { self.destroy(); } } }; }); // Pause Menu class var PauseMenu = Container.expand(function () { var self = Container.call(this); var frame = LK.getAsset('frame', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 - 250, width: 1.5 * 100 * 3, scaleY: 3.0 }); self.addChild(frame); var easyButton = self.attachAsset('easyButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 - 200, y: 2732 / 2, scaleX: 1.5, scaleY: 1.5 }); easyButton.down = function (x, y, obj) { game.difficulty = 'easy'; LK.effects.flashObject(easyButton, 0xffffff, 500); LK.getSound('fon1').play({ loop: true, volume: 1.0 }); self.destroy(); }; self.addChild(easyButton); var mediumButton = self.attachAsset('mediumButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2, scaleX: 1.5, scaleY: 1.5 }); mediumButton.down = function (x, y, obj) { game.difficulty = 'medium'; LK.effects.flashObject(mediumButton, 0xffffff, 500); LK.getSound('fon1').play({ loop: true, volume: 1.0 }); self.destroy(); }; self.addChild(mediumButton); var hardButton = self.attachAsset('hardButton', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2 + 200, y: 2732 / 2, scaleX: 1.5, scaleY: 1.5 }); hardButton.down = function (x, y, obj) { game.difficulty = 'hard'; LK.effects.flashObject(hardButton, 0xffffff, 500); LK.getSound('fon1').play({ loop: true, volume: 1.0 }); self.destroy(); }; self.addChild(hardButton); }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.move = function (x, y, obj) { var game_position = game.toLocal(obj.global); self.x = game_position.x; self.y = game_position.y; }; }); // SuperCoin class var SuperCoin = Container.expand(function () { var self = Container.call(this); var superCoinGraphics = self.attachAsset('superCoin', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { if (game.difficulty) { self.y += 5; // Move super coin downwards if (self.y > 2732) { self.destroy(); } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x808080 //Init game with gray background }); /**** * Game Code ****/ var backgroundImage = game.attachAsset('background', { anchorX: 0.0, anchorY: 0.0, x: 0, y: 0 }); game.addChild(backgroundImage); game.addChild(new PauseMenu()); // Initialize arrays and variables var coins = []; var obstacles = []; var superCoins = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; // Handle move events game.move = function (x, y, obj) { player.move(x, y, obj); }; // Update game logic game.update = function () { // Loop the song 'fon1' if (!LK.getSound('fon1').isPlaying) { LK.getSound('fon1').play({ loop: true, volume: 1.0 }); } // Update coins for (var i = coins.length - 1; i >= 0; i--) { if (coins[i].intersects(player)) { score += 1; scoreTxt.setText(score); coins[i].destroy(); coins.splice(i, 1); LK.getSound('coin').play(); } else if (coins[i].y > 2732) { coins[i].destroy(); coins.splice(i, 1); } } // Update super coins for (var i = superCoins.length - 1; i >= 0; i--) { if (superCoins[i].intersects(player)) { var superCoinValue = Math.floor(Math.random() * 6) + 5; // Generate a random number between 5 and 10 score += superCoinValue; // Increase score by the generated random number when player collects super coins scoreTxt.setText(score); superCoins[i].destroy(); superCoins.splice(i, 1); LK.getSound('coin').play(); } else if (superCoins[i].y > 2732) { superCoins[i].destroy(); superCoins.splice(i, 1); } } // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { if (obstacles[i].intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else if (obstacles[i].y > 2732) { obstacles[i].destroy(); obstacles.splice(i, 1); } } // No need to initialize superCoins array here as it is already initialized at the start of the game // Spawn coins and super coins if (LK.ticks % 60 == 0) { var newCoin = new Coin(); newCoin.x = Math.random() * 2048; newCoin.y = -50; coins.push(newCoin); game.addChild(newCoin); // Spawn super coins rarely if (Math.random() < 0.1) { var newSuperCoin = new SuperCoin(); newSuperCoin.x = Math.random() * 2048; newSuperCoin.y = -50; superCoins.push(newSuperCoin); game.addChild(newSuperCoin); } } // Spawn obstacles if (LK.ticks % (game.difficulty == 'easy' ? 90 : game.difficulty == 'medium' ? 60 : 30) == 0) { var newObstacle = new Obstacle(); newObstacle.x = Math.random() * 2048; newObstacle.y = -50; obstacles.push(newObstacle); game.addChild(newObstacle); } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (game.difficulty) {
self.y += 5; // Move coin downwards
if (self.y > 2732) {
self.destroy();
}
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (game.difficulty) {
self.y += 7; // Move obstacle downwards
if (self.y > 2732) {
self.destroy();
}
}
};
});
// Pause Menu class
var PauseMenu = Container.expand(function () {
var self = Container.call(this);
var frame = LK.getAsset('frame', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 - 250,
width: 1.5 * 100 * 3,
scaleY: 3.0
});
self.addChild(frame);
var easyButton = self.attachAsset('easyButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 - 200,
y: 2732 / 2,
scaleX: 1.5,
scaleY: 1.5
});
easyButton.down = function (x, y, obj) {
game.difficulty = 'easy';
LK.effects.flashObject(easyButton, 0xffffff, 500);
LK.getSound('fon1').play({
loop: true,
volume: 1.0
});
self.destroy();
};
self.addChild(easyButton);
var mediumButton = self.attachAsset('mediumButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
scaleX: 1.5,
scaleY: 1.5
});
mediumButton.down = function (x, y, obj) {
game.difficulty = 'medium';
LK.effects.flashObject(mediumButton, 0xffffff, 500);
LK.getSound('fon1').play({
loop: true,
volume: 1.0
});
self.destroy();
};
self.addChild(mediumButton);
var hardButton = self.attachAsset('hardButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2 + 200,
y: 2732 / 2,
scaleX: 1.5,
scaleY: 1.5
});
hardButton.down = function (x, y, obj) {
game.difficulty = 'hard';
LK.effects.flashObject(hardButton, 0xffffff, 500);
LK.getSound('fon1').play({
loop: true,
volume: 1.0
});
self.destroy();
};
self.addChild(hardButton);
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
self.x = game_position.x;
self.y = game_position.y;
};
});
// SuperCoin class
var SuperCoin = Container.expand(function () {
var self = Container.call(this);
var superCoinGraphics = self.attachAsset('superCoin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (game.difficulty) {
self.y += 5; // Move super coin downwards
if (self.y > 2732) {
self.destroy();
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x808080 //Init game with gray background
});
/****
* Game Code
****/
var backgroundImage = game.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0,
x: 0,
y: 0
});
game.addChild(backgroundImage);
game.addChild(new PauseMenu());
// Initialize arrays and variables
var coins = [];
var obstacles = [];
var superCoins = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Handle move events
game.move = function (x, y, obj) {
player.move(x, y, obj);
};
// Update game logic
game.update = function () {
// Loop the song 'fon1'
if (!LK.getSound('fon1').isPlaying) {
LK.getSound('fon1').play({
loop: true,
volume: 1.0
});
}
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
if (coins[i].intersects(player)) {
score += 1;
scoreTxt.setText(score);
coins[i].destroy();
coins.splice(i, 1);
LK.getSound('coin').play();
} else if (coins[i].y > 2732) {
coins[i].destroy();
coins.splice(i, 1);
}
}
// Update super coins
for (var i = superCoins.length - 1; i >= 0; i--) {
if (superCoins[i].intersects(player)) {
var superCoinValue = Math.floor(Math.random() * 6) + 5; // Generate a random number between 5 and 10
score += superCoinValue; // Increase score by the generated random number when player collects super coins
scoreTxt.setText(score);
superCoins[i].destroy();
superCoins.splice(i, 1);
LK.getSound('coin').play();
} else if (superCoins[i].y > 2732) {
superCoins[i].destroy();
superCoins.splice(i, 1);
}
}
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else if (obstacles[i].y > 2732) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
// No need to initialize superCoins array here as it is already initialized at the start of the game
// Spawn coins and super coins
if (LK.ticks % 60 == 0) {
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = -50;
coins.push(newCoin);
game.addChild(newCoin);
// Spawn super coins rarely
if (Math.random() < 0.1) {
var newSuperCoin = new SuperCoin();
newSuperCoin.x = Math.random() * 2048;
newSuperCoin.y = -50;
superCoins.push(newSuperCoin);
game.addChild(newSuperCoin);
}
}
// Spawn obstacles
if (LK.ticks % (game.difficulty == 'easy' ? 90 : game.difficulty == 'medium' ? 60 : 30) == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
};