/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Castle = Container.expand(function () {
var self = Container.call(this);
var castleGraphics = self.attachAsset('castle', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Dice = Container.expand(function () {
var self = Container.call(this);
var diceGraphics = self.attachAsset('dice', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 1;
self.isRolling = false;
var diceText = new Text2('1', {
size: 60,
fill: 0x000000
});
diceText.anchor.set(0.5, 0.5);
self.addChild(diceText);
self.roll = function () {
if (self.isRolling) return;
self.isRolling = true;
LK.getSound('diceRoll').play();
var rollCount = 0;
var rollInterval = LK.setInterval(function () {
self.value = Math.floor(Math.random() * 6) + 1;
diceText.setText(self.value.toString());
rollCount++;
if (rollCount >= 10) {
LK.clearInterval(rollInterval);
self.isRolling = false;
showQuestion();
}
}, 100);
};
self.down = function (x, y, obj) {
if (!self.isRolling && !questionActive && !gameOver && canRollDice) {
self.roll();
}
};
return self;
});
var Knight = Container.expand(function () {
var self = Container.call(this);
var knightGraphics = self.attachAsset('knight', {
anchorX: 0.5,
anchorY: 0.5
});
self.currentTile = 0;
self.moveToTile = function (tileIndex) {
if (tileIndex < pathTiles.length) {
self.currentTile = tileIndex;
var targetTile = pathTiles[tileIndex];
tween(self, {
x: targetTile.x,
y: targetTile.y
}, {
duration: 1000,
easing: tween.easeInOut,
onFinish: function onFinish() {
if (tileIndex === pathTiles.length - 1) {
showVictory();
}
}
});
}
};
return self;
});
var Tile = Container.expand(function () {
var self = Container.call(this);
var tileGraphics = self.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Village = Container.expand(function () {
var self = Container.call(this);
var villageGraphics = self.attachAsset('village', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game state variables
var tokens = 5;
var questionActive = false;
var gameOver = false;
var currentQuestion = null;
var currentAnswers = [];
var canRollDice = true;
var pathTiles = [];
var knight = null;
var dice = null;
var tokenText = null;
var questionContainer = null;
var backgroundImage = null;
var usedQuestions = [];
// Initialize background
function initializeBackground() {
try {
backgroundImage = new Container();
game.addChild(backgroundImage);
backgroundImage.attachAsset('Mapa', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundImage.x = 1024;
backgroundImage.y = 1366;
backgroundImage.scaleX = 10;
backgroundImage.scaleY = 10;
game.setChildIndex(backgroundImage, 0);
console.log("Background loaded successfully!");
} catch (error) {
console.log("Could not load background image:", error.message);
}
}
// Updated initializePath with visual alignment
function initializePath() {
var village = game.addChild(new Village());
village.x = 300;
village.y = 2150;
pathTiles.push(village);
var pathPositions = [{
x: 450,
y: 2000
}, {
x: 600,
y: 1850
}, {
x: 700,
y: 1700
}, {
x: 850,
y: 1550
}, {
x: 1000,
y: 1400
}, {
x: 1150,
y: 1250
}, {
x: 1300,
y: 1100
}, {
x: 1450,
y: 950
}, {
x: 1600,
y: 800
}];
for (var i = 0; i < pathPositions.length; i++) {
var tile = game.addChild(new Tile());
tile.x = pathPositions[i].x;
tile.y = pathPositions[i].y;
pathTiles.push(tile);
}
var castle = game.addChild(new Castle());
castle.x = 1750;
castle.y = 650;
pathTiles.push(castle);
}
function initializeUI() {
tokenText = new Text2('Tokens: 5', {
size: 80,
fill: 0xFFD700
});
tokenText.anchor.set(0.5, 0);
LK.gui.top.addChild(tokenText);
dice = game.addChild(new Dice());
dice.x = 1700;
dice.y = 2400;
var instructionText = new Text2('¡Toca el dado para lanzar y moverte!', {
size: 60,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(instructionText);
}
function initializeKnight() {
knight = game.addChild(new Knight());
knight.x = pathTiles[0].x;
knight.y = pathTiles[0].y;
}
function moveKnight() {
var newPosition = knight.currentTile + dice.value;
if (newPosition >= pathTiles.length - 1) {
newPosition = pathTiles.length - 1;
}
knight.moveToTile(newPosition);
}
// Questions database
var questions = [{
question: "¿Cuánto es 2 + 2?",
answers: ["3", "4", "5", "6"],
correct: 1
}, {
question: "¿Cuál es la capital de Francia?",
answers: ["Londres", "París", "Berlín", "Madrid"],
correct: 1
}, {
question: "¿Cuántos lados tiene un triángulo?",
answers: ["2", "3", "4", "5"],
correct: 1
}, {
question: "¿Cuánto es 5 x 3?",
answers: ["12", "15", "18", "20"],
correct: 1
}, {
question: "¿Cuál es el planeta más cercano al Sol?",
answers: ["Venus", "Mercurio", "Tierra", "Marte"],
correct: 1
}];
function showQuestion() {
if (questionActive) return;
questionActive = true;
canRollDice = false;
// Reset used questions if all have been used
if (usedQuestions.length >= questions.length) {
usedQuestions = [];
}
// Select random question that hasn't been used
var availableQuestions = [];
for (var i = 0; i < questions.length; i++) {
if (usedQuestions.indexOf(i) === -1) {
availableQuestions.push(i);
}
}
var randomIndex = Math.floor(Math.random() * availableQuestions.length);
var questionIndex = availableQuestions[randomIndex];
currentQuestion = questions[questionIndex];
usedQuestions.push(questionIndex);
// Create question container
questionContainer = game.addChild(new Container());
questionContainer.x = 1024;
questionContainer.y = 1366;
// Question background
var questionBg = questionContainer.attachAsset('questionBox', {
anchorX: 0.5,
anchorY: 0.5
});
// Question text
var questionText = new Text2(currentQuestion.question, {
size: 60,
fill: 0x000000
});
questionText.anchor.set(0.5, 0.5);
questionText.x = 0;
questionText.y = -150;
questionContainer.addChild(questionText);
// Answer buttons
currentAnswers = [];
for (var i = 0; i < currentQuestion.answers.length; i++) {
var answerButton = questionContainer.addChild(new Container());
answerButton.x = i % 2 === 0 ? -300 : 300;
answerButton.y = i < 2 ? -50 : 100;
answerButton.answerIndex = i;
var answerBg = answerButton.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
var answerText = new Text2(currentQuestion.answers[i], {
size: 40,
fill: 0xFFFFFF
});
answerText.anchor.set(0.5, 0.5);
answerButton.addChild(answerText);
answerButton.down = function (x, y, obj) {
selectAnswer(this.answerIndex);
};
currentAnswers.push(answerButton);
}
}
function selectAnswer(answerIndex) {
if (!questionActive) return;
console.log("Selected answer index:", answerIndex);
console.log("Correct answer index:", currentQuestion.correct);
if (Number(answerIndex) === Number(currentQuestion.correct)) {
console.log("Correct answer!");
showCorrectPopup();
tokens += 5;
updateTokenDisplay();
moveKnight();
} else {
console.log("Wrong answer!");
tokens -= 5;
updateTokenDisplay();
if (tokens <= 0) {
showGameOver();
return;
}
canRollDice = true;
hideQuestion();
}
}
function hideQuestion() {
if (questionContainer) {
questionContainer.destroy();
questionContainer = null;
}
questionActive = false;
currentQuestion = null;
currentAnswers = [];
}
function updateTokenDisplay() {
if (tokenText) {
tokenText.setText('Tokens: ' + tokens);
}
}
function showVictory() {
gameOver = true;
canRollDice = false;
LK.showYouWin();
}
function showCorrectPopup() {
// Create popup container
var correctPopup = game.addChild(new Container());
correctPopup.x = 1024;
correctPopup.y = 1366;
// Background for popup
var popupBg = correctPopup.attachAsset('questionBox', {
anchorX: 0.5,
anchorY: 0.5
});
popupBg.scaleX = 0.8;
popupBg.scaleY = 0.4;
// CORRECTO! text
var correctText = new Text2('¡CORRECTO!', {
size: 120,
fill: 0x00FF00
});
correctText.anchor.set(0.5, 0.5);
correctPopup.addChild(correctText);
// Auto-hide popup after 2 seconds
LK.setTimeout(function () {
correctPopup.destroy();
canRollDice = true;
hideQuestion();
}, 2000);
}
function showGameOver() {
gameOver = true;
canRollDice = false;
LK.showGameOver();
}
// Initialize game
initializeBackground();
initializePath();
initializeUI();
initializeKnight();
LK.playMusic('medievalMusic');
game.update = function () {}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Castle = Container.expand(function () {
var self = Container.call(this);
var castleGraphics = self.attachAsset('castle', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Dice = Container.expand(function () {
var self = Container.call(this);
var diceGraphics = self.attachAsset('dice', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 1;
self.isRolling = false;
var diceText = new Text2('1', {
size: 60,
fill: 0x000000
});
diceText.anchor.set(0.5, 0.5);
self.addChild(diceText);
self.roll = function () {
if (self.isRolling) return;
self.isRolling = true;
LK.getSound('diceRoll').play();
var rollCount = 0;
var rollInterval = LK.setInterval(function () {
self.value = Math.floor(Math.random() * 6) + 1;
diceText.setText(self.value.toString());
rollCount++;
if (rollCount >= 10) {
LK.clearInterval(rollInterval);
self.isRolling = false;
showQuestion();
}
}, 100);
};
self.down = function (x, y, obj) {
if (!self.isRolling && !questionActive && !gameOver && canRollDice) {
self.roll();
}
};
return self;
});
var Knight = Container.expand(function () {
var self = Container.call(this);
var knightGraphics = self.attachAsset('knight', {
anchorX: 0.5,
anchorY: 0.5
});
self.currentTile = 0;
self.moveToTile = function (tileIndex) {
if (tileIndex < pathTiles.length) {
self.currentTile = tileIndex;
var targetTile = pathTiles[tileIndex];
tween(self, {
x: targetTile.x,
y: targetTile.y
}, {
duration: 1000,
easing: tween.easeInOut,
onFinish: function onFinish() {
if (tileIndex === pathTiles.length - 1) {
showVictory();
}
}
});
}
};
return self;
});
var Tile = Container.expand(function () {
var self = Container.call(this);
var tileGraphics = self.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Village = Container.expand(function () {
var self = Container.call(this);
var villageGraphics = self.attachAsset('village', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game state variables
var tokens = 5;
var questionActive = false;
var gameOver = false;
var currentQuestion = null;
var currentAnswers = [];
var canRollDice = true;
var pathTiles = [];
var knight = null;
var dice = null;
var tokenText = null;
var questionContainer = null;
var backgroundImage = null;
var usedQuestions = [];
// Initialize background
function initializeBackground() {
try {
backgroundImage = new Container();
game.addChild(backgroundImage);
backgroundImage.attachAsset('Mapa', {
anchorX: 0.5,
anchorY: 0.5
});
backgroundImage.x = 1024;
backgroundImage.y = 1366;
backgroundImage.scaleX = 10;
backgroundImage.scaleY = 10;
game.setChildIndex(backgroundImage, 0);
console.log("Background loaded successfully!");
} catch (error) {
console.log("Could not load background image:", error.message);
}
}
// Updated initializePath with visual alignment
function initializePath() {
var village = game.addChild(new Village());
village.x = 300;
village.y = 2150;
pathTiles.push(village);
var pathPositions = [{
x: 450,
y: 2000
}, {
x: 600,
y: 1850
}, {
x: 700,
y: 1700
}, {
x: 850,
y: 1550
}, {
x: 1000,
y: 1400
}, {
x: 1150,
y: 1250
}, {
x: 1300,
y: 1100
}, {
x: 1450,
y: 950
}, {
x: 1600,
y: 800
}];
for (var i = 0; i < pathPositions.length; i++) {
var tile = game.addChild(new Tile());
tile.x = pathPositions[i].x;
tile.y = pathPositions[i].y;
pathTiles.push(tile);
}
var castle = game.addChild(new Castle());
castle.x = 1750;
castle.y = 650;
pathTiles.push(castle);
}
function initializeUI() {
tokenText = new Text2('Tokens: 5', {
size: 80,
fill: 0xFFD700
});
tokenText.anchor.set(0.5, 0);
LK.gui.top.addChild(tokenText);
dice = game.addChild(new Dice());
dice.x = 1700;
dice.y = 2400;
var instructionText = new Text2('¡Toca el dado para lanzar y moverte!', {
size: 60,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(instructionText);
}
function initializeKnight() {
knight = game.addChild(new Knight());
knight.x = pathTiles[0].x;
knight.y = pathTiles[0].y;
}
function moveKnight() {
var newPosition = knight.currentTile + dice.value;
if (newPosition >= pathTiles.length - 1) {
newPosition = pathTiles.length - 1;
}
knight.moveToTile(newPosition);
}
// Questions database
var questions = [{
question: "¿Cuánto es 2 + 2?",
answers: ["3", "4", "5", "6"],
correct: 1
}, {
question: "¿Cuál es la capital de Francia?",
answers: ["Londres", "París", "Berlín", "Madrid"],
correct: 1
}, {
question: "¿Cuántos lados tiene un triángulo?",
answers: ["2", "3", "4", "5"],
correct: 1
}, {
question: "¿Cuánto es 5 x 3?",
answers: ["12", "15", "18", "20"],
correct: 1
}, {
question: "¿Cuál es el planeta más cercano al Sol?",
answers: ["Venus", "Mercurio", "Tierra", "Marte"],
correct: 1
}];
function showQuestion() {
if (questionActive) return;
questionActive = true;
canRollDice = false;
// Reset used questions if all have been used
if (usedQuestions.length >= questions.length) {
usedQuestions = [];
}
// Select random question that hasn't been used
var availableQuestions = [];
for (var i = 0; i < questions.length; i++) {
if (usedQuestions.indexOf(i) === -1) {
availableQuestions.push(i);
}
}
var randomIndex = Math.floor(Math.random() * availableQuestions.length);
var questionIndex = availableQuestions[randomIndex];
currentQuestion = questions[questionIndex];
usedQuestions.push(questionIndex);
// Create question container
questionContainer = game.addChild(new Container());
questionContainer.x = 1024;
questionContainer.y = 1366;
// Question background
var questionBg = questionContainer.attachAsset('questionBox', {
anchorX: 0.5,
anchorY: 0.5
});
// Question text
var questionText = new Text2(currentQuestion.question, {
size: 60,
fill: 0x000000
});
questionText.anchor.set(0.5, 0.5);
questionText.x = 0;
questionText.y = -150;
questionContainer.addChild(questionText);
// Answer buttons
currentAnswers = [];
for (var i = 0; i < currentQuestion.answers.length; i++) {
var answerButton = questionContainer.addChild(new Container());
answerButton.x = i % 2 === 0 ? -300 : 300;
answerButton.y = i < 2 ? -50 : 100;
answerButton.answerIndex = i;
var answerBg = answerButton.attachAsset('tile', {
anchorX: 0.5,
anchorY: 0.5
});
var answerText = new Text2(currentQuestion.answers[i], {
size: 40,
fill: 0xFFFFFF
});
answerText.anchor.set(0.5, 0.5);
answerButton.addChild(answerText);
answerButton.down = function (x, y, obj) {
selectAnswer(this.answerIndex);
};
currentAnswers.push(answerButton);
}
}
function selectAnswer(answerIndex) {
if (!questionActive) return;
console.log("Selected answer index:", answerIndex);
console.log("Correct answer index:", currentQuestion.correct);
if (Number(answerIndex) === Number(currentQuestion.correct)) {
console.log("Correct answer!");
showCorrectPopup();
tokens += 5;
updateTokenDisplay();
moveKnight();
} else {
console.log("Wrong answer!");
tokens -= 5;
updateTokenDisplay();
if (tokens <= 0) {
showGameOver();
return;
}
canRollDice = true;
hideQuestion();
}
}
function hideQuestion() {
if (questionContainer) {
questionContainer.destroy();
questionContainer = null;
}
questionActive = false;
currentQuestion = null;
currentAnswers = [];
}
function updateTokenDisplay() {
if (tokenText) {
tokenText.setText('Tokens: ' + tokens);
}
}
function showVictory() {
gameOver = true;
canRollDice = false;
LK.showYouWin();
}
function showCorrectPopup() {
// Create popup container
var correctPopup = game.addChild(new Container());
correctPopup.x = 1024;
correctPopup.y = 1366;
// Background for popup
var popupBg = correctPopup.attachAsset('questionBox', {
anchorX: 0.5,
anchorY: 0.5
});
popupBg.scaleX = 0.8;
popupBg.scaleY = 0.4;
// CORRECTO! text
var correctText = new Text2('¡CORRECTO!', {
size: 120,
fill: 0x00FF00
});
correctText.anchor.set(0.5, 0.5);
correctPopup.addChild(correctText);
// Auto-hide popup after 2 seconds
LK.setTimeout(function () {
correctPopup.destroy();
canRollDice = true;
hideQuestion();
}, 2000);
}
function showGameOver() {
gameOver = true;
canRollDice = false;
LK.showGameOver();
}
// Initialize game
initializeBackground();
initializePath();
initializeUI();
initializeKnight();
LK.playMusic('medievalMusic');
game.update = function () {};