/**** * Classes ****/ // Class for coins 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 }; }); // Class for obstacles 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; }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for the player character 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: 0x87CEEB // Light blue background to represent the sky }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; // Arrays to hold obstacles and coins var obstacles = []; var coins = []; // Function to handle player movement function handleMove(x, y, obj) { player.x = x; player.y = y; } // Add event listeners for player movement game.down = function (x, y, obj) { handleMove(x, y, obj); }; game.move = function (x, y, obj) { handleMove(x, y, obj); }; // Game update function game.update = function () { // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { var obstacle = obstacles[i]; obstacle.update(); if (obstacle.y < -50) { obstacle.destroy(); obstacles.splice(i, 1); } } // Update coins for (var j = coins.length - 1; j >= 0; j--) { var coin = coins[j]; coin.update(); if (coin.intersects(player)) { LK.setScore(LK.getScore() + 1); coin.destroy(); coins.splice(j, 1); } } // Spawn new obstacles if (LK.ticks % 60 == 0) { var newObstacle = new Obstacle(); newObstacle.x = Math.random() * 2048; newObstacle.y = 2732; obstacles.push(newObstacle); game.addChild(newObstacle); } // Spawn new coins if (LK.ticks % 120 == 0) { var newCoin = new Coin(); newCoin.x = Math.random() * 2048; newCoin.y = 2732; coins.push(newCoin); game.addChild(newCoin); } }; // Display score var scoreTxt = new Text2('0', { size: 100, fill: 0xFFFFFF }); scoreTxt.setText(LK.getScore()); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt);
/****
* Classes
****/
// Class for coins
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
};
});
// Class for obstacles
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;
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the player character
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: 0x87CEEB // Light blue background to represent the sky
});
/****
* Game Code
****/
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Arrays to hold obstacles and coins
var obstacles = [];
var coins = [];
// Function to handle player movement
function handleMove(x, y, obj) {
player.x = x;
player.y = y;
}
// Add event listeners for player movement
game.down = function (x, y, obj) {
handleMove(x, y, obj);
};
game.move = function (x, y, obj) {
handleMove(x, y, obj);
};
// Game update function
game.update = function () {
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
if (obstacle.y < -50) {
obstacle.destroy();
obstacles.splice(i, 1);
}
}
// Update coins
for (var j = coins.length - 1; j >= 0; j--) {
var coin = coins[j];
coin.update();
if (coin.intersects(player)) {
LK.setScore(LK.getScore() + 1);
coin.destroy();
coins.splice(j, 1);
}
}
// Spawn new obstacles
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = 2732;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Spawn new coins
if (LK.ticks % 120 == 0) {
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = 2732;
coins.push(newCoin);
game.addChild(newCoin);
}
};
// Display score
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);