/**** * Classes ****/ // Obstacle class representing obstacles in the game var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speedX = -5; // Update function for obstacle movement self.update = function () { self.x += self.speedX; // Remove obstacle if it goes off screen if (self.x < -obstacleGraphics.width / 2) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Player class representing the main character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 0.5; self.jumpStrength = -10; // Update function for player movement self.update = function () { self.speedY += self.gravity; self.y += self.speedY; // Prevent player from falling below the ground if (self.y > 2732 - playerGraphics.height / 2) { self.y = 2732 - playerGraphics.height / 2; self.speedY = 0; } }; // Jump function for the player self.jump = function () { self.speedY = self.jumpStrength; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = new Player(); player.x = 2048 / 4; player.y = 2732 / 2; game.addChild(player); // Array to keep track of obstacles var obstacles = []; // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048 + obstacle.width / 2; obstacle.y = 2732 - obstacle.height / 2; obstacles.push(obstacle); game.addChild(obstacle); } // Set interval to spawn obstacles periodically var obstacleInterval = LK.setInterval(spawnObstacle, 2000); // Handle game updates game.update = function () { player.update(); // Update all obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); // Check for collision with player if (player.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } }; // Handle touch events for jumping game.down = function (x, y, obj) { player.jump(); }; // Clear obstacle interval on game over game.on('gameOver', function () { LK.clearInterval(obstacleInterval); });
/****
* Classes
****/
// Obstacle class representing obstacles in the game
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
// Update function for obstacle movement
self.update = function () {
self.x += self.speedX;
// Remove obstacle if it goes off screen
if (self.x < -obstacleGraphics.width / 2) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Player class representing the main character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpStrength = -10;
// Update function for player movement
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
// Prevent player from falling below the ground
if (self.y > 2732 - playerGraphics.height / 2) {
self.y = 2732 - playerGraphics.height / 2;
self.speedY = 0;
}
};
// Jump function for the player
self.jump = function () {
self.speedY = self.jumpStrength;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
player.x = 2048 / 4;
player.y = 2732 / 2;
game.addChild(player);
// Array to keep track of obstacles
var obstacles = [];
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048 + obstacle.width / 2;
obstacle.y = 2732 - obstacle.height / 2;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Set interval to spawn obstacles periodically
var obstacleInterval = LK.setInterval(spawnObstacle, 2000);
// Handle game updates
game.update = function () {
player.update();
// Update all obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
// Check for collision with player
if (player.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
player.jump();
};
// Clear obstacle interval on game over
game.on('gameOver', function () {
LK.clearInterval(obstacleInterval);
});