/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { coins: 0, pickLevel: 1 }); /**** * Classes ****/ var Digger = Container.expand(function () { var self = Container.call(this); var diggerGraphics = self.attachAsset('digger', { anchorX: 0.5, anchorY: 0.5 }); self.digging = false; self.digStrength = 1; self.startDig = function (targetX, targetY) { self.digging = true; self.x = targetX; self.y = targetY; // Make digger appear to dig with animation tween(diggerGraphics, { rotation: Math.PI * 2 }, { duration: 500, easing: tween.easeInOut, onFinish: function onFinish() { diggerGraphics.rotation = 0; self.digging = false; } }); }; self.upgrade = function (level) { self.digStrength = level; // Visual indication of upgraded digger diggerGraphics.scaleX = 1 + level * 0.1; diggerGraphics.scaleY = 1 + level * 0.1; }; return self; }); var Dirt = Container.expand(function (type, gemType) { var self = Container.call(this); var assetType = type === 'hard' ? 'hardDirt' : 'normalDirt'; var dirtGraphics = self.attachAsset(assetType, { anchorX: 0.5, anchorY: 0.5 }); self.health = type === 'hard' ? 3 : 1; self.originalHealth = self.health; self.hasGem = gemType > 0; self.gemType = gemType; // Reveal gem if this dirt has one if (self.hasGem) { var gemAsset = 'gem' + gemType; var gemGraphics = self.attachAsset(gemAsset, { anchorX: 0.5, anchorY: 0.5, alpha: 0 // Hidden initially }); self.gem = gemGraphics; } self.dig = function (strength) { self.health -= strength; if (self.health <= 0) { // Dirt is completely dug dirtGraphics.alpha = 0; // Reveal gem if exists if (self.hasGem && self.gem) { self.gem.alpha = 1; return true; } } else { // Visual feedback that dirt is being dug dirtGraphics.alpha = self.health / self.originalHealth; } return false; }; self.collectGem = function () { if (self.hasGem && self.gem && self.gem.alpha > 0) { self.gem.alpha = 0; return self.gemType; } return 0; }; return self; }); var GrassTile = Container.expand(function () { var self = Container.call(this); var grassGraphics = self.attachAsset('Grass', { anchorX: 0.5, anchorY: 0.5 }); // Grass tiles can't be dug self.dig = function () { return false; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Game constants var GRID_WIDTH = 6; var GRID_HEIGHT = 8; var CELL_SIZE = 120; var GRID_PADDING_X = (2048 - GRID_WIDTH * CELL_SIZE) / 2; var GRID_PADDING_Y = 500; // Game variables var digger; var grid = []; var score = 0; var level = 1; var coinsCollected = 0; var pickLevel = storage.pickLevel || 1; var coins = storage.coins || 0; // UI elements var scoreTxt; var levelTxt; var coinsTxt; var upgradeBtn; // Initialize game function initGame() { game.setBackgroundColor(0x333333); // Create header UI createUI(); // Create digger digger = new Digger(); digger.x = 2048 / 2; digger.y = 300; digger.upgrade(pickLevel); game.addChild(digger); // Create initial grid createGrid(); // Play background music LK.playMusic('bgmusic'); } function createUI() { // Score text scoreTxt = new Text2('Score: 0', { size: 60, fill: 0xFFFFFF }); scoreTxt.anchor.set(0, 0); scoreTxt.x = 150; scoreTxt.y = 50; LK.gui.addChild(scoreTxt); // Level text levelTxt = new Text2('Level: 1', { size: 60, fill: 0xFFFFFF }); levelTxt.anchor.set(0.5, 0); levelTxt.x = 2048 / 2; levelTxt.y = 50; LK.gui.addChild(levelTxt); // Coins text coinsTxt = new Text2('Coins: ' + coins, { size: 60, fill: 0xFFFF00 }); coinsTxt.anchor.set(1, 0); coinsTxt.x = 2048 - 150; coinsTxt.y = 50; LK.gui.addChild(coinsTxt); // Upgrade button var buttonText = 'Upgrade Pick: ' + pickLevel * 50 + ' coins'; upgradeBtn = new Text2(buttonText, { size: 50, fill: 0x00FF00 }); upgradeBtn.anchor.set(0.5, 0); upgradeBtn.x = 2048 / 2; upgradeBtn.y = 150; upgradeBtn.interactive = true; upgradeBtn.down = function () { upgradePick(); }; LK.gui.addChild(upgradeBtn); } function createGrid() { // Clear existing grid if any for (var i = 0; i < grid.length; i++) { for (var j = 0; j < grid[i].length; j++) { if (grid[i][j]) { grid[i][j].destroy(); } } } // Create new grid grid = []; for (var row = 0; row < GRID_HEIGHT; row++) { grid[row] = []; for (var col = 0; col < GRID_WIDTH; col++) { // For the top row (level one), use grass instead of dirt if (row === 0) { // Add grass tile var grassTile = new Container(); var grassGraphics = grassTile.attachAsset('Grass', { anchorX: 0.5, anchorY: 0.5 }); grassTile.x = GRID_PADDING_X + col * CELL_SIZE + CELL_SIZE / 2; grassTile.y = GRID_PADDING_Y + row * CELL_SIZE + CELL_SIZE / 2; game.addChild(grassTile); // Still create the dirt object but make it have no gems var dirt = new Dirt('normal', 0); dirt.x = GRID_PADDING_X + col * CELL_SIZE + CELL_SIZE / 2; dirt.y = GRID_PADDING_Y + row * CELL_SIZE + CELL_SIZE / 2; dirt.health = 0; // Already dug out dirt.dig = function () { return false; }; // Can't dig grass grid[row][col] = dirt; continue; } // Regular dirt tiles for all other rows // Determine dirt type - more hard dirt in higher levels var isHard = Math.random() < 0.1 * level; var dirtType = isHard ? 'hard' : 'normal'; // Determine if this cell has a gem - more valuable gems in deeper rows var gemChance = 0.2 + row / GRID_HEIGHT * 0.3; var hasGem = Math.random() < gemChance; var gemType = 0; if (hasGem) { // Gems range from 1-3, with better gems having a higher chance in deeper levels var gemRoll = Math.random(); if (gemRoll < 0.6 - level * 0.05) { gemType = 1; // Common gem } else if (gemRoll < 0.9 - level * 0.03) { gemType = 2; // Uncommon gem } else { gemType = 3; // Rare gem } } var dirt = new Dirt(dirtType, gemType); dirt.x = GRID_PADDING_X + col * CELL_SIZE + CELL_SIZE / 2; dirt.y = GRID_PADDING_Y + row * CELL_SIZE + CELL_SIZE / 2; grid[row][col] = dirt; game.addChild(dirt); } } } function attemptDig(x, y) { if (digger.digging) { return; } // Find the grid cell that was clicked for (var row = 0; row < grid.length; row++) { for (var col = 0; col < grid[row].length; col++) { var cell = grid[row][col]; // Check if click is within this cell if (Math.abs(x - cell.x) < CELL_SIZE / 2 && Math.abs(y - cell.y) < CELL_SIZE / 2) { // Move digger to this cell digger.startDig(cell.x, cell.y); // Play dig sound LK.getSound('dig').play(); // Attempt to dig the dirt var gemRevealed = cell.dig(digger.digStrength); if (gemRevealed) { // Check if all gems have been collected to advance level checkLevelComplete(); } return; } } } } function collectGem(x, y) { // Find the grid cell that was clicked for (var row = 0; row < grid.length; row++) { for (var col = 0; col < grid[row].length; col++) { var cell = grid[row][col]; // Check if click is within this cell if (Math.abs(x - cell.x) < CELL_SIZE / 2 && Math.abs(y - cell.y) < CELL_SIZE / 2) { // Try to collect gem var gemType = cell.collectGem(); if (gemType > 0) { // Play collect sound LK.getSound('collect').play(); // Award points based on gem type var points = gemType * gemType * 10; score += points; // Award coins coinsCollected += gemType; coins += gemType; // Update score display updateUI(); // Check if all gems have been collected checkLevelComplete(); return true; } } } } return false; } function checkLevelComplete() { // Count remaining gems var gemsLeft = 0; for (var row = 0; row < grid.length; row++) { for (var col = 0; col < grid[row].length; col++) { var cell = grid[row][col]; if (cell.hasGem && cell.gem && cell.gem.alpha > 0) { gemsLeft++; } } } // If no gems left, advance to next level if (gemsLeft === 0) { level++; levelTxt.setText('Level: ' + level); // Save progress saveGame(); // Create new grid for next level createGrid(); } } function upgradePick() { var upgradeCost = pickLevel * 50; if (coins >= upgradeCost) { coins -= upgradeCost; pickLevel++; digger.upgrade(pickLevel); // Update UI upgradeBtn.setText('Upgrade Pick: ' + pickLevel * 50 + ' coins'); coinsTxt.setText('Coins: ' + coins); // Save upgrade saveGame(); } } function updateUI() { scoreTxt.setText('Score: ' + score); coinsTxt.setText('Coins: ' + coins); } function saveGame() { storage.coins = coins; storage.pickLevel = pickLevel; } // Game event handlers game.down = function (x, y, obj) { // If player clicks on a gem, collect it if (!collectGem(x, y)) { // Otherwise try to dig attemptDig(x, y); } }; // Initialize the game initGame();
===================================================================
--- original.js
+++ change.js
@@ -87,8 +87,20 @@
return 0;
};
return self;
});
+var GrassTile = Container.expand(function () {
+ var self = Container.call(this);
+ var grassGraphics = self.attachAsset('Grass', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Grass tiles can't be dug
+ self.dig = function () {
+ return false;
+ };
+ return self;
+});
/****
* Initialize Game
****/
@@ -190,8 +202,31 @@
grid = [];
for (var row = 0; row < GRID_HEIGHT; row++) {
grid[row] = [];
for (var col = 0; col < GRID_WIDTH; col++) {
+ // For the top row (level one), use grass instead of dirt
+ if (row === 0) {
+ // Add grass tile
+ var grassTile = new Container();
+ var grassGraphics = grassTile.attachAsset('Grass', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ grassTile.x = GRID_PADDING_X + col * CELL_SIZE + CELL_SIZE / 2;
+ grassTile.y = GRID_PADDING_Y + row * CELL_SIZE + CELL_SIZE / 2;
+ game.addChild(grassTile);
+ // Still create the dirt object but make it have no gems
+ var dirt = new Dirt('normal', 0);
+ dirt.x = GRID_PADDING_X + col * CELL_SIZE + CELL_SIZE / 2;
+ dirt.y = GRID_PADDING_Y + row * CELL_SIZE + CELL_SIZE / 2;
+ dirt.health = 0; // Already dug out
+ dirt.dig = function () {
+ return false;
+ }; // Can't dig grass
+ grid[row][col] = dirt;
+ continue;
+ }
+ // Regular dirt tiles for all other rows
// Determine dirt type - more hard dirt in higher levels
var isHard = Math.random() < 0.1 * level;
var dirtType = isHard ? 'hard' : 'normal';
// Determine if this cell has a gem - more valuable gems in deeper rows