User prompt
Keep the blocks instead of boneblock and if you press more it will show and the other blocks will hide
User prompt
Add boneblock
User prompt
Add a button that will show more block options
User prompt
Add Jackolanta
User prompt
Add punkin
User prompt
Add snowblock snowgrassblock and iceblock
User prompt
i added cacti
User prompt
can you make then button smaller
User prompt
I added log and leaf
User prompt
I added water and lava
User prompt
I added sand
Code edit (1 edits merged)
Please save this source code
User prompt
Block Builder World
Initial prompt
Block build. With 3 blocks that you can choose you can make a world. The three blocks are stone dirt and grass
/****
* Classes
****/
var Block = Container.expand(function (blockType) {
var self = Container.call(this);
self.blockType = blockType || 'stone';
var assetName;
if (self.blockType === 'water') {
assetName = 'Water';
} else if (self.blockType === 'lava') {
assetName = 'Lava';
} else if (self.blockType === 'log') {
assetName = 'Log';
} else if (self.blockType === 'leaf') {
assetName = 'Leaf';
} else if (self.blockType === 'cacti') {
assetName = 'cacti';
} else if (self.blockType === 'snowblock') {
assetName = 'Snowblock';
} else if (self.blockType === 'snowgrassblock') {
assetName = 'Grasssnow';
} else if (self.blockType === 'iceblock') {
assetName = 'Iceblock';
} else if (self.blockType === 'punkin') {
assetName = 'Punkin';
} else if (self.blockType === 'jackolanta') {
assetName = 'Jackolanta';
} else {
assetName = self.blockType + 'Block';
}
var blockGraphics = self.attachAsset(assetName, {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var BlockButton = Container.expand(function (blockType) {
var self = Container.call(this);
self.blockType = blockType;
self.isSelected = false;
self.buttonBg = self.attachAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5
});
self.selectedBg = self.attachAsset('selectedButtonBg', {
anchorX: 0.5,
anchorY: 0.5
});
self.selectedBg.visible = false;
var blockPreview = new Block(blockType);
blockPreview.scale.set(0.6);
self.addChild(blockPreview);
self.setSelected = function (selected) {
self.isSelected = selected;
self.buttonBg.visible = !selected;
self.selectedBg.visible = selected;
};
self.down = function (x, y, obj) {
selectBlockType(self.blockType);
};
return self;
});
var GridCell = Container.expand(function (gridX, gridY) {
var self = Container.call(this);
self.gridX = gridX;
self.gridY = gridY;
self.hasBlock = false;
self.block = null;
var cellBorder = self.attachAsset('gridCell', {
anchorX: 0.5,
anchorY: 0.5
});
cellBorder.alpha = 0.1;
self.placeBlock = function (blockType) {
if (!self.hasBlock) {
self.block = new Block(blockType);
self.addChild(self.block);
self.hasBlock = true;
LK.getSound('placeBlock').play();
}
};
self.removeBlock = function () {
if (self.hasBlock && self.block) {
self.removeChild(self.block);
self.block = null;
self.hasBlock = false;
LK.getSound('removeBlock').play();
}
};
self.down = function (x, y, obj) {
if (self.hasBlock) {
self.removeBlock();
} else {
self.placeBlock(selectedBlockType);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var GRID_SIZE = 120;
var GRID_COLS = 16;
var GRID_ROWS = 18;
var GRID_START_X = (2048 - GRID_COLS * GRID_SIZE) / 2;
var GRID_START_Y = 100;
var selectedBlockType = 'stone';
var showMoreBlocks = false;
var grid = [];
var blockButtons = {};
// Create grid
for (var row = 0; row < GRID_ROWS; row++) {
grid[row] = [];
for (var col = 0; col < GRID_COLS; col++) {
var cell = new GridCell(col, row);
cell.x = GRID_START_X + col * GRID_SIZE + GRID_SIZE / 2;
cell.y = GRID_START_Y + row * GRID_SIZE + GRID_SIZE / 2;
grid[row][col] = cell;
game.addChild(cell);
}
}
// Create block selection buttons
var buttonContainer = new Container();
buttonContainer.x = 2048 / 2;
buttonContainer.y = 2732 - 100;
game.addChild(buttonContainer);
var basicBlockTypes = ['stone', 'dirt', 'grass', 'sand', 'water', 'lava'];
var additionalBlockTypes = ['log', 'leaf', 'cacti', 'snowblock', 'snowgrassblock', 'iceblock', 'punkin', 'jackolanta'];
var currentBlockTypes = basicBlockTypes;
// Create more blocks button
var moreButton = new Container();
var moreBg = moreButton.attachAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5
});
var moreText = new Text2('MORE', {
size: 32,
fill: 0xFFFFFF
});
moreText.anchor.set(0.5, 0.5);
moreButton.addChild(moreText);
moreButton.down = function (x, y, obj) {
toggleMoreBlocks();
};
buttonContainer.addChild(moreButton);
function createBlockButtons() {
// Clear existing buttons except more button
for (var type in blockButtons) {
if (blockButtons[type].parent) {
buttonContainer.removeChild(blockButtons[type]);
}
}
blockButtons = {};
var buttonSpacing = 140;
var totalWidth = currentBlockTypes.length * buttonSpacing; // Include space for more button
var startX = -totalWidth / 2;
for (var i = 0; i < currentBlockTypes.length; i++) {
var blockType = currentBlockTypes[i];
var button = new BlockButton(blockType);
button.x = startX + i * buttonSpacing;
blockButtons[blockType] = button;
buttonContainer.addChild(button);
}
// Position more button at the end
moreButton.x = startX + currentBlockTypes.length * buttonSpacing;
}
createBlockButtons();
// Create clear button
var clearButton = new Container();
var clearBg = clearButton.attachAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1.2
});
var clearText = new Text2('CLEAR', {
size: 40,
fill: 0xFFFFFF
});
clearText.anchor.set(0.5, 0.5);
clearButton.addChild(clearText);
clearButton.x = 2048 / 2;
clearButton.y = 2732 - 250;
game.addChild(clearButton);
clearButton.down = function (x, y, obj) {
clearGrid();
};
function toggleMoreBlocks() {
showMoreBlocks = !showMoreBlocks;
if (showMoreBlocks) {
currentBlockTypes = basicBlockTypes.concat(additionalBlockTypes);
} else {
currentBlockTypes = basicBlockTypes;
// If selected block is not in basic types, switch to stone
var isSelectedInBasic = false;
for (var i = 0; i < basicBlockTypes.length; i++) {
if (basicBlockTypes[i] === selectedBlockType) {
isSelectedInBasic = true;
break;
}
}
if (!isSelectedInBasic) {
selectedBlockType = 'stone';
}
}
createBlockButtons();
selectBlockType(selectedBlockType);
}
function selectBlockType(blockType) {
selectedBlockType = blockType;
for (var type in blockButtons) {
blockButtons[type].setSelected(type === blockType);
}
}
function clearGrid() {
for (var row = 0; row < GRID_ROWS; row++) {
for (var col = 0; col < GRID_COLS; col++) {
if (grid[row][col].hasBlock) {
grid[row][col].removeBlock();
}
}
}
}
;
// Initialize with stone selected
selectBlockType('stone');
game.update = function () {
// Game runs continuously for creative building
}; ===================================================================
--- original.js
+++ change.js
@@ -112,8 +112,9 @@
var GRID_ROWS = 18;
var GRID_START_X = (2048 - GRID_COLS * GRID_SIZE) / 2;
var GRID_START_Y = 100;
var selectedBlockType = 'stone';
+var showMoreBlocks = false;
var grid = [];
var blockButtons = {};
// Create grid
for (var row = 0; row < GRID_ROWS; row++) {
@@ -130,19 +131,49 @@
var buttonContainer = new Container();
buttonContainer.x = 2048 / 2;
buttonContainer.y = 2732 - 100;
game.addChild(buttonContainer);
-var blockTypes = ['stone', 'dirt', 'grass', 'sand', 'water', 'lava', 'log', 'leaf', 'cacti', 'snowblock', 'snowgrassblock', 'iceblock', 'punkin', 'jackolanta'];
-var buttonSpacing = 140;
-var totalWidth = (blockTypes.length - 1) * buttonSpacing;
-var startX = -totalWidth / 2;
-for (var i = 0; i < blockTypes.length; i++) {
- var blockType = blockTypes[i];
- var button = new BlockButton(blockType);
- button.x = startX + i * buttonSpacing;
- blockButtons[blockType] = button;
- buttonContainer.addChild(button);
+var basicBlockTypes = ['stone', 'dirt', 'grass', 'sand', 'water', 'lava'];
+var additionalBlockTypes = ['log', 'leaf', 'cacti', 'snowblock', 'snowgrassblock', 'iceblock', 'punkin', 'jackolanta'];
+var currentBlockTypes = basicBlockTypes;
+// Create more blocks button
+var moreButton = new Container();
+var moreBg = moreButton.attachAsset('buttonBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+});
+var moreText = new Text2('MORE', {
+ size: 32,
+ fill: 0xFFFFFF
+});
+moreText.anchor.set(0.5, 0.5);
+moreButton.addChild(moreText);
+moreButton.down = function (x, y, obj) {
+ toggleMoreBlocks();
+};
+buttonContainer.addChild(moreButton);
+function createBlockButtons() {
+ // Clear existing buttons except more button
+ for (var type in blockButtons) {
+ if (blockButtons[type].parent) {
+ buttonContainer.removeChild(blockButtons[type]);
+ }
+ }
+ blockButtons = {};
+ var buttonSpacing = 140;
+ var totalWidth = currentBlockTypes.length * buttonSpacing; // Include space for more button
+ var startX = -totalWidth / 2;
+ for (var i = 0; i < currentBlockTypes.length; i++) {
+ var blockType = currentBlockTypes[i];
+ var button = new BlockButton(blockType);
+ button.x = startX + i * buttonSpacing;
+ blockButtons[blockType] = button;
+ buttonContainer.addChild(button);
+ }
+ // Position more button at the end
+ moreButton.x = startX + currentBlockTypes.length * buttonSpacing;
}
+createBlockButtons();
// Create clear button
var clearButton = new Container();
var clearBg = clearButton.attachAsset('buttonBg', {
anchorX: 0.5,
@@ -160,8 +191,29 @@
game.addChild(clearButton);
clearButton.down = function (x, y, obj) {
clearGrid();
};
+function toggleMoreBlocks() {
+ showMoreBlocks = !showMoreBlocks;
+ if (showMoreBlocks) {
+ currentBlockTypes = basicBlockTypes.concat(additionalBlockTypes);
+ } else {
+ currentBlockTypes = basicBlockTypes;
+ // If selected block is not in basic types, switch to stone
+ var isSelectedInBasic = false;
+ for (var i = 0; i < basicBlockTypes.length; i++) {
+ if (basicBlockTypes[i] === selectedBlockType) {
+ isSelectedInBasic = true;
+ break;
+ }
+ }
+ if (!isSelectedInBasic) {
+ selectedBlockType = 'stone';
+ }
+ }
+ createBlockButtons();
+ selectBlockType(selectedBlockType);
+}
function selectBlockType(blockType) {
selectedBlockType = blockType;
for (var type in blockButtons) {
blockButtons[type].setSelected(type === blockType);