/****
* Classes
****/
var Button = Container.expand(function () {
var self = Container.call(this);
this.text = new Text2('', {
size: 100,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
}
});
self.addChild(this.text);
this.setLabel = function (label) {
this.text.setText(label);
};
this.setColor = function (color) {
self.tint = color;
};
});
// Class for the trivia question
var TriviaQuestion = Container.expand(function () {
var self = Container.call(this);
this.text = new Text2('', {
size: 150,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
}
});
this.text.x = 2048 / 2;
this.text.y = 2732 / 2 - 300;
self.addChild(this.text);
this.text.anchor.set(0.5, 0.5);
this.text.x = 2048 / 2;
this.text.y = 2732 / 2 - 300;
this.questions = [{
question: 'The sky is blue.',
answer: true
}, {
question: 'Cows can fly.',
answer: false
}, {
question: 'The earth is flat.',
answer: false
}, {
question: 'JavaScript is a programming language.',
answer: true
}];
this.currentQuestionIndex = 0;
// Function to set the question text
this.setQuestion = function () {
this.text.setText(this.questions[this.currentQuestionIndex].question);
};
// Function to check if the answer is correct
this.checkAnswer = function (isTrue) {
return isTrue === this.questions[this.currentQuestionIndex].answer;
};
// Function to load the next question
this.nextQuestion = function () {
this.currentQuestionIndex++;
if (this.currentQuestionIndex >= this.questions.length) {
this.currentQuestionIndex = 0;
}
this.setQuestion();
};
});
// Class for the swipe gesture
var SwipeGesture = Container.expand(function () {
var self = Container.call(this);
this.startX = 0;
this.startY = 0;
this.endX = 0;
this.endY = 0;
// Function to start the swipe
this.startSwipe = function (event) {
var pos = event.getLocalPosition(game);
this.startX = pos.x;
this.startY = pos.y;
};
// Function to end the swipe and determine the direction
this.endSwipe = function (event) {
var pos = event.getLocalPosition(game);
this.endX = pos.x;
this.endY = pos.y;
return this.getSwipeDirection();
};
// Function to get the swipe direction
this.getSwipeDirection = function () {
var dx = this.endX - this.startX;
var dy = this.endY - this.startY;
if (Math.abs(dx) > Math.abs(dy)) {
return dx > 0 ? 'right' : 'left';
} else {
return dy > 0 ? 'down' : 'up';
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Add a hill background
var hillBackground = game.addChild(LK.getAsset('hillBackground', 'Hill Background'));
hillBackground.width = 2048;
hillBackground.height = 2732;
// Initialize the trivia question
var triviaQuestion = game.addChild(new TriviaQuestion());
triviaQuestion.setQuestion("True or False?");
// Initialize the swipe gesture
var swipeGesture = game.addChild(new SwipeGesture());
// Event listeners for swipe gestures
game.on('down', function (obj) {
swipeGesture.startSwipe(obj.event);
});
game.on('up', function (obj) {
var direction = swipeGesture.endSwipe(obj.event);
if (direction === 'right') {
// Handle swipe right for true
if (triviaQuestion.checkAnswer(true)) {
LK.effects.flashScreen(0x00ff00, 500);
triviaQuestion.nextQuestion();
} else {
// Handle wrong answer
LK.effects.flashScreen(0xff0000, 500);
}
} else if (direction === 'left') {
// Handle swipe left for false
if (triviaQuestion.checkAnswer(false)) {
LK.effects.flashScreen(0x00ff00, 500);
triviaQuestion.nextQuestion();
} else {
// Handle wrong answer
LK.effects.flashScreen(0xff0000, 500);
}
}
});
// Main game loop
LK.on('tick', function () {
// Game logic goes here
// For example, updating the question text, handling game over conditions, etc.
});