/**** * Classes ****/ var BackgroundContainer = Container.expand(function () { var self = Container.call(this); var backgroundImage = self.attachAsset('background', { anchorX: 0.5, anchorY: 0.5 }); backgroundImage.x = 2048 / 2; backgroundImage.y = 2732 / 2; }); //<Assets used in the game will automatically appear here> // Button class var Button = Container.expand(function (buttonId) { var self = Container.call(this); var buttonGraphics = self.attachAsset(buttonId, { anchorX: 0.5, anchorY: 0.5 }); // Add speed property to Button class self.speed = { x: Math.random() * 2 - 1, y: Math.random() * 2 - 1 }; self.update = function () { // Move the button self.x += self.speed.x; self.y += self.speed.y; // Bounce the button off the screen edges if (self.x - buttonGraphics.width / 2 < 0 || self.x + buttonGraphics.width / 2 > 2048) { self.speed.x *= -1; } if (self.y - buttonGraphics.height / 2 < 0 || self.y + buttonGraphics.height / 2 > 2732) { self.speed.y *= -1; } // Bounce the button off the center area if (Math.abs(self.x - 2048 / 2) < 250 && Math.abs(self.y - 2732 / 2) < 250) { self.speed.x *= -1; self.speed.y *= -1; } }; self.down = function (x, y, obj) { // When the button is clicked, increase the score and move the button to a new random position if (buttonId === currentButtonId) { level += 1; levelTxt.setText(level); self.x = Math.random() * (2048 - buttonGraphics.width) + buttonGraphics.width / 2; self.y = Math.random() * (2732 - buttonGraphics.height - 300) + buttonGraphics.height / 2 + 300; updateButtons(); // Play the 'Correct' sound LK.getSound('Correct').play(); } else { LK.showGameOver(); } }; }); var ButtonDisplay = Container.expand(function () { var self = Container.call(this); var currentButtonId = null; var buttonGraphics = null; self.updateButton = function (buttonId) { if (buttonGraphics) { self.removeChild(buttonGraphics); } currentButtonId = buttonId; buttonGraphics = self.attachAsset(buttonId, { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 3 }); buttonGraphics.scale.set(3, 3); buttonGraphics.x = 2048 / 2; buttonGraphics.y = 0; // Position at the top center // Start the animation of the button shrinking self.timerValue = 200; self.updateButtonSize = function (timerValue) { var scale = timerValue / 200; buttonGraphics.scale.set(scale, scale); }; }; }); var ForegroundContainer = Container.expand(function () { var self = Container.call(this); }); var MidgroundContainer = Container.expand(function () { var self = Container.call(this); }); // Timer class var Timer = Container.expand(function () { var self = Container.call(this); var timerValue = 200; self.update = function () { if (timerValue > 0) { timerValue -= 1; buttonDisplay.updateButtonSize(timerValue); // Update the button display as the timer decreases buttonDisplay.updateButtonSize(timerValue); } else { LK.showGameOver(); } }; self.reset = function () { timerValue = 200; buttonDisplay.updateButtonSize(200); // Reset the button size to its original size }; }); var UI = Container.expand(function () { var self = Container.call(this); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize buttons array var buttons = []; // Initialize and add BackgroundContainer, MidgroundContainer, and ForegroundContainer to the game var backgroundContainer = game.addChild(new BackgroundContainer()); var midgroundContainer = game.addChild(new MidgroundContainer()); var foregroundContainer = game.addChild(new ForegroundContainer()); // Add UI_Holder to the midground container var uiHolder = midgroundContainer.attachAsset('UI_Holder', { anchorX: 0.5, anchorY: 0.5 }); uiHolder.x = 2048 / 2; uiHolder.y = 2732 / 2; // Initialize UI container and add it to the foreground container var uiContainer = foregroundContainer.addChild(new UI()); // Initialize Timer and add it to the UI container var timer = uiContainer.addChild(new Timer()); // Initialize ButtonDisplay var buttonDisplay = uiContainer.addChild(new ButtonDisplay()); buttonDisplay.y = 2732 / 2; // Initialize level variable var level = 1; // Initialize currentButtonId variable var currentButtonId = null; // Function to update buttons based on the current level function updateButtons() { // Clear existing buttons buttons.forEach(function (button) { return button.destroy(); }); buttons = []; // Add buttons based on the current level var availableButtons = []; for (var buttonId in buttonQuantities) { availableButtons.push(buttonId); } if (currentButtonId) { availableButtons = availableButtons.filter(function (buttonId) { return buttonId !== currentButtonId; }); } var randomButtonId = availableButtons[Math.floor(Math.random() * availableButtons.length)]; buttonDisplay.updateButton(randomButtonId); for (var buttonId in buttonQuantities) { var quantity = buttonQuantities[buttonId]; for (var j = 0; j < quantity; j++) { // Ensure only a single instance of the target button if (buttonId === randomButtonId && j > 0) { continue; } var button = new Button(buttonId); do { button.x = Math.random() * (2048 - button.width - 400) + button.width / 2 + 200; button.y = Math.random() * (2732 - button.height - 600) + button.height / 2 + 300; } while (Math.abs(button.x - 2048 / 2) < 250 && Math.abs(button.y - 2732 / 2) < 250); buttons.push(button); if (buttonId === randomButtonId) { foregroundContainer.addChild(button); } else { midgroundContainer.addChild(button); } } } currentButtonId = randomButtonId; // Reset the Timer timer.reset(); // Introduce a new button type and increase existing button quantities each round if (level <= 10) { var newButtonId = 'Button_' + (level + 1); buttonQuantities[newButtonId] = 1; } for (var buttonId in buttonQuantities) { if (buttonId !== currentButtonId) { buttonQuantities[buttonId]++; } else { buttonQuantities[buttonId] = 1; // Ensure target button has only one instance } } // Continue to increase the number of existing buttons infinitely for (var buttonId in buttonQuantities) { if (buttonId !== currentButtonId) { buttonQuantities[buttonId]++; } } } // Initialize level text var levelTxt = new Text2('1', { size: 200, fill: "#ffffff", stroke: "#000000", strokeThickness: 15 }); levelTxt.setText(level.toString()); levelTxt.anchor.set(0.5, 0.5); levelTxt.x = 2048 / 2; levelTxt.y = 200; uiContainer.addChild(levelTxt); // Initialize button quantities for the first round var buttonQuantities = { 'Button_1': 1, 'Button_2': 1 }; // Initialize buttons at the start of the game updateButtons(); // Update function for the game game.update = function () { // No specific update logic for the game }; // Mouse or touch down on the game object game.down = function (x, y, obj) { // No specific logic for game down event }; // Mouse or touch up on the game object game.up = function (x, y, obj) { // No specific logic for game up event }; // Mouse or touch move on the game object game.move = function (x, y, obj) { // No specific logic for game move event };
/****
* Classes
****/
var BackgroundContainer = Container.expand(function () {
var self = Container.call(this);
var backgroundImage = self.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundImage.x = 2048 / 2;
backgroundImage.y = 2732 / 2;
});
//<Assets used in the game will automatically appear here>
// Button class
var Button = Container.expand(function (buttonId) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset(buttonId, {
anchorX: 0.5,
anchorY: 0.5
});
// Add speed property to Button class
self.speed = {
x: Math.random() * 2 - 1,
y: Math.random() * 2 - 1
};
self.update = function () {
// Move the button
self.x += self.speed.x;
self.y += self.speed.y;
// Bounce the button off the screen edges
if (self.x - buttonGraphics.width / 2 < 0 || self.x + buttonGraphics.width / 2 > 2048) {
self.speed.x *= -1;
}
if (self.y - buttonGraphics.height / 2 < 0 || self.y + buttonGraphics.height / 2 > 2732) {
self.speed.y *= -1;
}
// Bounce the button off the center area
if (Math.abs(self.x - 2048 / 2) < 250 && Math.abs(self.y - 2732 / 2) < 250) {
self.speed.x *= -1;
self.speed.y *= -1;
}
};
self.down = function (x, y, obj) {
// When the button is clicked, increase the score and move the button to a new random position
if (buttonId === currentButtonId) {
level += 1;
levelTxt.setText(level);
self.x = Math.random() * (2048 - buttonGraphics.width) + buttonGraphics.width / 2;
self.y = Math.random() * (2732 - buttonGraphics.height - 300) + buttonGraphics.height / 2 + 300;
updateButtons();
// Play the 'Correct' sound
LK.getSound('Correct').play();
} else {
LK.showGameOver();
}
};
});
var ButtonDisplay = Container.expand(function () {
var self = Container.call(this);
var currentButtonId = null;
var buttonGraphics = null;
self.updateButton = function (buttonId) {
if (buttonGraphics) {
self.removeChild(buttonGraphics);
}
currentButtonId = buttonId;
buttonGraphics = self.attachAsset(buttonId, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3,
scaleY: 3
});
buttonGraphics.scale.set(3, 3);
buttonGraphics.x = 2048 / 2;
buttonGraphics.y = 0; // Position at the top center
// Start the animation of the button shrinking
self.timerValue = 200;
self.updateButtonSize = function (timerValue) {
var scale = timerValue / 200;
buttonGraphics.scale.set(scale, scale);
};
};
});
var ForegroundContainer = Container.expand(function () {
var self = Container.call(this);
});
var MidgroundContainer = Container.expand(function () {
var self = Container.call(this);
});
// Timer class
var Timer = Container.expand(function () {
var self = Container.call(this);
var timerValue = 200;
self.update = function () {
if (timerValue > 0) {
timerValue -= 1;
buttonDisplay.updateButtonSize(timerValue);
// Update the button display as the timer decreases
buttonDisplay.updateButtonSize(timerValue);
} else {
LK.showGameOver();
}
};
self.reset = function () {
timerValue = 200;
buttonDisplay.updateButtonSize(200); // Reset the button size to its original size
};
});
var UI = Container.expand(function () {
var self = Container.call(this);
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize buttons array
var buttons = [];
// Initialize and add BackgroundContainer, MidgroundContainer, and ForegroundContainer to the game
var backgroundContainer = game.addChild(new BackgroundContainer());
var midgroundContainer = game.addChild(new MidgroundContainer());
var foregroundContainer = game.addChild(new ForegroundContainer());
// Add UI_Holder to the midground container
var uiHolder = midgroundContainer.attachAsset('UI_Holder', {
anchorX: 0.5,
anchorY: 0.5
});
uiHolder.x = 2048 / 2;
uiHolder.y = 2732 / 2;
// Initialize UI container and add it to the foreground container
var uiContainer = foregroundContainer.addChild(new UI());
// Initialize Timer and add it to the UI container
var timer = uiContainer.addChild(new Timer());
// Initialize ButtonDisplay
var buttonDisplay = uiContainer.addChild(new ButtonDisplay());
buttonDisplay.y = 2732 / 2;
// Initialize level variable
var level = 1;
// Initialize currentButtonId variable
var currentButtonId = null;
// Function to update buttons based on the current level
function updateButtons() {
// Clear existing buttons
buttons.forEach(function (button) {
return button.destroy();
});
buttons = [];
// Add buttons based on the current level
var availableButtons = [];
for (var buttonId in buttonQuantities) {
availableButtons.push(buttonId);
}
if (currentButtonId) {
availableButtons = availableButtons.filter(function (buttonId) {
return buttonId !== currentButtonId;
});
}
var randomButtonId = availableButtons[Math.floor(Math.random() * availableButtons.length)];
buttonDisplay.updateButton(randomButtonId);
for (var buttonId in buttonQuantities) {
var quantity = buttonQuantities[buttonId];
for (var j = 0; j < quantity; j++) {
// Ensure only a single instance of the target button
if (buttonId === randomButtonId && j > 0) {
continue;
}
var button = new Button(buttonId);
do {
button.x = Math.random() * (2048 - button.width - 400) + button.width / 2 + 200;
button.y = Math.random() * (2732 - button.height - 600) + button.height / 2 + 300;
} while (Math.abs(button.x - 2048 / 2) < 250 && Math.abs(button.y - 2732 / 2) < 250);
buttons.push(button);
if (buttonId === randomButtonId) {
foregroundContainer.addChild(button);
} else {
midgroundContainer.addChild(button);
}
}
}
currentButtonId = randomButtonId;
// Reset the Timer
timer.reset();
// Introduce a new button type and increase existing button quantities each round
if (level <= 10) {
var newButtonId = 'Button_' + (level + 1);
buttonQuantities[newButtonId] = 1;
}
for (var buttonId in buttonQuantities) {
if (buttonId !== currentButtonId) {
buttonQuantities[buttonId]++;
} else {
buttonQuantities[buttonId] = 1; // Ensure target button has only one instance
}
}
// Continue to increase the number of existing buttons infinitely
for (var buttonId in buttonQuantities) {
if (buttonId !== currentButtonId) {
buttonQuantities[buttonId]++;
}
}
}
// Initialize level text
var levelTxt = new Text2('1', {
size: 200,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 15
});
levelTxt.setText(level.toString());
levelTxt.anchor.set(0.5, 0.5);
levelTxt.x = 2048 / 2;
levelTxt.y = 200;
uiContainer.addChild(levelTxt);
// Initialize button quantities for the first round
var buttonQuantities = {
'Button_1': 1,
'Button_2': 1
};
// Initialize buttons at the start of the game
updateButtons();
// Update function for the game
game.update = function () {
// No specific update logic for the game
};
// Mouse or touch down on the game object
game.down = function (x, y, obj) {
// No specific logic for game down event
};
// Mouse or touch up on the game object
game.up = function (x, y, obj) {
// No specific logic for game up event
};
// Mouse or touch move on the game object
game.move = function (x, y, obj) {
// No specific logic for game move event
};