/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Class for the character
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.lane = 1; // Start in the middle lane
self.moveLeft = function () {
if (self.lane > 0) {
self.lane--;
self.x -= 2048 / 3;
}
};
self.moveRight = function () {
if (self.lane < 2) {
self.lane++;
self.x += 2048 / 3;
}
};
});
// Class for hurdles
var Hurdle = Container.expand(function () {
var self = Container.call(this);
var hurdleGraphics = self.attachAsset('hurdle', {
anchorX: 0.5,
anchorY: 0.5,
tint: 0xff0000 // Highlight hurdles by changing their color to red
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize character
var character = game.addChild(new Character());
character.x = 2048 / 2;
character.y = 2732 - 200;
// Initialize hurdles array
var hurdles = [];
// Function to create new hurdles
function createHurdle() {
// Create two hurdles at a time
for (var i = 0; i < 2; i++) {
var lane = Math.floor(Math.random() * 3);
var hurdle = new Hurdle();
hurdle.x = 2048 / 3 * lane + 2048 / 6;
hurdle.y = -100 - i * 100; // Offset each hurdle
hurdles.push(hurdle);
game.addChild(hurdle);
}
}
// Set interval to create hurdles
var hurdleInterval = LK.setInterval(createHurdle, 1000);
// Handle touch events for character movement
game.down = function (x, y, obj) {
if (x < 2048 / 2) {
character.moveLeft();
} else {
character.moveRight();
}
};
// Update game state
var speedIncrease = 0;
game.update = function () {
for (var i = hurdles.length - 1; i >= 0; i--) {
hurdles[i].speed += speedIncrease;
hurdles[i].update();
if (hurdles[i].intersects(character)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
speedIncrease += 0.001; // Increase the speed of hurdles over time
};