/**** * Classes ****/ // Explosion class to represent the explosion effect var Explosion = Container.expand(function () { var self = Container.call(this); var explosionGraphics = self.attachAsset('explosion', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { if (self.alpha > 0) { self.alpha -= 0.01; } else { self.destroy(); } }; }); var Light = Container.expand(function () { var self = Container.call(this); var lightGraphics = self.attachAsset('light', { anchorX: 0.5, anchorY: 0.5 }); self.state = 'red'; // Initial state self.setColor = function (color) { if (color === 'green') { lightGraphics.tint = 0x00FF00; // Green } else if (color === 'red') { lightGraphics.tint = 0xFF0000; // Red } else { lightGraphics.tint = 0x000000; // Off } self.state = color; }; }); // Meter class to represent the progress meter var Meter = Container.expand(function () { var self = Container.call(this); var meterGraphics = self.attachAsset('meter', { anchorX: 0.0, anchorY: 0.5 }); self.update = function () { meterGraphics.width = score / 20 * 2048; }; }); /**** * Initialize Game ****/ //<Assets used in the game will automatically appear here> var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var greenLightOnTime; var redLight = game.addChild(new Light()); redLight.x = 2048 / 2; redLight.y = 2732 / 2 - 300; redLight.setColor('red'); var yellowLight = game.addChild(new Light()); yellowLight.x = 2048 / 2; yellowLight.y = 2732 / 2; yellowLight.setColor('red'); var greenLight = game.addChild(new Light()); greenLight.x = 2048 / 2; greenLight.y = 2732 / 2 + 300; greenLight.setColor('red'); var score = 0; var explosion = game.addChild(new Explosion()); explosion.x = 2048 / 2; explosion.y = 2732 / 2; explosion.alpha = 0; var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var meter = game.addChild(new Meter()); meter.x = 0; meter.y = 2732 - meter.height; var canTap = false; var changeLightTimeout; // Function to change the light color function changeLight() { if (redLight.state === 'red') { redLight.setColor('green'); yellowLight.setColor('green'); greenLight.setColor('green'); canTap = true; greenLightOnTime = Date.now(); changeLightTimeout = LK.setTimeout(function () { redLight.setColor('red'); yellowLight.setColor('red'); greenLight.setColor('red'); canTap = false; changeLight(); }, Math.random() * 2000 + 1000); // Random time between 1-3 seconds } else { redLight.setColor('red'); yellowLight.setColor('red'); greenLight.setColor('red'); canTap = false; changeLightTimeout = LK.setTimeout(changeLight, Math.random() * 2000 + 1000); } } // Start the light change cycle changeLight(); // Handle tap event game.down = function (x, y, obj) { if (canTap && greenLight.state === 'green') { score++; var reactionTime = (Date.now() - greenLightOnTime) / 1000; scoreTxt.setText(('SCORE: ' + score + ', REACTION TIME: ' + reactionTime.toFixed(2) + 'S').toUpperCase()); greenLight.setColor('red'); canTap = false; LK.clearTimeout(changeLightTimeout); changeLight(); if (score == 10) { LK.playMusic('1'); explosion.alpha = 1; } if (score >= 10) { meter.update(); } } else if (greenLight.state === 'red') { // Reset score to zero for incorrect tap score = 0; scoreTxt.setText(('SCORE: ' + score).toUpperCase()); // Flash screen red for incorrect tap LK.effects.flashScreen(0xff0000, 500); // Stop the song when the light turns red LK.stopMusic(); } }; // Update function game.update = function () { // Game logic updates can be added here if needed };
/****
* Classes
****/
// Explosion class to represent the explosion effect
var Explosion = Container.expand(function () {
var self = Container.call(this);
var explosionGraphics = self.attachAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
if (self.alpha > 0) {
self.alpha -= 0.01;
} else {
self.destroy();
}
};
});
var Light = Container.expand(function () {
var self = Container.call(this);
var lightGraphics = self.attachAsset('light', {
anchorX: 0.5,
anchorY: 0.5
});
self.state = 'red'; // Initial state
self.setColor = function (color) {
if (color === 'green') {
lightGraphics.tint = 0x00FF00; // Green
} else if (color === 'red') {
lightGraphics.tint = 0xFF0000; // Red
} else {
lightGraphics.tint = 0x000000; // Off
}
self.state = color;
};
});
// Meter class to represent the progress meter
var Meter = Container.expand(function () {
var self = Container.call(this);
var meterGraphics = self.attachAsset('meter', {
anchorX: 0.0,
anchorY: 0.5
});
self.update = function () {
meterGraphics.width = score / 20 * 2048;
};
});
/****
* Initialize Game
****/
//<Assets used in the game will automatically appear here>
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var greenLightOnTime;
var redLight = game.addChild(new Light());
redLight.x = 2048 / 2;
redLight.y = 2732 / 2 - 300;
redLight.setColor('red');
var yellowLight = game.addChild(new Light());
yellowLight.x = 2048 / 2;
yellowLight.y = 2732 / 2;
yellowLight.setColor('red');
var greenLight = game.addChild(new Light());
greenLight.x = 2048 / 2;
greenLight.y = 2732 / 2 + 300;
greenLight.setColor('red');
var score = 0;
var explosion = game.addChild(new Explosion());
explosion.x = 2048 / 2;
explosion.y = 2732 / 2;
explosion.alpha = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var meter = game.addChild(new Meter());
meter.x = 0;
meter.y = 2732 - meter.height;
var canTap = false;
var changeLightTimeout;
// Function to change the light color
function changeLight() {
if (redLight.state === 'red') {
redLight.setColor('green');
yellowLight.setColor('green');
greenLight.setColor('green');
canTap = true;
greenLightOnTime = Date.now();
changeLightTimeout = LK.setTimeout(function () {
redLight.setColor('red');
yellowLight.setColor('red');
greenLight.setColor('red');
canTap = false;
changeLight();
}, Math.random() * 2000 + 1000); // Random time between 1-3 seconds
} else {
redLight.setColor('red');
yellowLight.setColor('red');
greenLight.setColor('red');
canTap = false;
changeLightTimeout = LK.setTimeout(changeLight, Math.random() * 2000 + 1000);
}
}
// Start the light change cycle
changeLight();
// Handle tap event
game.down = function (x, y, obj) {
if (canTap && greenLight.state === 'green') {
score++;
var reactionTime = (Date.now() - greenLightOnTime) / 1000;
scoreTxt.setText(('SCORE: ' + score + ', REACTION TIME: ' + reactionTime.toFixed(2) + 'S').toUpperCase());
greenLight.setColor('red');
canTap = false;
LK.clearTimeout(changeLightTimeout);
changeLight();
if (score == 10) {
LK.playMusic('1');
explosion.alpha = 1;
}
if (score >= 10) {
meter.update();
}
} else if (greenLight.state === 'red') {
// Reset score to zero for incorrect tap
score = 0;
scoreTxt.setText(('SCORE: ' + score).toUpperCase());
// Flash screen red for incorrect tap
LK.effects.flashScreen(0xff0000, 500);
// Stop the song when the light turns red
LK.stopMusic();
}
};
// Update function
game.update = function () {
// Game logic updates can be added here if needed
};