/**** * Classes ****/ // Bullet class for the projectiles var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -10; self.update = function () { self.y += self.speed; }; }); //<Assets used in the game will automatically appear here> // Mushroom class representing the target objects var Mushroom = Container.expand(function () { var self = Container.call(this); var mushroomGraphics = self.attachAsset('mushroom', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Logic for mushroom behavior can be added here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Add background image var background = LK.getAsset('backgroundImage', { anchorX: 0.5, anchorY: 0.5, scaleX: 1, scaleY: 1, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); // Initialize variables var mushrooms = []; var bullets = []; var score = 0; // Create score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to spawn mushrooms function spawnMushroom() { var mushroom = new Mushroom(); mushroom.x = Math.random() * 2048; mushroom.y = Math.random() * 2732; mushrooms.push(mushroom); game.addChild(mushroom); } // Function to handle shooting function shootBullet(x, y) { var bullet = new Bullet(); bullet.x = x; bullet.y = y; bullets.push(bullet); game.addChild(bullet); } // Game update loop game.update = function () { // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); if (bullets[i].y < -50) { bullets[i].destroy(); bullets.splice(i, 1); } } // Check for collisions for (var i = mushrooms.length - 1; i >= 0; i--) { for (var j = bullets.length - 1; j >= 0; j--) { if (mushrooms[i].intersects(bullets[j])) { mushrooms[i].destroy(); bullets[j].destroy(); mushrooms.splice(i, 1); bullets.splice(j, 1); score++; scoreTxt.setText(score); break; } } } // Spawn new mushrooms periodically if (LK.ticks % 60 == 0) { spawnMushroom(); } }; // Handle touch events for shooting game.down = function (x, y, obj) { shootBullet(x, y); };
/****
* Classes
****/
// Bullet class for the projectiles
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
});
//<Assets used in the game will automatically appear here>
// Mushroom class representing the target objects
var Mushroom = Container.expand(function () {
var self = Container.call(this);
var mushroomGraphics = self.attachAsset('mushroom', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Logic for mushroom behavior can be added here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Add background image
var background = LK.getAsset('backgroundImage', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1,
scaleY: 1,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
// Initialize variables
var mushrooms = [];
var bullets = [];
var score = 0;
// Create score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn mushrooms
function spawnMushroom() {
var mushroom = new Mushroom();
mushroom.x = Math.random() * 2048;
mushroom.y = Math.random() * 2732;
mushrooms.push(mushroom);
game.addChild(mushroom);
}
// Function to handle shooting
function shootBullet(x, y) {
var bullet = new Bullet();
bullet.x = x;
bullet.y = y;
bullets.push(bullet);
game.addChild(bullet);
}
// Game update loop
game.update = function () {
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].update();
if (bullets[i].y < -50) {
bullets[i].destroy();
bullets.splice(i, 1);
}
}
// Check for collisions
for (var i = mushrooms.length - 1; i >= 0; i--) {
for (var j = bullets.length - 1; j >= 0; j--) {
if (mushrooms[i].intersects(bullets[j])) {
mushrooms[i].destroy();
bullets[j].destroy();
mushrooms.splice(i, 1);
bullets.splice(j, 1);
score++;
scoreTxt.setText(score);
break;
}
}
}
// Spawn new mushrooms periodically
if (LK.ticks % 60 == 0) {
spawnMushroom();
}
};
// Handle touch events for shooting
game.down = function (x, y, obj) {
shootBullet(x, y);
};