/**** * Classes ****/ // 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 () { // Coin update logic }; }); // 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 = LK.ticks > 600 ? -10 - Math.floor(LK.ticks / 600) : -5 - Math.floor(LK.ticks / 600); self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // 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 = 10; self.update = function () { // Player update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x0000FF //Init game with blue background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; // Arrays to hold obstacles and coins var obstacles = []; var coins = []; // Score display var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game update function game.update = function () { // Update player player.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (obstacles[i].y < -50) { obstacles[i].destroy(); obstacles.splice(i, 1); } if (player.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } else { LK.setScore(LK.getScore() + 1); scoreTxt.setText('Score: ' + LK.getScore()); } } // Update coins for (var j = coins.length - 1; j >= 0; j--) { coins[j].update(); if (coins[j].y < -50) { coins[j].destroy(); coins.splice(j, 1); } if (player.intersects(coins[j])) { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); LK.setScore(LK.getScore() + 1); scoreTxt.setText('Score: ' + LK.getScore()); coins[j].destroy(); coins.splice(j, 1); } } // Spawn obstacles if (LK.ticks % 60 == 0) { var obstacleCount = LK.ticks > 1200 ? Math.min(6 + Math.floor(LK.ticks / 1200), 10) : 1; for (var i = 0; i < obstacleCount; i++) { var newObstacle = new Obstacle(); newObstacle.speed = LK.ticks > 1200 ? -10 - Math.floor(LK.ticks / 1200) : -5 - Math.floor(LK.ticks / 1200); newObstacle.x = Math.random() * 2048; newObstacle.y = 2732; obstacles.push(newObstacle); game.addChild(newObstacle); } } // Spawn coins if (LK.ticks % 120 == 0) { var newCoin = new Coin(); newCoin.x = Math.random() * 2048; newCoin.y = 2732; coins.push(newCoin); game.addChild(newCoin); } }; // Handle player movement game.down = function (x, y, obj) { player.x = x; player.y = y; }; game.move = function (x, y, obj) { player.x = x; player.y = y; }; game.up = function (x, y, obj) { // Stop player movement };
/****
* Classes
****/
// 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 () {
// Coin update logic
};
});
// 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 = LK.ticks > 600 ? -10 - Math.floor(LK.ticks / 600) : -5 - Math.floor(LK.ticks / 600);
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// 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 = 10;
self.update = function () {
// Player update logic
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 / 2;
// Arrays to hold obstacles and coins
var obstacles = [];
var coins = [];
// Score display
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game update function
game.update = function () {
// Update player
player.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].y < -50) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
if (player.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
} else {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
}
}
// Update coins
for (var j = coins.length - 1; j >= 0; j--) {
coins[j].update();
if (coins[j].y < -50) {
coins[j].destroy();
coins.splice(j, 1);
}
if (player.intersects(coins[j])) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.setScore(LK.getScore() + 1);
scoreTxt.setText('Score: ' + LK.getScore());
coins[j].destroy();
coins.splice(j, 1);
}
}
// Spawn obstacles
if (LK.ticks % 60 == 0) {
var obstacleCount = LK.ticks > 1200 ? Math.min(6 + Math.floor(LK.ticks / 1200), 10) : 1;
for (var i = 0; i < obstacleCount; i++) {
var newObstacle = new Obstacle();
newObstacle.speed = LK.ticks > 1200 ? -10 - Math.floor(LK.ticks / 1200) : -5 - Math.floor(LK.ticks / 1200);
newObstacle.x = Math.random() * 2048;
newObstacle.y = 2732;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
}
// Spawn coins
if (LK.ticks % 120 == 0) {
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = 2732;
coins.push(newCoin);
game.addChild(newCoin);
}
};
// Handle player movement
game.down = function (x, y, obj) {
player.x = x;
player.y = y;
};
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
game.up = function (x, y, obj) {
// Stop player movement
};