/****
* Initialize Game
****/
//<Assets used in the game will automatically appear here>
//<Write entity 'classes' with empty functions for important behavior here>
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
function advanceToLevel5() {
// Change title text
gameTitle.setText('The 60-min Game');
// Reset timer
elapsedTime = 0;
timerTxt.setText('0.0');
// Update checkTime function for level 5
checkTime = function checkTime() {
var finalTime = elapsedTime / 1000;
if (Math.abs(finalTime - 3600) < 0.1) {
LK.effects.flashScreen(0x00ff00, 1000); // Flash green for success
startButton.visible = false;
stopButton.visible = false;
} else {
LK.effects.flashScreen(0xff0000, 1000); // Flash red for failure
}
};
}
function advanceToLevel4() {
// Change title text
gameTitle.setText('The 5-min Game');
// Reset timer
elapsedTime = 0;
timerTxt.setText('0.0');
// Update checkTime function for level 4
checkTime = function checkTime() {
var finalTime = elapsedTime / 1000;
if (Math.abs(finalTime - 300) < 0.1) {
LK.effects.flashScreen(0x00ff00, 1000); // Flash green for success
advanceToLevel5();
} else {
LK.effects.flashScreen(0xff0000, 1000); // Flash red for failure
}
};
}
function advanceToLevel3() {
// Change title text
gameTitle.setText('The 60-Second Game');
// Reset timer
elapsedTime = 0;
timerTxt.setText('0.0');
// Update checkTime function for level 3
checkTime = function checkTime() {
var finalTime = elapsedTime / 1000;
if (Math.abs(finalTime - 60) < 0.1) {
LK.effects.flashScreen(0x00ff00, 1000); // Flash green for success
advanceToLevel4();
} else {
LK.effects.flashScreen(0xff0000, 1000); // Flash red for failure
}
};
}
function advanceToLevel2() {
// Change title text
gameTitle.setText('The 20-Second Game');
// Reset timer
elapsedTime = 0;
timerTxt.setText('0.0');
// Update checkTime function for level 2
checkTime = function checkTime() {
var finalTime = elapsedTime / 1000;
if (Math.abs(finalTime - 20) < 0.1) {
LK.effects.flashScreen(0x00ff00, 1000); // Flash green for success
advanceToLevel3();
} else {
LK.effects.flashScreen(0xff0000, 1000); // Flash red for failure
}
};
}
//<Write game logic code here, including initializing arrays and variables>
// Initialize the game title
var gameTitle = new Text2('The 10-Second Game', {
size: 150,
fill: "#ffffff"
});
gameTitle.anchor.set(0.5, 0.5);
gameTitle.y = 400;
LK.gui.top.addChild(gameTitle);
// Initialize the timer display
var timerTxt = new Text2('0.0', {
size: 150,
fill: "#ffffff"
});
timerTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(timerTxt);
// Initialize the start button
var startButton = new Text2('Start', {
size: 100,
fill: "#00ff00"
});
startButton.anchor.set(0.5, 0.5);
startButton.x = timerTxt.x;
startButton.y = timerTxt.y + 300;
LK.gui.center.addChild(startButton);
// Initialize the stop button
var stopButton = new Text2('Stop', {
size: 100,
fill: "#ff0000"
});
stopButton.anchor.set(0.5, 0.5);
stopButton.x = timerTxt.x;
stopButton.y = timerTxt.y + 300;
LK.gui.center.addChild(stopButton);
stopButton.visible = false;
// Variables to keep track of time
var startTime = 0;
var elapsedTime = 0;
var timerRunning = false;
// Function to start the timer
function startTimer() {
startTime = Date.now();
timerRunning = true;
startButton.visible = false;
stopButton.visible = true;
}
// Function to stop the timer
function stopTimer() {
timerRunning = false;
stopButton.visible = false;
startButton.visible = true;
checkTime();
}
// Function to check the elapsed time
function checkTime() {
var finalTime = elapsedTime / 1000;
if (Math.abs(finalTime - 10) < 0.1) {
LK.effects.flashScreen(0x00ff00, 1000); // Flash green for success
advanceToLevel2();
} else {
LK.effects.flashScreen(0xff0000, 1000); // Flash red for failure
}
}
// Event listener for the start button
startButton.down = function (x, y, obj) {
startTimer();
};
// Event listener for the stop button
stopButton.down = function (x, y, obj) {
stopTimer();
};
// Update function to keep track of the timer
game.update = function () {
if (timerRunning) {
elapsedTime = Date.now() - startTime;
timerTxt.setText((elapsedTime / 1000).toFixed(1));
}
};