Code edit (1 edits merged)
Please save this source code
User prompt
Caja de Fortuna
Initial prompt
idea de juego, quiero hacerlo tipo 50 x 15 pero con cajas a elegir, elijes 1 de entre 20 donde solo 1 ahi premio gorod y las demas son todas preguntas con premios pequeños, si aciertan eligen si quieren cambiar de caja o seguir jugando abriendo cajas nuevas acumulando premiom si fallas alguna pregunta pierdes 1 de los 1 de los 2 comodines que tienes una vez pierdas los 2 comodines con las preguntas se acabara el juego y perderas todo lo recaudado te vas sin la caja elegida y sin ningun premio acumulado.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Box = Container.expand(function (index, isGrandPrize) {
var self = Container.call(this);
self.index = index;
self.isGrandPrize = isGrandPrize;
self.isOpened = false;
self.isSelected = false;
var boxGraphics = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
var numberText = new Text2((index + 1).toString(), {
size: 60,
fill: 0xFFFFFF
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
self.select = function () {
self.isSelected = true;
self.removeChild(boxGraphics);
boxGraphics = self.attachAsset('selectedBox', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(boxGraphics, 0);
};
self.open = function () {
self.isOpened = true;
self.removeChild(boxGraphics);
if (self.isGrandPrize) {
boxGraphics = self.attachAsset('grandPrizeBox', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
boxGraphics = self.attachAsset('openedBox', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.addChildAt(boxGraphics, 0);
};
self.down = function (x, y, obj) {
if (!self.isOpened) {
if (gameState === 'selecting') {
selectBox(self);
} else if (gameState === 'switching' && !self.isSelected) {
switchToBox(self);
}
}
};
return self;
});
var Button = Container.expand(function (text, callback) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.callback = callback;
self.down = function (x, y, obj) {
if (self.callback) {
self.callback();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var boxes = [];
var selectedBox = null;
var gameState = 'selecting'; // 'selecting', 'question', 'switching', 'gameOver', 'won'
var lifelines = 2;
var accumulatedPrize = 0;
var grandPrizeIndex = Math.floor(Math.random() * 20);
// Trivia questions
var triviaQuestions = [{
question: "What is 2 + 2?",
answer: "4",
options: ["3", "4", "5", "6"],
reward: 100
}, {
question: "Capital of France?",
answer: "Paris",
options: ["London", "Berlin", "Paris", "Rome"],
reward: 150
}, {
question: "Largest ocean?",
answer: "Pacific",
options: ["Atlantic", "Pacific", "Indian", "Arctic"],
reward: 200
}, {
question: "Who painted Mona Lisa?",
answer: "Leonardo",
options: ["Picasso", "Leonardo", "Van Gogh", "Monet"],
reward: 180
}, {
question: "How many days in a year?",
answer: "365",
options: ["364", "365", "366", "367"],
reward: 120
}];
var currentQuestion = null;
// UI Elements
var titleText = new Text2('Caja de Fortuna', {
size: 80,
fill: 0xFFD700
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 50;
var instructionText = new Text2('Select a box to begin!', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0.5);
game.addChild(instructionText);
instructionText.x = 1024;
instructionText.y = 400;
var prizeText = new Text2('Prize: $0', {
size: 60,
fill: 0x00FF00
});
prizeText.anchor.set(0.5, 0);
LK.gui.top.addChild(prizeText);
prizeText.y = 150;
var lifelineContainer = new Container();
LK.gui.topRight.addChild(lifelineContainer);
lifelineContainer.x = -150;
lifelineContainer.y = 50;
// Create lifeline icons
var lifelineIcons = [];
for (var i = 0; i < 2; i++) {
var icon = LK.getAsset('lifelineIcon', {
anchorX: 0.5,
anchorY: 0.5
});
icon.x = i * 80;
lifelineContainer.addChild(icon);
lifelineIcons.push(icon);
}
// Create boxes in a 4x5 grid
for (var i = 0; i < 20; i++) {
var row = Math.floor(i / 4);
var col = i % 4;
var box = new Box(i, i === grandPrizeIndex);
box.x = 300 + col * 250;
box.y = 600 + row * 250;
boxes.push(box);
game.addChild(box);
}
// Question UI elements (initially hidden)
var questionContainer = new Container();
game.addChild(questionContainer);
questionContainer.visible = false;
var questionBg = LK.getAsset('button', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4,
scaleY: 8
});
questionBg.tint = 0x2c3e50;
questionContainer.addChild(questionBg);
var questionText = new Text2('', {
size: 50,
fill: 0xFFFFFF
});
questionText.anchor.set(0.5, 0.5);
questionContainer.addChild(questionText);
questionText.y = -200;
var optionButtons = [];
for (var i = 0; i < 4; i++) {
var button = new Button('', function () {
var selectedAnswer = this.children[1].text;
checkAnswer(selectedAnswer);
});
button.y = -50 + i * 100;
questionContainer.addChild(button);
optionButtons.push(button);
}
questionContainer.x = 1024;
questionContainer.y = 1366;
// Action buttons
var continueButton = new Button('Continue Playing', function () {
gameState = 'question';
showQuestion();
});
var switchButton = new Button('Switch Box', function () {
gameState = 'switching';
instructionText.setText('Select a different box');
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
});
var cashOutButton = new Button('Cash Out', function () {
endGame(false, true);
});
game.addChild(continueButton);
game.addChild(switchButton);
game.addChild(cashOutButton);
continueButton.x = 1024;
continueButton.y = 2200;
continueButton.visible = false;
switchButton.x = 1024;
switchButton.y = 2300;
switchButton.visible = false;
cashOutButton.x = 1024;
cashOutButton.y = 2400;
cashOutButton.visible = false;
function selectBox(box) {
if (selectedBox) {
// Deselect previous box
selectedBox.isSelected = false;
var prevGraphics = selectedBox.children[0];
selectedBox.removeChild(prevGraphics);
var newGraphics = selectedBox.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
selectedBox.addChildAt(newGraphics, 0);
}
selectedBox = box;
selectedBox.select();
LK.getSound('select').play();
if (selectedBox.isGrandPrize) {
// Player found grand prize
selectedBox.open();
endGame(true, false);
return;
}
gameState = 'question';
showQuestion();
}
function switchToBox(newBox) {
selectedBox.isSelected = false;
var prevGraphics = selectedBox.children[0];
selectedBox.removeChild(prevGraphics);
var newGraphics = selectedBox.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
selectedBox.addChildAt(newGraphics, 0);
selectedBox = newBox;
selectedBox.select();
LK.getSound('select').play();
if (selectedBox.isGrandPrize) {
selectedBox.open();
endGame(true, false);
return;
}
gameState = 'question';
showQuestion();
}
function showQuestion() {
currentQuestion = triviaQuestions[Math.floor(Math.random() * triviaQuestions.length)];
questionText.setText(currentQuestion.question);
for (var i = 0; i < 4; i++) {
var optionText = optionButtons[i].children[1];
optionText.setText(currentQuestion.options[i]);
}
questionContainer.visible = true;
instructionText.setText('Answer the question!');
// Hide action buttons
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
}
function checkAnswer(selectedAnswer) {
if (selectedAnswer === currentQuestion.answer) {
// Correct answer
accumulatedPrize += currentQuestion.reward;
prizeText.setText('Prize: $' + accumulatedPrize);
LK.getSound('correct').play();
questionContainer.visible = false;
instructionText.setText('Correct! Choose your next move');
// Show action buttons
continueButton.visible = true;
switchButton.visible = true;
cashOutButton.visible = true;
selectedBox.open();
} else {
// Wrong answer
lifelines--;
LK.getSound('wrong').play();
// Hide lifeline icon
if (lifelines >= 0) {
lifelineIcons[lifelines].visible = false;
}
if (lifelines <= 0) {
// Game over - lost all lifelines
endGame(false, false);
} else {
questionContainer.visible = false;
instructionText.setText('Wrong! You have ' + lifelines + ' lifeline(s) left');
// Show action buttons
continueButton.visible = true;
switchButton.visible = true;
cashOutButton.visible = true;
selectedBox.open();
}
}
}
function endGame(won, cashedOut) {
gameState = 'gameOver';
questionContainer.visible = false;
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
if (won) {
instructionText.setText('CONGRATULATIONS! You found the GRAND PRIZE!');
LK.setScore(10000); // Grand prize value
LK.showYouWin();
} else if (cashedOut) {
instructionText.setText('You cashed out with $' + accumulatedPrize + '!');
LK.setScore(accumulatedPrize);
if (accumulatedPrize > 0) {
LK.showYouWin();
} else {
LK.showGameOver();
}
} else {
instructionText.setText('Game Over! You lost everything!');
LK.setScore(0);
LK.showGameOver();
}
}
game.update = function () {
// Update prize display
if (accumulatedPrize > 0) {
prizeText.setText('Prize: $' + accumulatedPrize);
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Box = Container.expand(function (index, isGrandPrize) {
var self = Container.call(this);
self.index = index;
self.isGrandPrize = isGrandPrize;
self.isOpened = false;
self.isSelected = false;
var boxGraphics = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
var numberText = new Text2((index + 1).toString(), {
size: 60,
fill: 0xFFFFFF
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
self.select = function () {
self.isSelected = true;
self.removeChild(boxGraphics);
boxGraphics = self.attachAsset('selectedBox', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(boxGraphics, 0);
};
self.open = function () {
self.isOpened = true;
self.removeChild(boxGraphics);
if (self.isGrandPrize) {
boxGraphics = self.attachAsset('grandPrizeBox', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
boxGraphics = self.attachAsset('openedBox', {
anchorX: 0.5,
anchorY: 0.5
});
}
self.addChildAt(boxGraphics, 0);
};
self.down = function (x, y, obj) {
if (!self.isOpened) {
if (gameState === 'selecting') {
selectBox(self);
} else if (gameState === 'switching' && !self.isSelected) {
switchToBox(self);
}
}
};
return self;
});
var Button = Container.expand(function (text, callback) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.callback = callback;
self.down = function (x, y, obj) {
if (self.callback) {
self.callback();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var boxes = [];
var selectedBox = null;
var gameState = 'selecting'; // 'selecting', 'question', 'switching', 'gameOver', 'won'
var lifelines = 2;
var accumulatedPrize = 0;
var grandPrizeIndex = Math.floor(Math.random() * 20);
// Trivia questions
var triviaQuestions = [{
question: "What is 2 + 2?",
answer: "4",
options: ["3", "4", "5", "6"],
reward: 100
}, {
question: "Capital of France?",
answer: "Paris",
options: ["London", "Berlin", "Paris", "Rome"],
reward: 150
}, {
question: "Largest ocean?",
answer: "Pacific",
options: ["Atlantic", "Pacific", "Indian", "Arctic"],
reward: 200
}, {
question: "Who painted Mona Lisa?",
answer: "Leonardo",
options: ["Picasso", "Leonardo", "Van Gogh", "Monet"],
reward: 180
}, {
question: "How many days in a year?",
answer: "365",
options: ["364", "365", "366", "367"],
reward: 120
}];
var currentQuestion = null;
// UI Elements
var titleText = new Text2('Caja de Fortuna', {
size: 80,
fill: 0xFFD700
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
titleText.y = 50;
var instructionText = new Text2('Select a box to begin!', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 0.5);
game.addChild(instructionText);
instructionText.x = 1024;
instructionText.y = 400;
var prizeText = new Text2('Prize: $0', {
size: 60,
fill: 0x00FF00
});
prizeText.anchor.set(0.5, 0);
LK.gui.top.addChild(prizeText);
prizeText.y = 150;
var lifelineContainer = new Container();
LK.gui.topRight.addChild(lifelineContainer);
lifelineContainer.x = -150;
lifelineContainer.y = 50;
// Create lifeline icons
var lifelineIcons = [];
for (var i = 0; i < 2; i++) {
var icon = LK.getAsset('lifelineIcon', {
anchorX: 0.5,
anchorY: 0.5
});
icon.x = i * 80;
lifelineContainer.addChild(icon);
lifelineIcons.push(icon);
}
// Create boxes in a 4x5 grid
for (var i = 0; i < 20; i++) {
var row = Math.floor(i / 4);
var col = i % 4;
var box = new Box(i, i === grandPrizeIndex);
box.x = 300 + col * 250;
box.y = 600 + row * 250;
boxes.push(box);
game.addChild(box);
}
// Question UI elements (initially hidden)
var questionContainer = new Container();
game.addChild(questionContainer);
questionContainer.visible = false;
var questionBg = LK.getAsset('button', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 4,
scaleY: 8
});
questionBg.tint = 0x2c3e50;
questionContainer.addChild(questionBg);
var questionText = new Text2('', {
size: 50,
fill: 0xFFFFFF
});
questionText.anchor.set(0.5, 0.5);
questionContainer.addChild(questionText);
questionText.y = -200;
var optionButtons = [];
for (var i = 0; i < 4; i++) {
var button = new Button('', function () {
var selectedAnswer = this.children[1].text;
checkAnswer(selectedAnswer);
});
button.y = -50 + i * 100;
questionContainer.addChild(button);
optionButtons.push(button);
}
questionContainer.x = 1024;
questionContainer.y = 1366;
// Action buttons
var continueButton = new Button('Continue Playing', function () {
gameState = 'question';
showQuestion();
});
var switchButton = new Button('Switch Box', function () {
gameState = 'switching';
instructionText.setText('Select a different box');
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
});
var cashOutButton = new Button('Cash Out', function () {
endGame(false, true);
});
game.addChild(continueButton);
game.addChild(switchButton);
game.addChild(cashOutButton);
continueButton.x = 1024;
continueButton.y = 2200;
continueButton.visible = false;
switchButton.x = 1024;
switchButton.y = 2300;
switchButton.visible = false;
cashOutButton.x = 1024;
cashOutButton.y = 2400;
cashOutButton.visible = false;
function selectBox(box) {
if (selectedBox) {
// Deselect previous box
selectedBox.isSelected = false;
var prevGraphics = selectedBox.children[0];
selectedBox.removeChild(prevGraphics);
var newGraphics = selectedBox.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
selectedBox.addChildAt(newGraphics, 0);
}
selectedBox = box;
selectedBox.select();
LK.getSound('select').play();
if (selectedBox.isGrandPrize) {
// Player found grand prize
selectedBox.open();
endGame(true, false);
return;
}
gameState = 'question';
showQuestion();
}
function switchToBox(newBox) {
selectedBox.isSelected = false;
var prevGraphics = selectedBox.children[0];
selectedBox.removeChild(prevGraphics);
var newGraphics = selectedBox.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
selectedBox.addChildAt(newGraphics, 0);
selectedBox = newBox;
selectedBox.select();
LK.getSound('select').play();
if (selectedBox.isGrandPrize) {
selectedBox.open();
endGame(true, false);
return;
}
gameState = 'question';
showQuestion();
}
function showQuestion() {
currentQuestion = triviaQuestions[Math.floor(Math.random() * triviaQuestions.length)];
questionText.setText(currentQuestion.question);
for (var i = 0; i < 4; i++) {
var optionText = optionButtons[i].children[1];
optionText.setText(currentQuestion.options[i]);
}
questionContainer.visible = true;
instructionText.setText('Answer the question!');
// Hide action buttons
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
}
function checkAnswer(selectedAnswer) {
if (selectedAnswer === currentQuestion.answer) {
// Correct answer
accumulatedPrize += currentQuestion.reward;
prizeText.setText('Prize: $' + accumulatedPrize);
LK.getSound('correct').play();
questionContainer.visible = false;
instructionText.setText('Correct! Choose your next move');
// Show action buttons
continueButton.visible = true;
switchButton.visible = true;
cashOutButton.visible = true;
selectedBox.open();
} else {
// Wrong answer
lifelines--;
LK.getSound('wrong').play();
// Hide lifeline icon
if (lifelines >= 0) {
lifelineIcons[lifelines].visible = false;
}
if (lifelines <= 0) {
// Game over - lost all lifelines
endGame(false, false);
} else {
questionContainer.visible = false;
instructionText.setText('Wrong! You have ' + lifelines + ' lifeline(s) left');
// Show action buttons
continueButton.visible = true;
switchButton.visible = true;
cashOutButton.visible = true;
selectedBox.open();
}
}
}
function endGame(won, cashedOut) {
gameState = 'gameOver';
questionContainer.visible = false;
continueButton.visible = false;
switchButton.visible = false;
cashOutButton.visible = false;
if (won) {
instructionText.setText('CONGRATULATIONS! You found the GRAND PRIZE!');
LK.setScore(10000); // Grand prize value
LK.showYouWin();
} else if (cashedOut) {
instructionText.setText('You cashed out with $' + accumulatedPrize + '!');
LK.setScore(accumulatedPrize);
if (accumulatedPrize > 0) {
LK.showYouWin();
} else {
LK.showGameOver();
}
} else {
instructionText.setText('Game Over! You lost everything!');
LK.setScore(0);
LK.showGameOver();
}
}
game.update = function () {
// Update prize display
if (accumulatedPrize > 0) {
prizeText.setText('Prize: $' + accumulatedPrize);
}
};