/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Bubble class to represent each bubble in the game
var Bubble = Container.expand(function () {
var self = Container.call(this);
// Randomly choose a color for the bubble
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var color = colors[Math.floor(Math.random() * colors.length)];
// Create and attach bubble asset
var bubbleGraphics = self.attachAsset('bubble', {
color: color,
shape: 'ellipse',
width: 100,
height: 100,
anchorX: 0.5,
anchorY: 0.5
});
// Bubble properties
self.speed = 5;
self.matched = false;
// Update function for bubble movement
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 bubbles = [];
var shooter;
var score = 0;
// Create shooter
function createShooter() {
shooter = new Container();
var shooterGraphics = shooter.attachAsset('shooter', {
color: 0xffffff,
shape: 'box',
width: 100,
height: 100,
anchorX: 0.5,
anchorY: 0.5
});
shooter.x = 2048 / 2;
shooter.y = 2500;
game.addChild(shooter);
}
// Function to shoot a bubble
function shootBubble() {
var bubble = new Bubble();
bubble.x = shooter.x;
bubble.y = shooter.y;
bubbles.push(bubble);
game.addChild(bubble);
}
// Function to check for matches
function checkMatches() {
for (var i = 0; i < bubbles.length; i++) {
for (var j = i + 1; j < bubbles.length; j++) {
if (bubbles[i].color === bubbles[j].color && !bubbles[i].matched && !bubbles[j].matched) {
var dx = bubbles[i].x - bubbles[j].x;
var dy = bubbles[i].y - bubbles[j].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
bubbles[i].matched = true;
bubbles[j].matched = true;
score += 10;
}
}
}
}
}
// Function to remove matched bubbles
function removeMatchedBubbles() {
for (var i = bubbles.length - 1; i >= 0; i--) {
if (bubbles[i].matched) {
bubbles[i].destroy();
bubbles.splice(i, 1);
}
}
}
// Game update function
game.update = function () {
for (var i = 0; i < bubbles.length; i++) {
bubbles[i].update();
}
checkMatches();
removeMatchedBubbles();
};
// Event listener for shooting bubbles
game.down = function (x, y, obj) {
shootBubble();
};
// Initialize game elements
createShooter(); /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Bubble class to represent each bubble in the game
var Bubble = Container.expand(function () {
var self = Container.call(this);
// Randomly choose a color for the bubble
var colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
var color = colors[Math.floor(Math.random() * colors.length)];
// Create and attach bubble asset
var bubbleGraphics = self.attachAsset('bubble', {
color: color,
shape: 'ellipse',
width: 100,
height: 100,
anchorX: 0.5,
anchorY: 0.5
});
// Bubble properties
self.speed = 5;
self.matched = false;
// Update function for bubble movement
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 bubbles = [];
var shooter;
var score = 0;
// Create shooter
function createShooter() {
shooter = new Container();
var shooterGraphics = shooter.attachAsset('shooter', {
color: 0xffffff,
shape: 'box',
width: 100,
height: 100,
anchorX: 0.5,
anchorY: 0.5
});
shooter.x = 2048 / 2;
shooter.y = 2500;
game.addChild(shooter);
}
// Function to shoot a bubble
function shootBubble() {
var bubble = new Bubble();
bubble.x = shooter.x;
bubble.y = shooter.y;
bubbles.push(bubble);
game.addChild(bubble);
}
// Function to check for matches
function checkMatches() {
for (var i = 0; i < bubbles.length; i++) {
for (var j = i + 1; j < bubbles.length; j++) {
if (bubbles[i].color === bubbles[j].color && !bubbles[i].matched && !bubbles[j].matched) {
var dx = bubbles[i].x - bubbles[j].x;
var dy = bubbles[i].y - bubbles[j].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
bubbles[i].matched = true;
bubbles[j].matched = true;
score += 10;
}
}
}
}
}
// Function to remove matched bubbles
function removeMatchedBubbles() {
for (var i = bubbles.length - 1; i >= 0; i--) {
if (bubbles[i].matched) {
bubbles[i].destroy();
bubbles.splice(i, 1);
}
}
}
// Game update function
game.update = function () {
for (var i = 0; i < bubbles.length; i++) {
bubbles[i].update();
}
checkMatches();
removeMatchedBubbles();
};
// Event listener for shooting bubbles
game.down = function (x, y, obj) {
shootBubble();
};
// Initialize game elements
createShooter();