/****
* Classes
****/
// Dot class representing the main ship
var Dot = Container.expand(function () {
var self = Container.call(this);
var dotGraphics = self.attachAsset('dot', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10; // Speed of the dot
// Update method to move the dot
self.update = function () {
// Logic for updating dot position can be added here
};
});
//<Assets used in the game will automatically appear here>
// 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.speed = 13; // Speed of the obstacle
// Update method to move the obstacle
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
// Initialize game variables
var obstacles = [];
var score = 0;
var scoreTxt;
// Initialize the main ship as a dot
mainShip = null;
// Function to initialize game elements
function initGame() {
// Create score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize the main ship as a dot
mainShip = new Dot();
mainShip.x = 2048 / 2;
mainShip.y = 2732 - 100; // Position the main ship near the bottom of the screen
game.addChild(mainShip);
}
// Function to handle game updates
game.update = function () {
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].y > 2732) {
LK.showGameOver();
} else if (mainShip.intersects(obstacles[i])) {
obstacles[i].destroy();
obstacles.splice(i, 1);
score++;
scoreTxt.setText('Score: ' + score);
}
}
// Update main ship
mainShip.update();
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Spawn new dots
};
// Event listener for touch/mouse down
game.down = function (x, y, obj) {
// Move the main ship to the touch/mouse position
mainShip.x = x;
mainShip.y = y;
// Check for obstacle intersections
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].intersects({
x: x,
y: y
})) {
obstacles[i].destroy(); // Destroy the obstacle
obstacles.splice(i, 1); // Remove the obstacle from the array
score++; // Increase the score
scoreTxt.setText('Score: ' + score); // Update the score text
}
}
};
// Initialize game elements
initGame();