User prompt
add score title is treasure collection
User prompt
set the score text as treasure and diamond collection
User prompt
Please fix the bug: 'ReferenceError: treasureScoreTxt is not defined' in or related to this line: 'treasureScoreTxt.setText('Treasures: ' + treasures.filter(function (t) {' Line Number: 559
User prompt
add a two score titles 1)treasure here show me the how many treasure collected and score each treasue for 50 2)diamond show howmany diamonds collected each diamond got 10
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'if (levelGrid[y][x] === 'S') {' Line Number: 403
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var y = 0; y < currentLayout.length; y++) {' Line Number: 438
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var y = 0; y < currentLayout.length; y++) {' Line Number: 441
User prompt
Please fix the bug: 'currentLayout is not defined' in or related to this line: 'for (var y = 0; y < currentLayout.length; y++) {' Line Number: 440
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'if (levelGrid[y][x] === 'S') {' Line Number: 403
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var y = 0; y < currentLayout.length; y++) {' Line Number: 438
Code edit (13 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var x = 0; x < currentLayout[y].length; x++) {' Line Number: 445
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading '0')' in or related to this line: 'if (levelGrid[y][x] === 'S') {' Line Number: 403
Code edit (1 edits merged)
Please save this source code
User prompt
some wall are blocking the way just remove that wall in the game
User prompt
VA in the game no way for explorer to move and get key make a possible and easy between the wall for explorer to get the key
User prompt
in the game noway for explorer to move and get key
User prompt
i need the walls structure like a puzzle
User prompt
create a possible way that should be easy to move over all the game the explorer
User prompt
some wall are in the way (like obstacles )delete only that wall in the game
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { level: 1, collectedKeys: 0, lives: 3 }); /**** * Classes ****/ var Diamond = Container.expand(function () { var self = Container.call(this); var diamondGraphics = self.attachAsset('diamond', { anchorX: 0.5, anchorY: 0.5 }); self.isCollected = false; self.collect = function () { if (!self.isCollected) { self.isCollected = true; tween(diamondGraphics, { scaleX: 1.3, scaleY: 1.3 }, { duration: 300, onFinish: function onFinish() { self.destroy(); } }); LK.setScore(LK.getScore() + 30); // Diamonds give 30 points LK.getSound('collect').play(); } }; self.update = function () { if (!self.isCollected) { // Shine animation var glowIntensity = (Math.sin(LK.ticks / 30) + 1) / 2; diamondGraphics.alpha = 0.7 + glowIntensity * 0.3; } }; return self; }); var Explorer = Container.expand(function () { var self = Container.call(this); var explorerGraphics = self.attachAsset('explorer', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.isMoving = false; self.direction = null; self.targetX = 0; self.targetY = 0; self.hasKey = false; self.moveToward = function (x, y) { self.isMoving = true; self.targetX = x; self.targetY = y; // Calculate direction var dx = self.targetX - self.x; var dy = self.targetY - self.y; // Determine primary direction based on larger delta if (Math.abs(dx) > Math.abs(dy)) { self.direction = dx > 0 ? 'right' : 'left'; } else { self.direction = dy > 0 ? 'down' : 'up'; } }; self.stopMoving = function () { self.isMoving = false; self.direction = null; }; self.update = function () { if (self.isMoving) { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < self.speed) { self.x = self.targetX; self.y = self.targetY; self.stopMoving(); } else { var ratio = self.speed / distance; self.x += dx * ratio; self.y += dy * ratio; } } }; self.collectKey = function () { self.hasKey = true; tween(explorerGraphics, { tint: 0xFFD700 }, { duration: 300, onFinish: function onFinish() { tween(explorerGraphics, { tint: 0xFFFFFF }, { duration: 300 }); } }); }; self.useKey = function () { self.hasKey = false; storage.collectedKeys++; }; self.takeDamage = function () { LK.effects.flashObject(self, 0xFF0000, 500); storage.lives--; LK.getSound('damage').play(); if (storage.lives <= 0) { LK.showGameOver(); } }; return self; }); var FallingRock = Container.expand(function () { var self = Container.call(this); var rockGraphics = self.attachAsset('rock', { anchorX: 0.5, anchorY: 0.5 }); self.isTriggered = false; self.isFalling = false; self.initialY = 0; self.fallSpeed = 0; self.resetTimer = 0; self.resetDelay = 180; // 3 seconds self.trigger = function () { if (!self.isTriggered && !self.isFalling) { self.isTriggered = true; self.fallSpeed = 2; // Start with slow fall // Shake before falling tween(rockGraphics, { x: 5 }, { duration: 100, onFinish: function onFinish() { tween(rockGraphics, { x: -5 }, { duration: 100, onFinish: function onFinish() { tween(rockGraphics, { x: 0 }, { duration: 100, onFinish: function onFinish() { self.isFalling = true; self.isTriggered = false; } }); } }); } }); } }; self.reset = function () { self.y = self.initialY; self.isFalling = false; self.fallSpeed = 0; self.resetTimer = 0; }; self.update = function () { if (self.isFalling) { self.fallSpeed += 0.2; // Acceleration self.y += self.fallSpeed; // Check if rock has fallen out of view if (self.y > 2832) { self.isFalling = false; self.resetTimer = 0; } } else if (self.y !== self.initialY) { // Reset timer self.resetTimer++; if (self.resetTimer >= self.resetDelay) { self.reset(); } } }; return self; }); var Key = Container.expand(function () { var self = Container.call(this); var keyGraphics = self.attachAsset('key', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Float animation keyGraphics.y = Math.sin(LK.ticks / 20) * 5; // Rotate slowly keyGraphics.rotation += 0.01; }; return self; }); var Treasure = Container.expand(function () { var self = Container.call(this); var treasureGraphics = self.attachAsset('treasure', { anchorX: 0.5, anchorY: 0.5 }); self.isCollected = false; self.collect = function () { if (!self.isCollected) { self.isCollected = true; tween(treasureGraphics, { scaleX: 1.3, scaleY: 1.3 }, { duration: 300, onFinish: function onFinish() { self.destroy(); } }); LK.setScore(LK.getScore() + 50); LK.getSound('collectTreasure').play(); // Check if all treasures are collected var allCollected = true; for (var i = 0; i < treasures.length; i++) { if (!treasures[i].isCollected) { allCollected = false; break; } } // Removed game end condition when all treasures are collected } }; self.update = function () { if (!self.isCollected) { // Shine animation var glowIntensity = (Math.sin(LK.ticks / 30) + 1) / 2; treasureGraphics.alpha = 0.7 + glowIntensity * 0.3; } }; return self; }); var Wall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('wall', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x607D8B // Cave-like dark blue-gray }); /**** * Game Code ****/ function shouldRemoveWall(x, y) { // Define conditions to remove walls, e.g., based on position // Example: Remove walls at specific coordinates var removablePositions = [{ x: 5, y: 5 }, { x: 10, y: 10 } // Add more positions as needed ]; return removablePositions.some(function (pos) { return pos.x === x && pos.y === y; }); } // New diamond asset // Game variables var tileSize = 100; var gridWidth = 20; var gridHeight = 27; var explorer; var walls = []; var keys = []; var doors = []; var treasures = []; var spikes = []; var diamonds = []; var fallingRocks = []; var levelGrid = []; var gameStarted = false; // UI Elements var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); var levelTxt = new Text2('Level: ' + storage.level, { size: 60, fill: 0xFFFFFF }); var keysTxt = new Text2('Keys: ' + storage.collectedKeys, { size: 60, fill: 0xFFD700 }); var livesTxt = new Text2('Lives: ' + storage.lives, { size: 60, fill: 0xFF5252 }); // Initialize game function initializeGame() { // Clear previous game elements resetGameElements(); // Generate level based on current level generateLevel(storage.level); // Initialize explorer createExplorer(); // Setup UI setupUI(); // Start game gameStarted = true; // Play background music LK.playMusic('caveMusic', { fade: { start: 0, end: 0.3, duration: 1000 } }); } function resetGameElements() { // Remove all existing game elements for (var i = 0; i < walls.length; i++) { walls[i].destroy(); } walls = []; for (var i = 0; i < keys.length; i++) { keys[i].destroy(); } keys = []; for (var i = 0; i < treasures.length; i++) { treasures[i].destroy(); } treasures = []; for (var i = 0; i < fallingRocks.length; i++) { fallingRocks[i].destroy(); } fallingRocks = []; levelGrid = []; if (explorer) { explorer.destroy(); explorer = null; } } function createExplorer() { explorer = new Explorer(); game.addChild(explorer); // Position explorer at start position for (var y = 0; y < gridHeight; y++) { for (var x = 0; x < gridWidth; x++) { if (levelGrid[y][x] === 'S') { explorer.x = x * tileSize + tileSize / 2; explorer.y = y * tileSize + tileSize / 2; break; } } } } function setupUI() { // Setup score text scoreTxt.setText(LK.getScore()); scoreTxt.anchor.set(1, 0); LK.gui.topRight.addChild(scoreTxt); // Setup level text levelTxt.setText('Level: ' + storage.level); levelTxt.anchor.set(0.5, 0); levelTxt.y = 80; LK.gui.top.addChild(levelTxt); // Setup keys text keysTxt.setText('Keys: ' + storage.collectedKeys); keysTxt.anchor.set(0, 0); keysTxt.y = 80; LK.gui.topRight.addChild(keysTxt); // Setup lives text livesTxt.setText('Lives: ' + storage.lives); livesTxt.anchor.set(0, 0); LK.gui.topRight.addChild(livesTxt); } function generateLevel(level) { // Define different level layouts var layouts = []; // Level 1: Simple tutorial level layouts[1] = ["WWWWWWWWWWWWWWWWWWWW", "W W W", "W WWWWWW W WWWWWWW W", "W W K W W", "W W WWWWWWWWWWWW W W", "W W W W W", "W W W WWWWWWWWWW W W", "W W W W W W W", "W W W W WWWWWW W W W", "W W W W W W W W W", "W W W W W WW W W W W", "W W W W W W W W", "W WWWWW WWWWWWWW W W", "W W W", "W WWWWWWWWWWWWWWWW W", "W W", "W WWWWWWWW WWWWWWW W", "W W W W W", "W W WWWW WWWWWWW W W", "W W W W W W", "W W W WWWWWWWWW W W W", "W W W W W W W", "W W WWWWWWWWW W W W W", "W W DW W W W W", "W WWWWWWWWWWW W W TS W", "W W W", "WWWWWWWWWWWWWWWWWWWWW"]; // Get layout for the single level var currentLayout = layouts[1]; // Parse level layout for (var y = 0; y < currentLayout.length; y++) { levelGrid[y] = []; for (var x = 0; x < currentLayout[y].length; x++) { var cellType = currentLayout[y][x]; levelGrid[y][x] = cellType; var xPos = x * tileSize + tileSize / 2; var Pos = xPos; // Define Pos variable var yPos = y * tileSize + tileSize / 2; switch (cellType) { case 'W': // Wall if (!shouldRemoveWall(x, y)) { var wall = new Wall(); wall.x = Pos; wall.y = yPos; game.addChild(wall); walls.push(wall); } break; case 'K': // Key var key = new Key(); key.x = xPos; key.y = yPos; game.addChild(key); keys.push(key); break; case 'T': // Treasure var treasure = new Treasure(); treasure.x = xPos; treasure.y = yPos; game.addChild(treasure); treasures.push(treasure); break; case 'F': // Falling rock var rock = new FallingRock(); rock.x = xPos; rock.y = yPos; rock.initialY = yPos; game.addChild(rock); fallingRocks.push(rock); break; case 'D': // Diamond var diamond = new Diamond(); diamond.x = xPos; diamond.y = yPos; game.addChild(diamond); diamonds.push(diamond); break; } } } } function getGridPosition(x, y) { var gridX = Math.floor(x / tileSize); var gridY = Math.floor(y / tileSize); if (gridX >= 0 && gridX < gridWidth && gridY >= 0 && gridY < gridHeight) { return { x: gridX, y: gridY }; } return null; } function isWalkable(gridX, gridY) { if (gridX < 0 || gridX >= gridWidth || gridY < 0 || gridY >= gridHeight) { return false; } var cellType = levelGrid[gridY][gridX]; // Check if cell is walkable if (cellType === 'W') { return false; } return true; } function getDoorAt(gridX, gridY) { for (var i = 0; i < doors.length; i++) { var doorPos = getGridPosition(doors[i].x, doors[i].y); if (doorPos && doorPos.x === gridX && doorPos.y === gridY) { return doors[i]; } } return null; } function getRiverAt(gridX, gridY) { for (var i = 0; i < rivers.length; i++) { var riverPos = getGridPosition(rivers[i].x, rivers[i].y); if (riverPos && riverPos.x === gridX && riverPos.y === gridY) { return rivers[i]; } } return null; } // Update UI function updateUI() { scoreTxt.setText(LK.getScore()); levelTxt.setText('Level: ' + storage.level); keysTxt.setText('Keys: ' + storage.collectedKeys); livesTxt.setText('Lives: ' + storage.lives); } // Check for collisions function checkCollisions() { if (!explorer) { return; } var explorerPos = getGridPosition(explorer.x, explorer.y); if (!explorerPos) { return; } var cellType = levelGrid[explorerPos.y][explorerPos.x]; // Check key collision if (cellType === 'K') { for (var i = 0; i < keys.length; i++) { var keyPos = getGridPosition(keys[i].x, keys[i].y); if (keyPos && keyPos.x === explorerPos.x && keyPos.y === explorerPos.y) { explorer.collectKey(); keys[i].destroy(); keys.splice(i, 1); levelGrid[explorerPos.y][explorerPos.x] = ' '; LK.getSound('collect').play(); break; } } } // Check treasure collision if (cellType === 'T') { for (var i = 0; i < treasures.length; i++) { var treasurePos = getGridPosition(treasures[i].x, treasures[i].y); if (treasurePos && treasurePos.x === explorerPos.x && treasurePos.y === explorerPos.y && !treasures[i].isCollected) { treasures[i].collect(); break; } } } // Check diamond collision for (var i = 0; i < diamonds.length; i++) { var diamondPos = getGridPosition(diamonds[i].x, diamonds[i].y); if (diamondPos && diamondPos.x === explorerPos.x && diamondPos.y === explorerPos.y && !diamonds[i].isCollected) { diamonds[i].collect(); break; } } // Check falling rock collision for (var i = 0; i < fallingRocks.length; i++) { var rock = fallingRocks[i]; // Trigger rock if explorer is underneath var rockPos = getGridPosition(rock.x, rock.y); if (rockPos && explorerPos.x === rockPos.x && explorerPos.y > rockPos.y && !rock.isTriggered && !rock.isFalling) { rock.trigger(); } // Check if rock hits explorer if (rock.isFalling && explorer.intersects(rock)) { explorer.takeDamage(); rock.reset(); } } } function moveExplorer(targetX, targetY) { if (!explorer || explorer.isMoving) { return; } var currentGridPos = getGridPosition(explorer.x, explorer.y); var targetGridPos = getGridPosition(targetX, targetY); if (!currentGridPos || !targetGridPos) { return; } // Only allow movement to adjacent cells var dx = Math.abs(targetGridPos.x - currentGridPos.x); var dy = Math.abs(targetGridPos.y - currentGridPos.y); if (dx === 1 && dy === 0 || dx === 0 && dy === 1) { if (isWalkable(targetGridPos.x, targetGridPos.y)) { var cellCenterX = targetGridPos.x * tileSize + tileSize / 2; var cellCenterY = targetGridPos.y * tileSize + tileSize / 2; explorer.moveToward(cellCenterX, cellCenterY); } } // Check if the explorer is at the key's position if (levelGrid[currentGridPos.y][currentGridPos.x] === 'K') { explorer.collectKey(); for (var i = 0; i < keys.length; i++) { var keyPos = getGridPosition(keys[i].x, keys[i].y); if (keyPos && keyPos.x === currentGridPos.x && keyPos.y === currentGridPos.y) { keys[i].destroy(); keys.splice(i, 1); levelGrid[currentGridPos.y][currentGridPos.x] = ' '; LK.getSound('collect').play(); break; } } } } // Handle touch/mouse events function handleGameInput(x, y, obj) { if (!gameStarted) { return; } moveExplorer(x, y); } // Start game when page loads initializeGame(); // Handle touch/mouse events game.down = handleGameInput; // Game update loop game.update = function () { if (!gameStarted) { return; } // Update UI updateUI(); // Check collisions checkCollisions(); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
level: 1,
collectedKeys: 0,
lives: 3
});
/****
* Classes
****/
var Diamond = Container.expand(function () {
var self = Container.call(this);
var diamondGraphics = self.attachAsset('diamond', {
anchorX: 0.5,
anchorY: 0.5
});
self.isCollected = false;
self.collect = function () {
if (!self.isCollected) {
self.isCollected = true;
tween(diamondGraphics, {
scaleX: 1.3,
scaleY: 1.3
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
}
});
LK.setScore(LK.getScore() + 30); // Diamonds give 30 points
LK.getSound('collect').play();
}
};
self.update = function () {
if (!self.isCollected) {
// Shine animation
var glowIntensity = (Math.sin(LK.ticks / 30) + 1) / 2;
diamondGraphics.alpha = 0.7 + glowIntensity * 0.3;
}
};
return self;
});
var Explorer = Container.expand(function () {
var self = Container.call(this);
var explorerGraphics = self.attachAsset('explorer', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.isMoving = false;
self.direction = null;
self.targetX = 0;
self.targetY = 0;
self.hasKey = false;
self.moveToward = function (x, y) {
self.isMoving = true;
self.targetX = x;
self.targetY = y;
// Calculate direction
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
// Determine primary direction based on larger delta
if (Math.abs(dx) > Math.abs(dy)) {
self.direction = dx > 0 ? 'right' : 'left';
} else {
self.direction = dy > 0 ? 'down' : 'up';
}
};
self.stopMoving = function () {
self.isMoving = false;
self.direction = null;
};
self.update = function () {
if (self.isMoving) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.x = self.targetX;
self.y = self.targetY;
self.stopMoving();
} else {
var ratio = self.speed / distance;
self.x += dx * ratio;
self.y += dy * ratio;
}
}
};
self.collectKey = function () {
self.hasKey = true;
tween(explorerGraphics, {
tint: 0xFFD700
}, {
duration: 300,
onFinish: function onFinish() {
tween(explorerGraphics, {
tint: 0xFFFFFF
}, {
duration: 300
});
}
});
};
self.useKey = function () {
self.hasKey = false;
storage.collectedKeys++;
};
self.takeDamage = function () {
LK.effects.flashObject(self, 0xFF0000, 500);
storage.lives--;
LK.getSound('damage').play();
if (storage.lives <= 0) {
LK.showGameOver();
}
};
return self;
});
var FallingRock = Container.expand(function () {
var self = Container.call(this);
var rockGraphics = self.attachAsset('rock', {
anchorX: 0.5,
anchorY: 0.5
});
self.isTriggered = false;
self.isFalling = false;
self.initialY = 0;
self.fallSpeed = 0;
self.resetTimer = 0;
self.resetDelay = 180; // 3 seconds
self.trigger = function () {
if (!self.isTriggered && !self.isFalling) {
self.isTriggered = true;
self.fallSpeed = 2; // Start with slow fall
// Shake before falling
tween(rockGraphics, {
x: 5
}, {
duration: 100,
onFinish: function onFinish() {
tween(rockGraphics, {
x: -5
}, {
duration: 100,
onFinish: function onFinish() {
tween(rockGraphics, {
x: 0
}, {
duration: 100,
onFinish: function onFinish() {
self.isFalling = true;
self.isTriggered = false;
}
});
}
});
}
});
}
};
self.reset = function () {
self.y = self.initialY;
self.isFalling = false;
self.fallSpeed = 0;
self.resetTimer = 0;
};
self.update = function () {
if (self.isFalling) {
self.fallSpeed += 0.2; // Acceleration
self.y += self.fallSpeed;
// Check if rock has fallen out of view
if (self.y > 2832) {
self.isFalling = false;
self.resetTimer = 0;
}
} else if (self.y !== self.initialY) {
// Reset timer
self.resetTimer++;
if (self.resetTimer >= self.resetDelay) {
self.reset();
}
}
};
return self;
});
var Key = Container.expand(function () {
var self = Container.call(this);
var keyGraphics = self.attachAsset('key', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Float animation
keyGraphics.y = Math.sin(LK.ticks / 20) * 5;
// Rotate slowly
keyGraphics.rotation += 0.01;
};
return self;
});
var Treasure = Container.expand(function () {
var self = Container.call(this);
var treasureGraphics = self.attachAsset('treasure', {
anchorX: 0.5,
anchorY: 0.5
});
self.isCollected = false;
self.collect = function () {
if (!self.isCollected) {
self.isCollected = true;
tween(treasureGraphics, {
scaleX: 1.3,
scaleY: 1.3
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
}
});
LK.setScore(LK.getScore() + 50);
LK.getSound('collectTreasure').play();
// Check if all treasures are collected
var allCollected = true;
for (var i = 0; i < treasures.length; i++) {
if (!treasures[i].isCollected) {
allCollected = false;
break;
}
}
// Removed game end condition when all treasures are collected
}
};
self.update = function () {
if (!self.isCollected) {
// Shine animation
var glowIntensity = (Math.sin(LK.ticks / 30) + 1) / 2;
treasureGraphics.alpha = 0.7 + glowIntensity * 0.3;
}
};
return self;
});
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x607D8B // Cave-like dark blue-gray
});
/****
* Game Code
****/
function shouldRemoveWall(x, y) {
// Define conditions to remove walls, e.g., based on position
// Example: Remove walls at specific coordinates
var removablePositions = [{
x: 5,
y: 5
}, {
x: 10,
y: 10
}
// Add more positions as needed
];
return removablePositions.some(function (pos) {
return pos.x === x && pos.y === y;
});
}
// New diamond asset
// Game variables
var tileSize = 100;
var gridWidth = 20;
var gridHeight = 27;
var explorer;
var walls = [];
var keys = [];
var doors = [];
var treasures = [];
var spikes = [];
var diamonds = [];
var fallingRocks = [];
var levelGrid = [];
var gameStarted = false;
// UI Elements
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
var levelTxt = new Text2('Level: ' + storage.level, {
size: 60,
fill: 0xFFFFFF
});
var keysTxt = new Text2('Keys: ' + storage.collectedKeys, {
size: 60,
fill: 0xFFD700
});
var livesTxt = new Text2('Lives: ' + storage.lives, {
size: 60,
fill: 0xFF5252
});
// Initialize game
function initializeGame() {
// Clear previous game elements
resetGameElements();
// Generate level based on current level
generateLevel(storage.level);
// Initialize explorer
createExplorer();
// Setup UI
setupUI();
// Start game
gameStarted = true;
// Play background music
LK.playMusic('caveMusic', {
fade: {
start: 0,
end: 0.3,
duration: 1000
}
});
}
function resetGameElements() {
// Remove all existing game elements
for (var i = 0; i < walls.length; i++) {
walls[i].destroy();
}
walls = [];
for (var i = 0; i < keys.length; i++) {
keys[i].destroy();
}
keys = [];
for (var i = 0; i < treasures.length; i++) {
treasures[i].destroy();
}
treasures = [];
for (var i = 0; i < fallingRocks.length; i++) {
fallingRocks[i].destroy();
}
fallingRocks = [];
levelGrid = [];
if (explorer) {
explorer.destroy();
explorer = null;
}
}
function createExplorer() {
explorer = new Explorer();
game.addChild(explorer);
// Position explorer at start position
for (var y = 0; y < gridHeight; y++) {
for (var x = 0; x < gridWidth; x++) {
if (levelGrid[y][x] === 'S') {
explorer.x = x * tileSize + tileSize / 2;
explorer.y = y * tileSize + tileSize / 2;
break;
}
}
}
}
function setupUI() {
// Setup score text
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
// Setup level text
levelTxt.setText('Level: ' + storage.level);
levelTxt.anchor.set(0.5, 0);
levelTxt.y = 80;
LK.gui.top.addChild(levelTxt);
// Setup keys text
keysTxt.setText('Keys: ' + storage.collectedKeys);
keysTxt.anchor.set(0, 0);
keysTxt.y = 80;
LK.gui.topRight.addChild(keysTxt);
// Setup lives text
livesTxt.setText('Lives: ' + storage.lives);
livesTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(livesTxt);
}
function generateLevel(level) {
// Define different level layouts
var layouts = [];
// Level 1: Simple tutorial level
layouts[1] = ["WWWWWWWWWWWWWWWWWWWW", "W W W", "W WWWWWW W WWWWWWW W", "W W K W W", "W W WWWWWWWWWWWW W W", "W W W W W", "W W W WWWWWWWWWW W W", "W W W W W W W", "W W W W WWWWWW W W W", "W W W W W W W W W", "W W W W W WW W W W W", "W W W W W W W W", "W WWWWW WWWWWWWW W W", "W W W", "W WWWWWWWWWWWWWWWW W", "W W", "W WWWWWWWW WWWWWWW W", "W W W W W", "W W WWWW WWWWWWW W W", "W W W W W W", "W W W WWWWWWWWW W W W", "W W W W W W W", "W W WWWWWWWWW W W W W", "W W DW W W W W", "W WWWWWWWWWWW W W TS W", "W W W", "WWWWWWWWWWWWWWWWWWWWW"];
// Get layout for the single level
var currentLayout = layouts[1];
// Parse level layout
for (var y = 0; y < currentLayout.length; y++) {
levelGrid[y] = [];
for (var x = 0; x < currentLayout[y].length; x++) {
var cellType = currentLayout[y][x];
levelGrid[y][x] = cellType;
var xPos = x * tileSize + tileSize / 2;
var Pos = xPos; // Define Pos variable
var yPos = y * tileSize + tileSize / 2;
switch (cellType) {
case 'W':
// Wall
if (!shouldRemoveWall(x, y)) {
var wall = new Wall();
wall.x = Pos;
wall.y = yPos;
game.addChild(wall);
walls.push(wall);
}
break;
case 'K':
// Key
var key = new Key();
key.x = xPos;
key.y = yPos;
game.addChild(key);
keys.push(key);
break;
case 'T':
// Treasure
var treasure = new Treasure();
treasure.x = xPos;
treasure.y = yPos;
game.addChild(treasure);
treasures.push(treasure);
break;
case 'F':
// Falling rock
var rock = new FallingRock();
rock.x = xPos;
rock.y = yPos;
rock.initialY = yPos;
game.addChild(rock);
fallingRocks.push(rock);
break;
case 'D':
// Diamond
var diamond = new Diamond();
diamond.x = xPos;
diamond.y = yPos;
game.addChild(diamond);
diamonds.push(diamond);
break;
}
}
}
}
function getGridPosition(x, y) {
var gridX = Math.floor(x / tileSize);
var gridY = Math.floor(y / tileSize);
if (gridX >= 0 && gridX < gridWidth && gridY >= 0 && gridY < gridHeight) {
return {
x: gridX,
y: gridY
};
}
return null;
}
function isWalkable(gridX, gridY) {
if (gridX < 0 || gridX >= gridWidth || gridY < 0 || gridY >= gridHeight) {
return false;
}
var cellType = levelGrid[gridY][gridX];
// Check if cell is walkable
if (cellType === 'W') {
return false;
}
return true;
}
function getDoorAt(gridX, gridY) {
for (var i = 0; i < doors.length; i++) {
var doorPos = getGridPosition(doors[i].x, doors[i].y);
if (doorPos && doorPos.x === gridX && doorPos.y === gridY) {
return doors[i];
}
}
return null;
}
function getRiverAt(gridX, gridY) {
for (var i = 0; i < rivers.length; i++) {
var riverPos = getGridPosition(rivers[i].x, rivers[i].y);
if (riverPos && riverPos.x === gridX && riverPos.y === gridY) {
return rivers[i];
}
}
return null;
}
// Update UI
function updateUI() {
scoreTxt.setText(LK.getScore());
levelTxt.setText('Level: ' + storage.level);
keysTxt.setText('Keys: ' + storage.collectedKeys);
livesTxt.setText('Lives: ' + storage.lives);
}
// Check for collisions
function checkCollisions() {
if (!explorer) {
return;
}
var explorerPos = getGridPosition(explorer.x, explorer.y);
if (!explorerPos) {
return;
}
var cellType = levelGrid[explorerPos.y][explorerPos.x];
// Check key collision
if (cellType === 'K') {
for (var i = 0; i < keys.length; i++) {
var keyPos = getGridPosition(keys[i].x, keys[i].y);
if (keyPos && keyPos.x === explorerPos.x && keyPos.y === explorerPos.y) {
explorer.collectKey();
keys[i].destroy();
keys.splice(i, 1);
levelGrid[explorerPos.y][explorerPos.x] = ' ';
LK.getSound('collect').play();
break;
}
}
}
// Check treasure collision
if (cellType === 'T') {
for (var i = 0; i < treasures.length; i++) {
var treasurePos = getGridPosition(treasures[i].x, treasures[i].y);
if (treasurePos && treasurePos.x === explorerPos.x && treasurePos.y === explorerPos.y && !treasures[i].isCollected) {
treasures[i].collect();
break;
}
}
}
// Check diamond collision
for (var i = 0; i < diamonds.length; i++) {
var diamondPos = getGridPosition(diamonds[i].x, diamonds[i].y);
if (diamondPos && diamondPos.x === explorerPos.x && diamondPos.y === explorerPos.y && !diamonds[i].isCollected) {
diamonds[i].collect();
break;
}
}
// Check falling rock collision
for (var i = 0; i < fallingRocks.length; i++) {
var rock = fallingRocks[i];
// Trigger rock if explorer is underneath
var rockPos = getGridPosition(rock.x, rock.y);
if (rockPos && explorerPos.x === rockPos.x && explorerPos.y > rockPos.y && !rock.isTriggered && !rock.isFalling) {
rock.trigger();
}
// Check if rock hits explorer
if (rock.isFalling && explorer.intersects(rock)) {
explorer.takeDamage();
rock.reset();
}
}
}
function moveExplorer(targetX, targetY) {
if (!explorer || explorer.isMoving) {
return;
}
var currentGridPos = getGridPosition(explorer.x, explorer.y);
var targetGridPos = getGridPosition(targetX, targetY);
if (!currentGridPos || !targetGridPos) {
return;
}
// Only allow movement to adjacent cells
var dx = Math.abs(targetGridPos.x - currentGridPos.x);
var dy = Math.abs(targetGridPos.y - currentGridPos.y);
if (dx === 1 && dy === 0 || dx === 0 && dy === 1) {
if (isWalkable(targetGridPos.x, targetGridPos.y)) {
var cellCenterX = targetGridPos.x * tileSize + tileSize / 2;
var cellCenterY = targetGridPos.y * tileSize + tileSize / 2;
explorer.moveToward(cellCenterX, cellCenterY);
}
}
// Check if the explorer is at the key's position
if (levelGrid[currentGridPos.y][currentGridPos.x] === 'K') {
explorer.collectKey();
for (var i = 0; i < keys.length; i++) {
var keyPos = getGridPosition(keys[i].x, keys[i].y);
if (keyPos && keyPos.x === currentGridPos.x && keyPos.y === currentGridPos.y) {
keys[i].destroy();
keys.splice(i, 1);
levelGrid[currentGridPos.y][currentGridPos.x] = ' ';
LK.getSound('collect').play();
break;
}
}
}
}
// Handle touch/mouse events
function handleGameInput(x, y, obj) {
if (!gameStarted) {
return;
}
moveExplorer(x, y);
}
// Start game when page loads
initializeGame();
// Handle touch/mouse events
game.down = handleGameInput;
// Game update loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update UI
updateUI();
// Check collisions
checkCollisions();
};
small rock. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a magic crackers boom effect. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
create a glod color key. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows