/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { // Hero update logic }; self.move = function (x, y) { self.x = x; self.y = y; }; }); // 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 = 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 variables var hero; var obstacles = []; var scoreTxt; var score = 0; var gameOver = false; // Initialize hero hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Initialize score text scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to handle game over function handleGameOver() { gameOver = true; LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Function to spawn obstacles function spawnObstacle() { if (gameOver) return; var obstacle = new Obstacle(); obstacle.x = Math.random() * 2048; obstacle.y = -100; obstacles.push(obstacle); game.addChild(obstacle); } // Function to update score function updateScore() { score += 1; scoreTxt.setText(score); } // Game update function game.update = function () { if (gameOver) return; // Update hero hero.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (hero.intersects(obstacles[i])) { handleGameOver(); } } // Spawn new obstacles if (LK.ticks % 60 == 0) { spawnObstacle(); } // Update score if (LK.ticks % 30 == 0) { updateScore(); } }; // Handle touch/mouse move game.move = function (x, y, obj) { if (!gameOver) { hero.move(x, y); } }; // Start the game LK.setInterval(function () { if (!gameOver) { spawnObstacle(); } }, 1000);
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Hero update logic
};
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
// 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 = 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 variables
var hero;
var obstacles = [];
var scoreTxt;
var score = 0;
var gameOver = false;
// Initialize hero
hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to handle game over
function handleGameOver() {
gameOver = true;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Function to spawn obstacles
function spawnObstacle() {
if (gameOver) return;
var obstacle = new Obstacle();
obstacle.x = Math.random() * 2048;
obstacle.y = -100;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Function to update score
function updateScore() {
score += 1;
scoreTxt.setText(score);
}
// Game update function
game.update = function () {
if (gameOver) return;
// Update hero
hero.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (hero.intersects(obstacles[i])) {
handleGameOver();
}
}
// Spawn new obstacles
if (LK.ticks % 60 == 0) {
spawnObstacle();
}
// Update score
if (LK.ticks % 30 == 0) {
updateScore();
}
};
// Handle touch/mouse move
game.move = function (x, y, obj) {
if (!gameOver) {
hero.move(x, y);
}
};
// Start the game
LK.setInterval(function () {
if (!gameOver) {
spawnObstacle();
}
}, 1000);