User prompt
When we click on the box, a random number pops out. After clicking on the box, we can see which number is inside.
User prompt
When we click on the box, the random number inside will appear.
User prompt
Once the boxes are opened, we can see what's inside.
User prompt
Let us see the random numbers in the boxes.
User prompt
Let there be 100 boxes and numbers on the boxes
User prompt
Please fix the bug: 'Uncaught TypeError: setTimeout is not a function' in or related to this line: 'setTimeout(function () {' Line Number: 89
Code edit (1 edits merged)
Please save this source code
User prompt
Prison Box Challenge
Initial prompt
The game will consist of the following: 100 2D prisoners and a room. Inside the room are 100 boxes with rows from 1 to 100. Inside each box will be a random number from 1 to 100.Each number can only be used once, and each prisoner can enter one by one and view the boxes until he finds the number written on it.The prisoner who finds his number goes out, and the new prisoner cannot see inside the boxes. Each prisoner has the chance to try 50 boxes.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Box = Container.expand(function (boxNumber, containsNumber) {
var self = Container.call(this);
self.boxNumber = boxNumber;
self.containsNumber = containsNumber;
self.isOpen = false;
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.numberText = new Text2(boxNumber.toString(), {
size: 56,
fill: 0xFFFFFF,
font: "bold Arial"
});
self.numberText.anchor.set(0.5, 0.5);
self.numberText.y = -90; // Moved higher to prevent overlap with boxes above
self.addChild(self.numberText);
self.contentText = new Text2(containsNumber.toString(), {
size: 25,
fill: 0x000000
});
self.contentText.anchor.set(0.5, 0.5);
self.contentText.visible = false;
self.addChild(self.contentText);
// Add prisoner number label on the box (hidden)
self.prisonerNumberText = new Text2('P' + containsNumber.toString(), {
size: 32,
fill: 0xFFFFFF
});
self.prisonerNumberText.anchor.set(0.5, 0.5);
self.prisonerNumberText.y = 50;
self.prisonerNumberText.visible = false;
self.addChild(self.prisonerNumberText);
self.openBox = function () {
if (!self.isOpen) {
self.isOpen = true;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('boxOpen', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.setText(self.containsNumber.toString());
self.contentText.visible = true;
self.contentText.alpha = 1.0;
self.contentText.y = 0; // Center the text in the middle of the box
self.contentText.x = 0; // Ensure horizontal centering
// Make the text more prominent with larger size and bold styling
self.contentText.size = 40;
self.contentText.fill = 0x000000; // Black text for better contrast
// Create new Text2 with bold styling
self.removeChild(self.contentText);
self.contentText = new Text2(self.containsNumber.toString(), {
size: 120,
fill: 0x000000,
font: "bold Arial"
});
self.contentText.anchor.set(0.5, 0.5);
self.contentText.y = 0;
self.contentText.x = 0;
self.contentText.visible = true;
self.contentText.alpha = 1.0;
self.addChild(self.contentText);
// Hide the box serial number when opened
self.numberText.visible = false;
// Create pop-out animation effect
self.contentText.scaleX = 0.1;
self.contentText.scaleY = 0.1;
// Animate the number popping out
tween(self.contentText, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 1
}, {
duration: 300,
onFinish: function onFinish() {
// Scale back to normal size after pop effect
tween(self.contentText, {
scaleX: 1,
scaleY: 1
}, {
duration: 200
});
}
});
LK.getSound('boxOpen').play();
return self.containsNumber;
}
return null;
};
self.closeBox = function () {
if (self.isOpen) {
self.isOpen = false;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.visible = false;
}
};
self.down = function (x, y, obj) {
// Always allow opening boxes to see the random number inside
if (!self.isOpen) {
var foundNumber = self.openBox();
// Only process game logic if we're in playing state
if (gameState === 'playing' && attemptsLeft > 0) {
attemptsLeft--;
attemptsText.setText('Attempts: ' + attemptsLeft);
if (foundNumber === currentPrisoner) {
prisonerFound = true;
LK.getSound('success').play();
successText.setText('Prisoner ' + currentPrisoner + ' found their number!');
successText.visible = true;
LK.setTimeout(function () {
nextPrisoner();
}, 2000);
} else if (attemptsLeft <= 0) {
LK.getSound('failure').play();
failureText.setText('Prisoner ' + currentPrisoner + ' failed!');
failureText.visible = true;
gameState = 'gameOver';
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
}
}
};
return self;
});
var Prisoner = Container.expand(function (prisonerNumber) {
var self = Container.call(this);
self.prisonerNumber = prisonerNumber;
self.prisonerGraphic = self.attachAsset('prisoner', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2F4F4F
});
/****
* Game Code
****/
var totalPrisoners = 100;
var boxes = [];
var prisoners = [];
var currentPrisoner = 1;
var attemptsLeft = Math.floor(totalPrisoners / 2);
var maxAttempts = Math.floor(totalPrisoners / 2);
var gameState = 'setup'; // setup, playing, gameOver, victory
var prisonerFound = false;
var successfulPrisoners = 0;
// UI Elements
var titleText = new Text2('100 Məhkum Testi', {
size: 120,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 50;
game.addChild(titleText);
var instructionText = new Text2('Məhkum öz nömrəsini 50 sınamada tapmalıdır. ', {
size: 70,
fill: 0xCCCCCC
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 1024;
instructionText.y = 180;
game.addChild(instructionText);
var currentPrisonerText = new Text2('Məhkumun Sırası: 1', {
size: 90,
fill: 0xFFFF00
});
currentPrisonerText.anchor.set(0.5, 0);
currentPrisonerText.x = 1024;
currentPrisonerText.y = 260;
game.addChild(currentPrisonerText);
var magnifyingGlassIcon = LK.getAsset('magnifyingGlass', {
anchorX: 0.5,
anchorY: 0.5
});
magnifyingGlassIcon.x = 786; // Position to the left of the text (moved 0.5cm/19px left from previous position)
magnifyingGlassIcon.y = 400; // Align with the attempts text
game.addChild(magnifyingGlassIcon);
var attemptsText = new Text2('Axtarış: ' + attemptsLeft, {
size: 80,
fill: 0xFFFFFF
});
attemptsText.anchor.set(0.5, 0);
attemptsText.x = 1024;
attemptsText.y = 360;
game.addChild(attemptsText);
var successText = new Text2('', {
size: 80,
fill: 0x00FF00
});
successText.anchor.set(0.5, 0.5);
successText.x = 1024;
successText.y = 1366;
successText.visible = false;
game.addChild(successText);
var failureText = new Text2('', {
size: 80,
fill: 0xFF0000
});
failureText.anchor.set(0.5, 0.5);
failureText.x = 1024;
failureText.y = 1366;
failureText.visible = false;
game.addChild(failureText);
var progressText = new Text2('0/' + totalPrisoners, {
size: 80,
fill: 0xFFFFFF
});
progressText.anchor.set(1.0, 0);
progressText.x = 2024;
progressText.y = 50;
game.addChild(progressText);
// Create shuffled array of numbers 1 to totalPrisoners
function shuffleArray(array) {
var shuffled = array.slice();
for (var i = shuffled.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = temp;
}
return shuffled;
}
// Initialize the game
function setupGame() {
// Create array of numbers 1 to totalPrisoners
var numbers = [];
for (var i = 1; i <= totalPrisoners; i++) {
numbers.push(i);
}
// Shuffle the numbers
var shuffledNumbers = shuffleArray(numbers);
// Create boxes in a grid
var boxesPerRow = 10;
var spacing = 200; // Increased spacing to prevent overlap
var startX = 1024 - boxesPerRow * spacing / 2 + spacing / 2;
var startY = 600; // Moved down to make space for text above
for (var i = 0; i < totalPrisoners; i++) {
var row = Math.floor(i / boxesPerRow);
var col = i % boxesPerRow;
var box = new Box(i + 1, shuffledNumbers[i]);
box.x = startX + col * spacing;
box.y = startY + row * spacing;
boxes.push(box);
game.addChild(box);
}
// Create current prisoner display
var prisoner = new Prisoner(currentPrisoner);
prisoner.x = 537; // Moved 38 pixels (1cm) to the left from previous position
prisoner.y = 448; // Moved 38 pixels (1cm) down from previous position
prisoners.push(prisoner);
game.addChild(prisoner);
gameState = 'playing';
}
function nextPrisoner() {
// Close all boxes
for (var i = 0; i < boxes.length; i++) {
boxes[i].closeBox();
}
successfulPrisoners++;
progressText.setText(successfulPrisoners + '/' + totalPrisoners);
if (successfulPrisoners >= totalPrisoners) {
// All prisoners succeeded!
gameState = 'victory';
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
return;
}
currentPrisoner++;
attemptsLeft = maxAttempts;
prisonerFound = false;
// Update UI
currentPrisonerText.setText('Current Prisoner: ' + currentPrisoner);
attemptsText.setText('Axtarış: ' + attemptsLeft);
successText.visible = false;
failureText.visible = false;
// Update prisoner display
if (prisoners.length > 0) {
prisoners[0].prisonerNumber = currentPrisoner;
}
gameState = 'playing';
}
// Start the game
setupGame();
// Game update loop
game.update = function () {
// No continuous updates needed for this turn-based game
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Box = Container.expand(function (boxNumber, containsNumber) {
var self = Container.call(this);
self.boxNumber = boxNumber;
self.containsNumber = containsNumber;
self.isOpen = false;
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.numberText = new Text2(boxNumber.toString(), {
size: 56,
fill: 0xFFFFFF,
font: "bold Arial"
});
self.numberText.anchor.set(0.5, 0.5);
self.numberText.y = -90; // Moved higher to prevent overlap with boxes above
self.addChild(self.numberText);
self.contentText = new Text2(containsNumber.toString(), {
size: 25,
fill: 0x000000
});
self.contentText.anchor.set(0.5, 0.5);
self.contentText.visible = false;
self.addChild(self.contentText);
// Add prisoner number label on the box (hidden)
self.prisonerNumberText = new Text2('P' + containsNumber.toString(), {
size: 32,
fill: 0xFFFFFF
});
self.prisonerNumberText.anchor.set(0.5, 0.5);
self.prisonerNumberText.y = 50;
self.prisonerNumberText.visible = false;
self.addChild(self.prisonerNumberText);
self.openBox = function () {
if (!self.isOpen) {
self.isOpen = true;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('boxOpen', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.setText(self.containsNumber.toString());
self.contentText.visible = true;
self.contentText.alpha = 1.0;
self.contentText.y = 0; // Center the text in the middle of the box
self.contentText.x = 0; // Ensure horizontal centering
// Make the text more prominent with larger size and bold styling
self.contentText.size = 40;
self.contentText.fill = 0x000000; // Black text for better contrast
// Create new Text2 with bold styling
self.removeChild(self.contentText);
self.contentText = new Text2(self.containsNumber.toString(), {
size: 120,
fill: 0x000000,
font: "bold Arial"
});
self.contentText.anchor.set(0.5, 0.5);
self.contentText.y = 0;
self.contentText.x = 0;
self.contentText.visible = true;
self.contentText.alpha = 1.0;
self.addChild(self.contentText);
// Hide the box serial number when opened
self.numberText.visible = false;
// Create pop-out animation effect
self.contentText.scaleX = 0.1;
self.contentText.scaleY = 0.1;
// Animate the number popping out
tween(self.contentText, {
scaleX: 1.5,
scaleY: 1.5,
alpha: 1
}, {
duration: 300,
onFinish: function onFinish() {
// Scale back to normal size after pop effect
tween(self.contentText, {
scaleX: 1,
scaleY: 1
}, {
duration: 200
});
}
});
LK.getSound('boxOpen').play();
return self.containsNumber;
}
return null;
};
self.closeBox = function () {
if (self.isOpen) {
self.isOpen = false;
self.removeChild(self.boxGraphic);
self.boxGraphic = self.attachAsset('box', {
anchorX: 0.5,
anchorY: 0.5
});
self.contentText.visible = false;
}
};
self.down = function (x, y, obj) {
// Always allow opening boxes to see the random number inside
if (!self.isOpen) {
var foundNumber = self.openBox();
// Only process game logic if we're in playing state
if (gameState === 'playing' && attemptsLeft > 0) {
attemptsLeft--;
attemptsText.setText('Attempts: ' + attemptsLeft);
if (foundNumber === currentPrisoner) {
prisonerFound = true;
LK.getSound('success').play();
successText.setText('Prisoner ' + currentPrisoner + ' found their number!');
successText.visible = true;
LK.setTimeout(function () {
nextPrisoner();
}, 2000);
} else if (attemptsLeft <= 0) {
LK.getSound('failure').play();
failureText.setText('Prisoner ' + currentPrisoner + ' failed!');
failureText.visible = true;
gameState = 'gameOver';
LK.setTimeout(function () {
LK.showGameOver();
}, 2000);
}
}
}
};
return self;
});
var Prisoner = Container.expand(function (prisonerNumber) {
var self = Container.call(this);
self.prisonerNumber = prisonerNumber;
self.prisonerGraphic = self.attachAsset('prisoner', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2F4F4F
});
/****
* Game Code
****/
var totalPrisoners = 100;
var boxes = [];
var prisoners = [];
var currentPrisoner = 1;
var attemptsLeft = Math.floor(totalPrisoners / 2);
var maxAttempts = Math.floor(totalPrisoners / 2);
var gameState = 'setup'; // setup, playing, gameOver, victory
var prisonerFound = false;
var successfulPrisoners = 0;
// UI Elements
var titleText = new Text2('100 Məhkum Testi', {
size: 120,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
titleText.x = 1024;
titleText.y = 50;
game.addChild(titleText);
var instructionText = new Text2('Məhkum öz nömrəsini 50 sınamada tapmalıdır. ', {
size: 70,
fill: 0xCCCCCC
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 1024;
instructionText.y = 180;
game.addChild(instructionText);
var currentPrisonerText = new Text2('Məhkumun Sırası: 1', {
size: 90,
fill: 0xFFFF00
});
currentPrisonerText.anchor.set(0.5, 0);
currentPrisonerText.x = 1024;
currentPrisonerText.y = 260;
game.addChild(currentPrisonerText);
var magnifyingGlassIcon = LK.getAsset('magnifyingGlass', {
anchorX: 0.5,
anchorY: 0.5
});
magnifyingGlassIcon.x = 786; // Position to the left of the text (moved 0.5cm/19px left from previous position)
magnifyingGlassIcon.y = 400; // Align with the attempts text
game.addChild(magnifyingGlassIcon);
var attemptsText = new Text2('Axtarış: ' + attemptsLeft, {
size: 80,
fill: 0xFFFFFF
});
attemptsText.anchor.set(0.5, 0);
attemptsText.x = 1024;
attemptsText.y = 360;
game.addChild(attemptsText);
var successText = new Text2('', {
size: 80,
fill: 0x00FF00
});
successText.anchor.set(0.5, 0.5);
successText.x = 1024;
successText.y = 1366;
successText.visible = false;
game.addChild(successText);
var failureText = new Text2('', {
size: 80,
fill: 0xFF0000
});
failureText.anchor.set(0.5, 0.5);
failureText.x = 1024;
failureText.y = 1366;
failureText.visible = false;
game.addChild(failureText);
var progressText = new Text2('0/' + totalPrisoners, {
size: 80,
fill: 0xFFFFFF
});
progressText.anchor.set(1.0, 0);
progressText.x = 2024;
progressText.y = 50;
game.addChild(progressText);
// Create shuffled array of numbers 1 to totalPrisoners
function shuffleArray(array) {
var shuffled = array.slice();
for (var i = shuffled.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = shuffled[i];
shuffled[i] = shuffled[j];
shuffled[j] = temp;
}
return shuffled;
}
// Initialize the game
function setupGame() {
// Create array of numbers 1 to totalPrisoners
var numbers = [];
for (var i = 1; i <= totalPrisoners; i++) {
numbers.push(i);
}
// Shuffle the numbers
var shuffledNumbers = shuffleArray(numbers);
// Create boxes in a grid
var boxesPerRow = 10;
var spacing = 200; // Increased spacing to prevent overlap
var startX = 1024 - boxesPerRow * spacing / 2 + spacing / 2;
var startY = 600; // Moved down to make space for text above
for (var i = 0; i < totalPrisoners; i++) {
var row = Math.floor(i / boxesPerRow);
var col = i % boxesPerRow;
var box = new Box(i + 1, shuffledNumbers[i]);
box.x = startX + col * spacing;
box.y = startY + row * spacing;
boxes.push(box);
game.addChild(box);
}
// Create current prisoner display
var prisoner = new Prisoner(currentPrisoner);
prisoner.x = 537; // Moved 38 pixels (1cm) to the left from previous position
prisoner.y = 448; // Moved 38 pixels (1cm) down from previous position
prisoners.push(prisoner);
game.addChild(prisoner);
gameState = 'playing';
}
function nextPrisoner() {
// Close all boxes
for (var i = 0; i < boxes.length; i++) {
boxes[i].closeBox();
}
successfulPrisoners++;
progressText.setText(successfulPrisoners + '/' + totalPrisoners);
if (successfulPrisoners >= totalPrisoners) {
// All prisoners succeeded!
gameState = 'victory';
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
return;
}
currentPrisoner++;
attemptsLeft = maxAttempts;
prisonerFound = false;
// Update UI
currentPrisonerText.setText('Current Prisoner: ' + currentPrisoner);
attemptsText.setText('Axtarış: ' + attemptsLeft);
successText.visible = false;
failureText.visible = false;
// Update prisoner display
if (prisoners.length > 0) {
prisoners[0].prisonerNumber = currentPrisoner;
}
gameState = 'playing';
}
// Start the game
setupGame();
// Game update loop
game.update = function () {
// No continuous updates needed for this turn-based game
};