/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Obstacle class to represent 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.speed = 5; // Speed at which the obstacle moves // Update function to move the obstacle self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // Player class to represent the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); // Function to move the player self.move = function (x, y) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; var obstacles = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to handle game updates game.update = function () { // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (player.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn new obstacles if (LK.ticks % 60 == 0) { var newObstacle = new Obstacle(); newObstacle.x = Math.random() * 2048; newObstacle.y = -50; obstacles.push(newObstacle); game.addChild(newObstacle); } // Update score score += 1; scoreTxt.setText(score); }; // Handle player movement game.down = function (x, y, obj) { player.move(x, y); }; game.move = function (x, y, obj) { player.move(x, y); }; game.up = function (x, y, obj) { // No action needed on up event };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Obstacle class to represent 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.speed = 5; // Speed at which the obstacle moves
// Update function to move the obstacle
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Player class to represent the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
// Function to move the player
self.move = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to handle game updates
game.update = function () {
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (player.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Update score
score += 1;
scoreTxt.setText(score);
};
// Handle player movement
game.down = function (x, y, obj) {
player.move(x, y);
};
game.move = function (x, y, obj) {
player.move(x, y);
};
game.up = function (x, y, obj) {
// No action needed on up event
};