/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Balloon = Container.expand(function (number, color) { var self = Container.call(this); self.number = number; self.isTarget = false; self.isSelected = false; var balloonGraphics = self.attachAsset('balloon', { anchorX: 0.5, anchorY: 0.5, tint: color || 0x4CAF50 }); var numberText = new Text2(number.toString(), { size: 36, fill: 0xFFFFFF }); numberText.anchor.set(0.5, 0.5); self.addChild(numberText); self.down = function (x, y, obj) { if (!self.isTarget && !gameOver) { selectBalloon(self); } }; self.pop = function () { LK.getSound('pop').play(); tween(self, { scaleX: 0, scaleY: 0, alpha: 0 }, { duration: 300, easing: tween.easeOut, onFinish: function onFinish() { self.destroy(); } }); }; self.highlight = function () { tween(balloonGraphics, { tint: 0xFFEB3B }, { duration: 200 }); }; self.unhighlight = function () { tween(balloonGraphics, { tint: self.isTarget ? 0x2E7D32 : 0x4CAF50 }, { duration: 200 }); }; return self; }); var TargetBalloon = Container.expand(function (number) { var self = Container.call(this); self.number = number; self.isTarget = true; var balloonGraphics = self.attachAsset('targetBalloon', { anchorX: 0.5, anchorY: 0.5, tint: 0x2E7D32 }); var numberText = new Text2(number.toString(), { size: 48, fill: 0xFFFFFF }); numberText.anchor.set(0.5, 0.5); self.addChild(numberText); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var balloons = []; var targetBalloon = null; var selectedBalloons = []; var correctAnswers = 0; var gameOver = false; var targetNumber = 12; // UI Elements var scoreTxt = new Text2('Score: 0', { size: 60, fill: 0x2E7D32 }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); scoreTxt.y = 50; var instructionTxt = new Text2('Find two balloons that multiply to make the target!', { size: 36, fill: 0x2E7D32 }); instructionTxt.anchor.set(0.5, 0); LK.gui.top.addChild(instructionTxt); instructionTxt.y = 120; function generateRandomNumber() { return Math.floor(Math.random() * 12) + 1; } function generateTargetNumber() { var factors = [[2, 6], [3, 4], [2, 8], [3, 5], [4, 4], [2, 9], [3, 6], [4, 5], [2, 10], [3, 7], [4, 6], [5, 5], [2, 12], [3, 8], [4, 7], [5, 6]]; var pair = factors[Math.floor(Math.random() * factors.length)]; return pair[0] * pair[1]; } function createBalloons() { // Clear existing balloons for (var i = 0; i < balloons.length; i++) { balloons[i].destroy(); } balloons = []; if (targetBalloon) { targetBalloon.destroy(); } // Create target balloon targetNumber = generateTargetNumber(); targetBalloon = new TargetBalloon(targetNumber); targetBalloon.x = 1024; targetBalloon.y = 600; game.addChild(targetBalloon); // Create answer balloons (ensure at least one correct pair exists) var correctPairs = getFactorPairs(targetNumber); var correctPair = correctPairs[Math.floor(Math.random() * correctPairs.length)]; // Add the correct pair var balloon1 = new Balloon(correctPair[0], getRandomBalloonColor()); var balloon2 = new Balloon(correctPair[1], getRandomBalloonColor()); balloons.push(balloon1); balloons.push(balloon2); // Add random balloons for (var i = 0; i < 10; i++) { var number = generateRandomNumber(); var balloon = new Balloon(number, getRandomBalloonColor()); balloons.push(balloon); } // Position balloons in a circle around the target var radius = 400; var centerX = 1024; var centerY = 1400; for (var i = 0; i < balloons.length; i++) { var angle = i / balloons.length * Math.PI * 2; balloons[i].x = centerX + Math.cos(angle) * radius; balloons[i].y = centerY + Math.sin(angle) * radius; game.addChild(balloons[i]); // Add entrance animation balloons[i].scaleX = 0; balloons[i].scaleY = 0; tween(balloons[i], { scaleX: 1, scaleY: 1 }, { duration: 500, easing: tween.bounceOut }); } } function getFactorPairs(number) { var pairs = []; for (var i = 1; i <= number; i++) { if (number % i === 0) { var factor = number / i; if (i <= factor && i <= 12 && factor <= 12) { pairs.push([i, factor]); } } } return pairs; } function getRandomBalloonColor() { var colors = [0xFF5722, 0x9C27B0, 0x3F51B5, 0x00BCD4, 0x4CAF50, 0xFFEB3B, 0xFF9800]; return colors[Math.floor(Math.random() * colors.length)]; } function selectBalloon(balloon) { if (selectedBalloons.length < 2 && selectedBalloons.indexOf(balloon) === -1) { selectedBalloons.push(balloon); balloon.isSelected = true; balloon.highlight(); if (selectedBalloons.length === 2) { LK.setTimeout(checkAnswer, 500); } } } function checkAnswer() { if (selectedBalloons.length === 2) { var num1 = selectedBalloons[0].number; var num2 = selectedBalloons[1].number; var product = num1 * num2; if (product === targetNumber) { // Correct answer LK.getSound('correct').play(); correctAnswers++; LK.setScore(correctAnswers); scoreTxt.setText('Score: ' + correctAnswers); // Pop the selected balloons selectedBalloons[0].pop(); selectedBalloons[1].pop(); // Flash screen green LK.effects.flashScreen(0x4CAF50, 500); // Check win condition if (correctAnswers >= 10) { gameOver = true; LK.setTimeout(function () { LK.showYouWin(); }, 1000); } else { // Generate new puzzle after a delay LK.setTimeout(createBalloons, 1000); } } else { // Wrong answer LK.getSound('wrong').play(); // Unhighlight balloons selectedBalloons[0].unhighlight(); selectedBalloons[1].unhighlight(); // Flash screen red briefly LK.effects.flashScreen(0xF44336, 300); } // Reset selection selectedBalloons[0].isSelected = false; selectedBalloons[1].isSelected = false; selectedBalloons = []; } } // Initialize the game createBalloons(); game.update = function () { // Game loop - no specific updates needed for this game };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Balloon = Container.expand(function (number, color) {
var self = Container.call(this);
self.number = number;
self.isTarget = false;
self.isSelected = false;
var balloonGraphics = self.attachAsset('balloon', {
anchorX: 0.5,
anchorY: 0.5,
tint: color || 0x4CAF50
});
var numberText = new Text2(number.toString(), {
size: 36,
fill: 0xFFFFFF
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
self.down = function (x, y, obj) {
if (!self.isTarget && !gameOver) {
selectBalloon(self);
}
};
self.pop = function () {
LK.getSound('pop').play();
tween(self, {
scaleX: 0,
scaleY: 0,
alpha: 0
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.destroy();
}
});
};
self.highlight = function () {
tween(balloonGraphics, {
tint: 0xFFEB3B
}, {
duration: 200
});
};
self.unhighlight = function () {
tween(balloonGraphics, {
tint: self.isTarget ? 0x2E7D32 : 0x4CAF50
}, {
duration: 200
});
};
return self;
});
var TargetBalloon = Container.expand(function (number) {
var self = Container.call(this);
self.number = number;
self.isTarget = true;
var balloonGraphics = self.attachAsset('targetBalloon', {
anchorX: 0.5,
anchorY: 0.5,
tint: 0x2E7D32
});
var numberText = new Text2(number.toString(), {
size: 48,
fill: 0xFFFFFF
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var balloons = [];
var targetBalloon = null;
var selectedBalloons = [];
var correctAnswers = 0;
var gameOver = false;
var targetNumber = 12;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0x2E7D32
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
var instructionTxt = new Text2('Find two balloons that multiply to make the target!', {
size: 36,
fill: 0x2E7D32
});
instructionTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionTxt);
instructionTxt.y = 120;
function generateRandomNumber() {
return Math.floor(Math.random() * 12) + 1;
}
function generateTargetNumber() {
var factors = [[2, 6], [3, 4], [2, 8], [3, 5], [4, 4], [2, 9], [3, 6], [4, 5], [2, 10], [3, 7], [4, 6], [5, 5], [2, 12], [3, 8], [4, 7], [5, 6]];
var pair = factors[Math.floor(Math.random() * factors.length)];
return pair[0] * pair[1];
}
function createBalloons() {
// Clear existing balloons
for (var i = 0; i < balloons.length; i++) {
balloons[i].destroy();
}
balloons = [];
if (targetBalloon) {
targetBalloon.destroy();
}
// Create target balloon
targetNumber = generateTargetNumber();
targetBalloon = new TargetBalloon(targetNumber);
targetBalloon.x = 1024;
targetBalloon.y = 600;
game.addChild(targetBalloon);
// Create answer balloons (ensure at least one correct pair exists)
var correctPairs = getFactorPairs(targetNumber);
var correctPair = correctPairs[Math.floor(Math.random() * correctPairs.length)];
// Add the correct pair
var balloon1 = new Balloon(correctPair[0], getRandomBalloonColor());
var balloon2 = new Balloon(correctPair[1], getRandomBalloonColor());
balloons.push(balloon1);
balloons.push(balloon2);
// Add random balloons
for (var i = 0; i < 10; i++) {
var number = generateRandomNumber();
var balloon = new Balloon(number, getRandomBalloonColor());
balloons.push(balloon);
}
// Position balloons in a circle around the target
var radius = 400;
var centerX = 1024;
var centerY = 1400;
for (var i = 0; i < balloons.length; i++) {
var angle = i / balloons.length * Math.PI * 2;
balloons[i].x = centerX + Math.cos(angle) * radius;
balloons[i].y = centerY + Math.sin(angle) * radius;
game.addChild(balloons[i]);
// Add entrance animation
balloons[i].scaleX = 0;
balloons[i].scaleY = 0;
tween(balloons[i], {
scaleX: 1,
scaleY: 1
}, {
duration: 500,
easing: tween.bounceOut
});
}
}
function getFactorPairs(number) {
var pairs = [];
for (var i = 1; i <= number; i++) {
if (number % i === 0) {
var factor = number / i;
if (i <= factor && i <= 12 && factor <= 12) {
pairs.push([i, factor]);
}
}
}
return pairs;
}
function getRandomBalloonColor() {
var colors = [0xFF5722, 0x9C27B0, 0x3F51B5, 0x00BCD4, 0x4CAF50, 0xFFEB3B, 0xFF9800];
return colors[Math.floor(Math.random() * colors.length)];
}
function selectBalloon(balloon) {
if (selectedBalloons.length < 2 && selectedBalloons.indexOf(balloon) === -1) {
selectedBalloons.push(balloon);
balloon.isSelected = true;
balloon.highlight();
if (selectedBalloons.length === 2) {
LK.setTimeout(checkAnswer, 500);
}
}
}
function checkAnswer() {
if (selectedBalloons.length === 2) {
var num1 = selectedBalloons[0].number;
var num2 = selectedBalloons[1].number;
var product = num1 * num2;
if (product === targetNumber) {
// Correct answer
LK.getSound('correct').play();
correctAnswers++;
LK.setScore(correctAnswers);
scoreTxt.setText('Score: ' + correctAnswers);
// Pop the selected balloons
selectedBalloons[0].pop();
selectedBalloons[1].pop();
// Flash screen green
LK.effects.flashScreen(0x4CAF50, 500);
// Check win condition
if (correctAnswers >= 10) {
gameOver = true;
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
} else {
// Generate new puzzle after a delay
LK.setTimeout(createBalloons, 1000);
}
} else {
// Wrong answer
LK.getSound('wrong').play();
// Unhighlight balloons
selectedBalloons[0].unhighlight();
selectedBalloons[1].unhighlight();
// Flash screen red briefly
LK.effects.flashScreen(0xF44336, 300);
}
// Reset selection
selectedBalloons[0].isSelected = false;
selectedBalloons[1].isSelected = false;
selectedBalloons = [];
}
}
// Initialize the game
createBalloons();
game.update = function () {
// Game loop - no specific updates needed for this game
};