/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Shape class to represent the shapes in the game
var Shape = Container.expand(function () {
var self = Container.call(this);
self.shapeType = null;
self.shapeGraphics = null;
self.setShape = function (type) {
self.shapeType = type;
if (self.shapeGraphics) {
self.removeChild(self.shapeGraphics);
}
self.shapeGraphics = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5
});
};
});
// Text class to represent the shape names in the game
var ShapeText = Container.expand(function () {
var self = Container.call(this);
self.text = new Text2('', {
size: 100,
fill: "#ffffff"
});
self.text.anchor.set(0.5, 0.5);
self.addChild(self.text);
self.setText = function (text) {
self.text.setText(text.toUpperCase());
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Function to handle game over
function gameOver() {
LK.clearTimeout(timer);
LK.showGameOver();
}
// Initialize variables
var shapes = ['circle', 'square', 'triangle', 'hexagon', 'octagon', 'oval', 'pentagon', 'rectangle', 'rhombus'];
var currentShape = null;
var currentShapeText = null;
var score = 0;
var timer = null;
var timeLeft = 3;
var lives = 3;
var livesDisplay = [];
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var timerTxt = new Text2('Time: ' + timeLeft, {
size: 100,
fill: "#ffffff"
});
timerTxt.anchor.set(0.5, 0);
timerTxt.x = 2048 / 2;
timerTxt.y = 2732 * 3 / 4 - 200;
game.addChild(timerTxt);
// Create shape and text objects
var shape = new Shape();
shape.x = 2048 / 2;
shape.y = 2732 / 3;
game.addChild(shape);
var iamText = new Text2('I AM', {
size: 100,
fill: "#ffffff"
});
iamText.anchor.set(0.5, 0.5);
iamText.x = 2048 / 2;
iamText.y = 2732 / 3 - 400;
game.addChild(iamText);
var shapeText = new ShapeText();
shapeText.x = 2048 / 2;
shapeText.y = 2732 / 2;
game.addChild(shapeText);
// Function to update the shape and text
function updateShapeAndText() {
var randomShape = shapes[Math.floor(Math.random() * shapes.length)];
var randomText = shapes[Math.floor(Math.random() * shapes.length)];
shape.setShape(randomShape);
shapeText.setText(randomText);
currentShape = randomShape;
currentShapeText = randomText;
if (timer) {
LK.clearTimeout(timer);
}
timer = LK.setTimeout(gameOver, 3000);
timerTxt.setText('Time: 3');
}
// Function to handle True button press
function handleTruePress() {
if (currentShape === currentShapeText) {
score++;
scoreTxt.setText('Score: ' + score);
LK.clearTimeout(timer);
updateShapeAndText();
} else {
lives--;
LK.effects.flashScreen(0xff0000, 1000);
if (lives > 0) {
var heart = livesDisplay.pop();
LK.gui.topLeft.removeChild(heart);
} else {
gameOver();
}
}
}
// Function to handle False button press
function handleFalsePress() {
if (currentShape !== currentShapeText) {
score++;
scoreTxt.setText('Score: ' + score);
LK.clearTimeout(timer);
updateShapeAndText();
} else {
lives--;
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('wrongAnswer').play();
if (livesDisplay.length > 0) {
var heart = livesDisplay.pop();
LK.gui.topLeft.removeChild(heart);
}
LK.getSound('wrongAnswer').play();
if (lives <= 0) {
gameOver();
}
}
}
// Create heart image assets for the lives display
for (var i = 0; i < lives; i++) {
var heart = LK.getAsset('heart', {
anchorX: 0.0,
anchorY: 0.0,
scaleX: 0.5,
scaleY: 0.5,
x: 50 + i * 100,
y: 50
});
livesDisplay.push(heart);
LK.gui.topLeft.addChild(heart);
}
// Create True and False buttons
var trueButtonBackground = LK.getAsset('trueButtonBackground', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
game.addChild(trueButtonBackground);
trueButtonBackground.x = 2048 / 4;
trueButtonBackground.y = 2732 * 3 / 4;
var trueButton = new Text2('True', {
size: 200,
fill: "#ffffff"
});
trueButtonBackground.addChild(trueButton);
trueButton.anchor.set(0.5, 0.5);
trueButton.x = 2048 / 4;
trueButton.y = 2732 * 3 / 4;
game.addChild(trueButton);
var falseButtonBackground = LK.getAsset('falseButtonBackground', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
game.addChild(falseButtonBackground);
falseButtonBackground.x = 2048 * 3 / 4;
falseButtonBackground.y = 2732 * 3 / 4;
var falseButton = new Text2('False', {
size: 200,
fill: "#ffffff"
});
falseButtonBackground.addChild(falseButton);
falseButton.anchor.set(0.5, 0.5);
falseButton.x = 2048 * 3 / 4;
falseButton.y = 2732 * 3 / 4;
game.addChild(falseButton);
// Add event listeners for buttons
trueButton.down = function (x, y, obj) {
handleTruePress();
timeLeft = 3;
timerTxt.setText('Time: ' + timeLeft);
LK.clearTimeout(timer);
timer = LK.setTimeout(gameOver, 3000);
LK.getSound('buttonClick').play();
};
falseButton.down = function (x, y, obj) {
handleFalsePress();
timeLeft = 3;
timerTxt.setText('Time: ' + timeLeft);
LK.clearTimeout(timer);
timer = LK.setTimeout(gameOver, 3000);
LK.getSound('buttonClick').play();
};
// Initialize the game with the first shape and text
updateShapeAndText();
// Update the timer text every second
var timerInterval = LK.setInterval(function () {
console.log("Time Left: ", timeLeft);
if (timeLeft > 0) {
timeLeft--;
timerTxt.setText('Time: ' + timeLeft);
} else {
lives--;
if (lives > 0) {
var heart = livesDisplay.pop();
LK.gui.topLeft.removeChild(heart);
}
if (lives <= 0) {
// Do nothing
}
}
}, 1000);
// Clear the timer interval when game over