/**** * Classes ****/ // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 1.0 // Anchor at the bottom for easier ground alignment }); self.speedX = -5; self.move = function () { self.x += self.speedX; }; self.isOffScreen = function () { return self.x < -obstacleGraphics.width; }; }); // Assets are automatically created based on usage in the code. // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 1.0 // Anchor at the bottom center for easier ground alignment }); self.speedY = 0; self.gravity = 0.5; self.jumpPower = -15; self.grounded = false; self.update = function () { // Apply gravity if (!self.grounded) { self.speedY += self.gravity; self.y += self.speedY; } // Check for ground if (self.y >= game.height - playerGraphics.height) { self.y = game.height - playerGraphics.height; self.grounded = true; self.speedY = 0; } }; self.jump = function () { if (self.grounded) { self.speedY = self.jumpPower; self.grounded = false; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to simulate the sky }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 200; player.y = game.height - 100; // Start position near the bottom of the screen var obstacles = []; var spawnObstacleTimer = 0; var spawnObstacleInterval = 120; // Frames until next obstacle spawns game.on('down', function (obj) { player.jump(); }); LK.on('tick', function () { player.update(); // Handle obstacle movement and cleanup for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].move(); if (obstacles[i].isOffScreen()) { obstacles[i].destroy(); obstacles.splice(i, 1); } else if (player.intersects(obstacles[i])) { // Game over logic LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn obstacles spawnObstacleTimer++; if (spawnObstacleTimer >= spawnObstacleInterval) { var obstacle = game.addChild(new Obstacle()); obstacle.x = game.width; obstacle.y = game.height - 100; // Position at ground level obstacles.push(obstacle); spawnObstacleTimer = 0; } });
/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0 // Anchor at the bottom for easier ground alignment
});
self.speedX = -5;
self.move = function () {
self.x += self.speedX;
};
self.isOffScreen = function () {
return self.x < -obstacleGraphics.width;
};
});
// Assets are automatically created based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0 // Anchor at the bottom center for easier ground alignment
});
self.speedY = 0;
self.gravity = 0.5;
self.jumpPower = -15;
self.grounded = false;
self.update = function () {
// Apply gravity
if (!self.grounded) {
self.speedY += self.gravity;
self.y += self.speedY;
}
// Check for ground
if (self.y >= game.height - playerGraphics.height) {
self.y = game.height - playerGraphics.height;
self.grounded = true;
self.speedY = 0;
}
};
self.jump = function () {
if (self.grounded) {
self.speedY = self.jumpPower;
self.grounded = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate the sky
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 200;
player.y = game.height - 100; // Start position near the bottom of the screen
var obstacles = [];
var spawnObstacleTimer = 0;
var spawnObstacleInterval = 120; // Frames until next obstacle spawns
game.on('down', function (obj) {
player.jump();
});
LK.on('tick', function () {
player.update();
// Handle obstacle movement and cleanup
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (obstacles[i].isOffScreen()) {
obstacles[i].destroy();
obstacles.splice(i, 1);
} else if (player.intersects(obstacles[i])) {
// Game over logic
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn obstacles
spawnObstacleTimer++;
if (spawnObstacleTimer >= spawnObstacleInterval) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = game.width;
obstacle.y = game.height - 100; // Position at ground level
obstacles.push(obstacle);
spawnObstacleTimer = 0;
}
});