User prompt
try again
User prompt
remove the gravity
User prompt
fix teh character controls
User prompt
add pixel world's world system
User prompt
add full dirt to worlds
User prompt
add buttons to the screen
User prompt
add control buttons left right and up
User prompt
add control buttons
User prompt
fix character control
User prompt
make a platform for the character
User prompt
fix the character when entering world fall down
User prompt
make a main menu button
User prompt
Please fix the bug: 'Uncaught TypeError: slot.updateCount is not a function' in or related to this line: 'slot.updateCount(inventory[slot.type] || 0);' Line Number: 971
User prompt
Please fix the bug: 'Uncaught TypeError: slot.updateCount is not a function' in or related to this line: 'slot.updateCount(inventory[slot.type] || 0);' Line Number: 971
User prompt
make character can controlled and add inventory ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
add character to the inside the world and add a main menu button
User prompt
Please fix the bug: 'LK.stringifyJSON is not a function' in or related to this line: 'return LK.stringifyJSON(obj);' Line Number: 516 ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'stringify')' in or related to this line: 'storage.worldData = JSON.stringify(data);' Line Number: 723
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'stringify')' in or related to this line: 'storage.worldData = JSON.stringify(data);' Line Number: 723
User prompt
fix line 723
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'stringify')' in or related to this line: 'storage.worldData = JSON.stringify(data);' Line Number: 723 ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'parse')' in or related to this line: 'var worldData = storage.worldData ? JSON.parse(storage.worldData) : generateWorld();' Line Number: 667
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'storage.worldData = data;' Line Number: 716 ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
fix the bugs
User prompt
make create world available
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
worldData: "undefined",
playerPosition: {
x: 1024,
y: 1600
},
inventory: {}
});
/****
* Classes
****/
var Block = Container.expand(function (type, size) {
var self = Container.call(this);
self.type = type;
self.size = size || 64;
var graphics = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5,
width: self.size,
height: self.size
});
self.down = function (x, y, obj) {
if (currentTool === 'break') {
self.breakBlock();
}
};
self.breakBlock = function () {
// Add to inventory
inventory[self.type] = (inventory[self.type] || 0) + 1;
updateInventoryDisplay();
// Play break sound
LK.getSound('break').play();
// Remove from world
var gridX = Math.floor(self.x / blockSize);
var gridY = Math.floor(self.y / blockSize);
if (worldData[gridY] && worldData[gridY][gridX] !== undefined && worldData[gridY][gridX] !== null) {
worldData[gridY][gridX] = null;
self.destroy();
}
// Save world data
// Convert worldData to string before storing
storage.worldData = JSON.stringify(worldData);
};
return self;
});
var Button = Container.expand(function (text, callback) {
var self = Container.call(this);
var background = self.attachAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5
});
self.buttonText = new Text2(text, {
size: 36,
fill: 0xFFFFFF
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.callback = callback;
self.down = function (x, y, obj) {
if (self.callback) {
self.callback();
}
};
return self;
});
var Character = Container.expand(function () {
var self = Container.call(this);
// Create character sprite
var sprite = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0,
width: 80,
height: 100
});
// Character properties
self.direction = 1; // 1 = right, -1 = left
self.isMoving = false;
self.speed = 4;
// Animation properties
self.frameTime = 0;
self.frameRate = 8; // frames per second
// Change direction method
self.faceDirection = function (dir) {
if (dir !== self.direction) {
self.direction = dir;
sprite.scale.x = self.direction;
}
};
// Start/stop movement
self.startMoving = function (dir) {
self.isMoving = true;
self.faceDirection(dir);
};
self.stopMoving = function () {
self.isMoving = false;
};
// Update method - called every frame
self.update = function () {
// Store last position for collision detection
var lastY = self.y;
var lastX = self.x;
// Handle movement
if (self.isMoving) {
self.x += self.speed * self.direction;
// Update animation
self.frameTime += 1 / 60;
if (self.frameTime >= 1 / self.frameRate) {
self.frameTime = 0;
// We would change animation frame here if we had sprite frames
}
}
// Keep character in bounds
if (self.x < 50) self.x = 50;
if (self.x > 2000) self.x = 2000;
// Apply gravity and check for ground collision
var gridX = Math.floor(self.x / blockSize);
var gridY = Math.floor((self.y + 10) / blockSize);
// Check if there's ground beneath the character
var onGround = false;
// Check multiple positions to detect standing on platforms
for (var checkX = gridX - 1; checkX <= gridX + 1; checkX++) {
// Check directly below the character
if (checkX >= 0 && gridY >= 0 && gridY < worldData.length && checkX < (worldData[gridY] ? worldData[gridY].length : 0) && worldData[gridY] && worldData[gridY][checkX] && worldData[gridY][checkX] !== 'water') {
// Distance from character center to the block
var distX = Math.abs(self.x - (checkX * blockSize + blockSize / 2));
// If character is close enough horizontally to be standing on the block
if (distX < blockSize * 0.8) {
// Position character on top of the block
self.y = gridY * blockSize - 10;
onGround = true;
break;
}
}
}
if (!onGround) {
// Apply gravity if no ground below
self.y += gravity;
}
// Check if we just landed on a platform
if (lastY < self.y && self.y - lastY < gravity * 1.5) {
// We just landed, stop any vertical momentum
self.y = Math.floor(self.y / blockSize) * blockSize - 10;
}
// Check for horizontal collisions
if (lastX !== self.x) {
var checkGridY = Math.floor((self.y - 50) / blockSize); // Check at character's mid height
var newGridX = Math.floor(self.x / blockSize);
// Check if there's a block at character's new position
if (checkGridY >= 0 && checkGridY < worldData.length && newGridX >= 0 && newGridX < worldData[checkGridY].length && worldData[checkGridY][newGridX]) {
// Collision detected, revert to previous position
self.x = lastX;
self.isMoving = false;
}
}
};
// Interactive events
self.down = function (x, y, obj) {
// Handle touch down on character
self.startDrag();
// Convert to world container's coordinate system for proper positioning
var worldX = obj.parent.toGlobal(obj.position).x;
var worldY = obj.parent.toGlobal(obj.position).y;
// Determine initial direction based on touch position
if (x > self.width / 2) {
self.faceDirection(1); // Face right
} else {
self.faceDirection(-1); // Face left
}
};
// Add touch controls for character
self.startDrag = function () {
self.isDragging = true;
};
self.stopDrag = function () {
self.isDragging = false;
self.isMoving = false;
};
// Add jump functionality
self.jump = function () {
// Find where the player is standing
var gridX = Math.floor(self.x / blockSize);
var gridY = Math.floor((self.y + 10) / blockSize);
// Check if character is on ground
var onGround = false;
for (var checkX = gridX - 1; checkX <= gridX + 1; checkX++) {
if (checkX >= 0 && gridY >= 0 && gridY < worldData.length && checkX < (worldData[gridY] ? worldData[gridY].length : 0) && worldData[gridY] && worldData[gridY][checkX] && worldData[gridY][checkX] !== 'water') {
var distX = Math.abs(self.x - (checkX * blockSize + blockSize / 2));
if (distX < blockSize * 0.8) {
onGround = true;
break;
}
}
}
if (onGround) {
// Apply upward force when jumping
self.y -= 15;
}
};
self.drag = function (x, y) {
if (self.isDragging) {
// Convert global coordinates to character's local frame
var localX = x - game.toLocal(self.parent.toGlobal(self.position)).x + self.x;
// Calculate difference for movement threshold
var diffX = localX - self.x;
// Determine direction based on current position and touch position
if (diffX > 10) {
self.faceDirection(1);
self.isMoving = true;
} else if (diffX < -10) {
self.faceDirection(-1);
self.isMoving = true;
} else {
self.isMoving = false;
}
}
};
return self;
});
var ControlButton = Container.expand(function (text, callback, width, height) {
var self = Container.call(this);
width = width || 150;
height = height || 150;
// Create button background
var background = self.attachAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5,
width: width,
height: height
});
// Create button text
self.buttonText = new Text2(text, {
size: 48,
fill: 0xFFFFFF
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
// Store callback function
self.callback = callback;
// Add control type to identify button function
self.controlType = '';
if (text === '←') self.controlType = 'left';else if (text === '→') self.controlType = 'right';else if (text === '↑') self.controlType = 'jump';else if (text === '■') self.controlType = 'stop';
// Button interaction
self.down = function (x, y, obj) {
if (self.callback) {
background.alpha = 0.7;
self.callback();
// Set control state for continuous button press
if (self.controlType && controlState) {
if (self.controlType === 'left') {
controlState.left = true;
controlState.right = false;
} else if (self.controlType === 'right') {
controlState.right = true;
controlState.left = false;
} else if (self.controlType === 'jump') {
controlState.jump = true;
} else if (self.controlType === 'stop') {
controlState.left = false;
controlState.right = false;
}
}
}
};
self.up = function (x, y, obj) {
background.alpha = 1.0;
// Clear control state when button is released
if (self.controlType && controlState) {
if (self.controlType === 'left') {
controlState.left = false;
} else if (self.controlType === 'right') {
controlState.right = false;
} else if (self.controlType === 'jump') {
controlState.jump = false;
}
}
};
return self;
});
var InventorySlot = Container.expand(function (index, type) {
var self = Container.call(this);
self.index = index;
self.type = type;
var background = self.attachAsset('inventorySlot', {
anchorX: 0.5,
anchorY: 0.5
});
if (type) {
var blockIcon = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5,
width: 60,
height: 60
});
}
self.countText = new Text2("0", {
size: 24,
fill: 0xFFFFFF
});
self.countText.anchor.set(1, 1);
self.countText.x = 30;
self.countText.y = 30;
self.addChild(self.countText);
self.updateCount = function (count) {
self.countText.setText(count.toString());
};
self.down = function (x, y, obj) {
selectInventorySlot(self.index);
};
return self;
});
var MainMenu = Container.expand(function () {
var self = Container.call(this);
// Create a semi-transparent background overlay
var overlay = LK.getAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5,
width: 2048,
height: 2732,
alpha: 0.7
});
overlay.tint = 0x000000;
self.addChild(overlay);
// Game title
self.title = new Text2("Pixel Realms", {
size: 120,
fill: 0xFFFFFF
});
self.title.anchor.set(0.5, 0.5);
self.title.x = 2048 / 2;
self.title.y = 800;
self.addChild(self.title);
// Start game button
self.startButton = new Button("Start Game", function () {
self.hide();
worldEnterMenu.show();
});
self.startButton.x = 2048 / 2;
self.startButton.y = 1200;
self.addChild(self.startButton);
// Add animated elements
self.show = function () {
self.visible = true;
// Reset positions for animation
self.title.y = 600;
self.title.alpha = 0;
self.startButton.y = 1300;
self.startButton.alpha = 0;
// Animate title
tween(self.title, {
y: 800,
alpha: 1
}, {
duration: 800,
easing: tween.easeOutBack
});
// Animate button with slight delay
tween(self.startButton, {
y: 1200,
alpha: 1
}, {
duration: 800,
easing: tween.easeOutBack
});
};
self.hide = function () {
// Animate out
tween(self.title, {
y: 600,
alpha: 0
}, {
duration: 500,
easing: tween.easeInBack
});
tween(self.startButton, {
y: 1300,
alpha: 0
}, {
duration: 500,
easing: tween.easeInBack,
onFinish: function onFinish() {
self.visible = false;
}
});
};
return self;
});
var Platform = Container.expand(function (width, height) {
var self = Container.call(this);
self.width = width || 5;
self.height = height || 1;
// Create platform blocks
for (var x = 0; x < self.width; x++) {
for (var y = 0; y < self.height; y++) {
var block = new Block('stone', blockSize);
block.x = x * blockSize + blockSize / 2;
block.y = y * blockSize + blockSize / 2;
self.addChild(block);
}
}
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1,
width: 60,
height: 120
});
// Movement properties
self.speed = 5;
self.velocity = {
x: 0,
y: 0
};
self.gravity = 0.5;
self.jumping = false;
self.grounded = false;
self.move = function (direction) {
if (direction === 'left') {
self.velocity.x = -self.speed;
} else if (direction === 'right') {
self.velocity.x = self.speed;
}
};
self.stopMove = function () {
self.velocity.x = 0;
};
self.jump = function () {
if (self.grounded) {
self.velocity.y = -12;
self.grounded = false;
self.jumping = true;
}
};
self.update = function () {
// Apply gravity
if (!self.grounded) {
self.velocity.y += self.gravity;
}
// Update position
self.x += self.velocity.x;
self.y += self.velocity.y;
// Check for ground collision
var gridX = Math.floor(self.x / blockSize);
var gridY = Math.floor((self.y + 10) / blockSize);
// Simple collision check with the block below
if (gridY >= 0 && gridY < worldData.length && gridX >= 0 && worldData[gridY] && gridX < worldData[gridY].length && worldData[gridY][gridX] && worldData[gridY][gridX] !== 'water') {
if (self.velocity.y > 0) {
self.y = gridY * blockSize - 10;
self.velocity.y = 0;
self.grounded = true;
self.jumping = false;
}
} else {
self.grounded = false;
}
// Update camera to follow player
updateCamera();
// Save player position periodically
if (LK.ticks % 60 === 0) {
storage.playerPosition = {
x: self.x,
y: self.y
};
}
};
return self;
});
var WorldEnterMenu = Container.expand(function () {
var self = Container.call(this);
// Create a semi-transparent background
var overlay = LK.getAsset('buttonBg', {
anchorX: 0.5,
anchorY: 0.5,
width: 2048,
height: 2732,
alpha: 0.8
});
overlay.tint = 0x000022;
self.addChild(overlay);
// World title
self.title = new Text2("Enter World", {
size: 100,
fill: 0xFFFFFF
});
self.title.anchor.set(0.5, 0.5);
self.title.x = 2048 / 2;
self.title.y = 600;
self.addChild(self.title);
// World description
self.description = new Text2("Select your world to start building", {
size: 48,
fill: 0xCCCCCC
});
self.description.anchor.set(0.5, 0.5);
self.description.x = 2048 / 2;
self.description.y = 720;
self.addChild(self.description);
// World selection buttons
self.createWorldButton = new Button("Create New World", function () {
// Generate new world
worldData = generateWorld();
// Reset player position
player.x = 1024;
player.y = 1600;
storage.playerPosition = {
x: player.x,
y: player.y
};
// Reset inventory
inventory = {
"grass": 10,
"stone": 10,
"wood": 10,
"water": 5
};
storage.inventory = inventory;
// Start game
self.hide();
gameStarted = true;
initializeWorld();
setupUI();
});
self.createWorldButton.x = 2048 / 2;
self.createWorldButton.y = 1000;
self.addChild(self.createWorldButton);
self.loadWorldButton = new Button("Continue World", function () {
// Load existing world (already loaded from storage)
self.hide();
gameStarted = true;
initializeWorld();
setupUI();
});
self.loadWorldButton.x = 2048 / 2;
self.loadWorldButton.y = 1150;
self.addChild(self.loadWorldButton);
self.backButton = new Button("Back to Menu", function () {
self.hide();
mainMenu.show();
});
self.backButton.x = 2048 / 2;
self.backButton.y = 1400;
self.addChild(self.backButton);
// Show animation
self.show = function () {
self.visible = true;
// Reset positions for animation
self.title.y = 400;
self.title.alpha = 0;
self.description.y = 620;
self.description.alpha = 0;
self.createWorldButton.y = 1100;
self.createWorldButton.alpha = 0;
self.loadWorldButton.y = 1250;
self.loadWorldButton.alpha = 0;
self.backButton.y = 1500;
self.backButton.alpha = 0;
// Animate title
tween(self.title, {
y: 600,
alpha: 1
}, {
duration: 600,
easing: tween.easeOutBack
});
// Animate description
tween(self.description, {
y: 720,
alpha: 1
}, {
duration: 600,
delay: 100,
easing: tween.easeOutBack
});
// Animate buttons
tween(self.createWorldButton, {
y: 1000,
alpha: 1
}, {
duration: 600,
delay: 200,
easing: tween.easeOutBack
});
tween(self.loadWorldButton, {
y: 1150,
alpha: 1
}, {
duration: 600,
delay: 300,
easing: tween.easeOutBack
});
tween(self.backButton, {
y: 1400,
alpha: 1
}, {
duration: 600,
delay: 400,
easing: tween.easeOutBack
});
};
// Hide animation
self.hide = function () {
// Animate out
tween(self.title, {
y: 400,
alpha: 0
}, {
duration: 400,
easing: tween.easeInBack
});
tween(self.description, {
y: 620,
alpha: 0
}, {
duration: 400,
easing: tween.easeInBack
});
tween(self.createWorldButton, {
y: 1100,
alpha: 0
}, {
duration: 400,
easing: tween.easeInBack
});
tween(self.loadWorldButton, {
y: 1250,
alpha: 0
}, {
duration: 400,
easing: tween.easeInBack
});
tween(self.backButton, {
y: 1500,
alpha: 0
}, {
duration: 400,
easing: tween.easeInBack,
onFinish: function onFinish() {
self.visible = false;
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Make sure JSON is defined for storage operations
// Game constants
function _typeof(o) {
"@babel/helpers - typeof";
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
return typeof o;
} : function (o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
}, _typeof(o);
}
var JSON = {
parse: function parse(text) {
return LK.parseJSON(text);
},
stringify: function stringify(obj) {
// Convert JavaScript object to a JSON string
var str = '';
if (obj === null) return 'null';
if (typeof obj === 'undefined') return undefined;
if (typeof obj === 'string') return '"' + obj.replace(/"/g, '\\"') + '"';
if (typeof obj === 'number' || typeof obj === 'boolean') return obj.toString();
if (Array.isArray(obj)) {
str = '[';
for (var i = 0; i < obj.length; i++) {
str += (i > 0 ? ',' : '') + JSON.stringify(obj[i]);
}
return str + ']';
}
if (_typeof(obj) === 'object') {
str = '{';
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
str += (i > 0 ? ',' : '') + '"' + key + '":' + JSON.stringify(obj[key]);
}
return str + '}';
}
return '';
}
};
var WorldGenerator = function WorldGenerator() {
// Generate a new world with more advanced terrain features
this.generate = function (width, height, blockSize) {
var data = [];
// Initialize with empty arrays
for (var y = 0; y < height; y++) {
data[y] = [];
for (var x = 0; x < width; x++) {
data[y][x] = null;
}
}
// Generate terrain using simple noise
var terrain = this.generateTerrain(width, height);
// Apply terrain to world data
for (var x = 0; x < width; x++) {
var groundHeight = Math.floor(terrain[x] * height * 0.4) + Math.floor(height * 0.5);
// Generate ground layers
for (var y = groundHeight; y < height; y++) {
if (y === groundHeight) {
data[y][x] = 'grass';
} else if (y < groundHeight + 3) {
data[y][x] = 'ground';
} else {
// More stones deeper underground
var stoneChance = Math.min(0.8, 0.2 + (y - groundHeight) * 0.05);
data[y][x] = Math.random() < stoneChance ? 'stone' : 'ground';
}
}
// Generate caves
this.generateCaves(data, x, groundHeight, height);
}
// Add trees
this.addTrees(data, width, height);
// Add water bodies
this.addWater(data, width, height);
return data;
};
// Generate basic terrain height map using simple noise
this.generateTerrain = function (width, height) {
var terrain = [];
var smoothness = 8; // Higher = smoother terrain
// Start with random height
terrain[0] = Math.random();
// Generate rest using midpoint displacement-like approach
for (var x = 1; x < width; x++) {
// Get previous height with some randomness
var prevHeight = terrain[x - 1];
var randomFactor = (Math.random() - 0.5) * 2 / smoothness;
// New height with constraints
terrain[x] = Math.max(0.1, Math.min(0.9, prevHeight + randomFactor));
// Add some hills/mountains occasionally
if (Math.random() < 0.05) {
var hillHeight = 0.1 + Math.random() * 0.2;
var hillWidth = 3 + Math.floor(Math.random() * 5);
// Create a hill centered at x
for (var h = 0; h < hillWidth && x + h < width; h++) {
var offset = hillHeight * Math.sin(h / hillWidth * Math.PI);
if (x + h < width) {
terrain[x + h] = Math.max(0.1, Math.min(0.9, terrain[x + h] - offset));
}
}
// Skip ahead
x += hillWidth;
}
}
return terrain;
};
// Add random caves under the terrain
this.generateCaves = function (data, x, groundHeight, height) {
// Random caves deeper underground
if (groundHeight + 5 < height) {
for (var c = 0; c < 2; c++) {
if (Math.random() < 0.05) {
var caveY = groundHeight + 5 + Math.floor(Math.random() * (height - groundHeight - 10));
var caveSize = 1 + Math.floor(Math.random() * 3);
for (var cy = caveY; cy < caveY + caveSize && cy < height; cy++) {
if (data[cy] && data[cy][x]) {
data[cy][x] = null;
}
}
}
}
}
};
// Add trees on the surface
this.addTrees = function (data, width, height) {
for (var x = 0; x < width; x++) {
// Find the ground level
var groundY = 0;
for (var y = 0; y < height; y++) {
if (data[y] && data[y][x] === 'grass') {
groundY = y;
break;
}
}
// Add random trees (if we found ground and not at the edge)
if (groundY > 0 && x > 2 && x < width - 2 && Math.random() < 0.1) {
var treeHeight = 3 + Math.floor(Math.random() * 3);
// Tree trunk
for (var ty = 1; ty <= treeHeight; ty++) {
if (groundY - ty >= 0) {
data[groundY - ty][x] = 'wood';
}
}
// Tree top (leaves would be added here if we had them)
var treeTop = groundY - treeHeight - 1;
if (treeTop >= 0) {
for (var tx = -1; tx <= 1; tx++) {
if (x + tx >= 0 && x + tx < width) {
data[treeTop][x + tx] = 'wood';
if (treeTop - 1 >= 0 && tx === 0) {
data[treeTop - 1][x] = 'wood';
}
}
}
}
}
}
};
// Add water bodies (lakes, rivers)
this.addWater = function (data, width, height) {
// Add some water pools/lakes
var lakeAttempts = Math.floor(width / 20);
for (var l = 0; l < lakeAttempts; l++) {
var lakeX = 5 + Math.floor(Math.random() * (width - 10));
var lakeWidth = 3 + Math.floor(Math.random() * 5);
// Find ground level at lakeX
var lakeY = 0;
for (var y = 0; y < height; y++) {
if (data[y] && data[y][lakeX] === 'grass') {
lakeY = y;
break;
}
}
// Create lake
if (lakeY > 0) {
for (var wx = 0; wx < lakeWidth && lakeX + wx < width; wx++) {
if (data[lakeY] && data[lakeY][lakeX + wx]) {
data[lakeY][lakeX + wx] = 'water';
// Sometimes make deeper water
if (Math.random() < 0.5 && lakeY + 1 < height) {
data[lakeY + 1][lakeX + wx] = 'water';
}
}
}
}
}
};
return this;
};
var blockSize = 64;
var worldWidth = 64;
var worldHeight = 32;
var gravity = 0.5;
// Game state
var gameStarted = false;
// Parse worldData from storage if it exists, checking for undefined/null values
var worldData;
try {
worldData = storage.worldData && storage.worldData !== "undefined" ? JSON.parse(storage.worldData) : generateWorld();
} catch (e) {
worldData = generateWorld();
}
var inventory = storage.inventory || {
"grass": 0,
"stone": 0,
"wood": 0,
"water": 0
};
var currentTool = 'place'; // 'place' or 'break'
var selectedBlockType = 'grass';
var selectedInventorySlot = 0;
// Game containers
var worldContainer = new Container();
var uiContainer = new Container();
var inventoryContainer = new Container();
var toolbarContainer = new Container();
game.addChild(worldContainer);
game.addChild(uiContainer);
// Create player
var player = new Player();
player.x = storage.playerPosition ? storage.playerPosition.x : 1024;
player.y = storage.playerPosition ? storage.playerPosition.y : 1600;
worldContainer.addChild(player);
// Create character
var character = new Character();
character.x = 500;
character.y = 1500;
worldContainer.addChild(character);
// Initialize world blocks
function initializeWorld() {
// Clear any existing blocks
while (worldContainer.children.length > 1) {
// Keep player
worldContainer.removeChildAt(1);
}
// Place blocks based on worldData
for (var y = 0; y < worldData.length; y++) {
for (var x = 0; x < worldData[y].length; x++) {
if (worldData[y][x]) {
var block = new Block(worldData[y][x], blockSize);
block.x = x * blockSize + blockSize / 2;
block.y = y * blockSize + blockSize / 2;
worldContainer.addChild(block);
}
}
}
// Create platforms if this is a new world
if (!storage.platformsCreated) {
createPlatforms();
storage.platformsCreated = true;
}
}
// Generate a new world
function generateWorld() {
var worldGen = new WorldGenerator();
var data = worldGen.generate(worldWidth, worldHeight, blockSize);
// Save the generated world
storage.worldData = JSON.stringify(data);
// Give player some starter blocks
inventory = {
"grass": 10,
"stone": 10,
"wood": 10,
"water": 5
};
storage.inventory = inventory;
return data;
}
// Setup UI
function setupUI() {
// Create inventory bar
setupInventory();
// Create tool selection buttons
setupToolbar();
// Create control buttons (left, right, jump)
setupControls();
// Create main menu button
setupMainMenuButton();
}
// Add main menu button to return to menu
function setupMainMenuButton() {
var menuButton = new Button("Menu", function () {
// Hide game elements
gameStarted = false;
// Show main menu
mainMenu.show();
});
menuButton.x = 120;
menuButton.y = 100;
LK.gui.topRight.addChild(menuButton);
// Add a character control button
var characterButton = new Button("Character", function () {
// Show character info or focus on character
var targetX = -character.x + 2048 / 2;
var targetY = -character.y + 2732 / 2;
// Animate the camera movement to the character
tween(worldContainer, {
x: targetX,
y: targetY
}, {
duration: 500,
easing: tween.easeOutQuad
});
});
characterButton.x = 120;
characterButton.y = 200;
LK.gui.topRight.addChild(characterButton);
}
function setupInventory() {
inventoryContainer = new Container();
var blockTypes = ['grass', 'stone', 'wood', 'water'];
var slotSpacing = 100;
// Create the background for selected slot
var selectedSlotBg = LK.getAsset('selectedSlot', {
anchorX: 0.5,
anchorY: 0.5
});
inventoryContainer.addChild(selectedSlotBg);
// Create inventory slots
for (var i = 0; i < blockTypes.length; i++) {
var slot = new InventorySlot(i, blockTypes[i]);
slot.x = i * slotSpacing;
slot.countText.setText((inventory[blockTypes[i]] || 0).toString());
inventoryContainer.addChild(slot);
}
// Position the inventory at the top center
inventoryContainer.x = 2048 / 2 - (blockTypes.length - 1) * slotSpacing / 2;
inventoryContainer.y = 100;
// Create inventory title
var inventoryTitle = new Text2("Inventory", {
size: 40,
fill: 0xFFFFFF
});
inventoryTitle.anchor.set(0.5, 1);
inventoryTitle.x = (blockTypes.length - 1) * slotSpacing / 2;
inventoryTitle.y = -20;
inventoryContainer.addChild(inventoryTitle);
// Update selected slot visual
updateSelectedSlot();
LK.gui.top.addChild(inventoryContainer);
}
function setupToolbar() {
toolbarContainer = new Container();
// Place tool
var placeToolBtn = new Button("Place", function () {
currentTool = 'place';
updateToolbarSelection();
});
placeToolBtn.x = 0;
toolbarContainer.addChild(placeToolBtn);
// Break tool
var breakToolBtn = new Button("Break", function () {
currentTool = 'break';
updateToolbarSelection();
});
breakToolBtn.x = 250;
toolbarContainer.addChild(breakToolBtn);
// Position toolbar at bottom right
toolbarContainer.x = 2048 - 400;
toolbarContainer.y = 2732 - 100;
LK.gui.bottomRight.addChild(toolbarContainer);
// Update the initial tool selection
updateToolbarSelection();
}
function setupControls() {
var controlsContainer = new Container();
// Create movement controls container (at the bottom left)
var moveControls = new Container();
// Left button
var leftBtn = new ControlButton("←", function () {
player.move('left');
player.stopMove = function () {
player.velocity.x = 0;
};
});
leftBtn.x = 0;
leftBtn.y = 0;
moveControls.addChild(leftBtn);
// Right button
var rightBtn = new ControlButton("→", function () {
player.move('right');
player.stopMove = function () {
player.velocity.x = 0;
};
});
rightBtn.x = 200;
rightBtn.y = 0;
moveControls.addChild(rightBtn);
// Stop button
var stopBtn = new ControlButton("■", function () {
player.stopMove();
});
stopBtn.x = 100;
stopBtn.y = 0;
moveControls.addChild(stopBtn);
// Position movement controls at bottom left
moveControls.x = 150;
moveControls.y = 2732 - 100;
LK.gui.bottomLeft.addChild(moveControls);
// Create action controls container (at bottom right)
var actionControls = new Container();
// Jump button
var jumpBtn = new ControlButton("↑", function () {
player.jump();
});
jumpBtn.x = 0;
jumpBtn.y = 0;
actionControls.addChild(jumpBtn);
// Character jump button
var characterJumpBtn = new ControlButton("C↑", function () {
character.jump();
});
characterJumpBtn.x = -200;
characterJumpBtn.y = 0;
actionControls.addChild(characterJumpBtn);
// Position action controls at bottom right
actionControls.x = 2048 - 150;
actionControls.y = 2732 - 100;
LK.gui.bottomRight.addChild(actionControls);
}
function updateSelectedSlot() {
var selectedSlotBg = inventoryContainer.getChildAt(0);
var slot = inventoryContainer.getChildAt(selectedInventorySlot + 1);
if (slot) {
selectedSlotBg.x = slot.x;
selectedSlotBg.y = slot.y;
selectedBlockType = slot.type;
}
}
function updateToolbarSelection() {
var placeToolBtn = toolbarContainer.getChildAt(0);
var breakToolBtn = toolbarContainer.getChildAt(1);
if (currentTool === 'place') {
if (placeToolBtn && placeToolBtn.buttonText) {
placeToolBtn.buttonText.style = {
fill: 0xFFFF00
};
}
if (breakToolBtn && breakToolBtn.buttonText) {
breakToolBtn.buttonText.style = {
fill: 0xFFFFFF
};
}
} else {
if (placeToolBtn && placeToolBtn.buttonText) {
placeToolBtn.buttonText.style = {
fill: 0xFFFFFF
};
}
if (breakToolBtn && breakToolBtn.buttonText) {
breakToolBtn.buttonText.style = {
fill: 0xFFFF00
};
}
}
}
function selectInventorySlot(index) {
selectedInventorySlot = index;
updateSelectedSlot();
// Play select sound
LK.getSound('select').play();
}
function updateInventoryDisplay() {
for (var i = 1; i < inventoryContainer.children.length; i++) {
var slot = inventoryContainer.getChildAt(i);
if (slot && typeof slot.updateCount === 'function') {
slot.updateCount(inventory[slot.type] || 0);
} else if (slot && slot.countText) {
slot.countText.setText((inventory[slot.type] || 0).toString());
}
}
}
function updateCamera() {
// Calculate how much to offset the world to center the player
var targetX = -player.x + 2048 / 2;
var targetY = -player.y + 2732 / 2;
// Add bounds to prevent seeing outside the world
targetX = Math.min(0, Math.max(targetX, -(worldWidth * blockSize - 2048)));
targetY = Math.min(0, Math.max(targetY, -(worldHeight * blockSize - 2732)));
// Smooth camera movement
worldContainer.x = targetX;
worldContainer.y = targetY;
}
function placeBlock(x, y) {
// Convert screen position to world position
var worldX = x - worldContainer.x;
var worldY = y - worldContainer.y;
// Convert world position to grid position
var gridX = Math.floor(worldX / blockSize);
var gridY = Math.floor(worldY / blockSize);
// Check if we have this block type in inventory
if (inventory[selectedBlockType] <= 0) {
return;
}
// Check if position is valid
if (gridX < 0 || gridX >= worldWidth || gridY < 0 || gridY >= worldHeight) {
return;
}
// Check if space is empty
if (!worldData[gridY] || gridX >= worldData[gridY].length || gridX < 0 || gridY < 0 || worldData[gridY][gridX] !== null) {
return;
}
// Don't allow placing blocks where the player is standing
var playerGridX = Math.floor(player.x / blockSize);
var playerGridY = Math.floor(player.y / blockSize);
var playerGridYBottom = Math.floor((player.y - 60) / blockSize); // Check player's head position too
if (gridX === playerGridX && (gridY === playerGridY || gridY === playerGridYBottom)) {
return; // Can't place block where player is standing
}
// Place the block
worldData[gridY][gridX] = selectedBlockType;
// Remove from inventory
inventory[selectedBlockType]--;
updateInventoryDisplay();
// Create the block visually
var block = new Block(selectedBlockType, blockSize);
block.x = gridX * blockSize + blockSize / 2;
block.y = gridY * blockSize + blockSize / 2;
worldContainer.addChild(block);
// Play place sound
LK.getSound('place').play();
// Save world data
// Save the data directly to storage
storage.worldData = JSON.stringify(worldData);
storage.inventory = inventory;
}
// Game event handlers
game.down = function (x, y, obj) {
if (!gameStarted) return; // Don't handle interactions if game hasn't started
if (currentTool === 'place') {
placeBlock(x, y);
}
// Check if we clicked on the character
if (obj && (obj === character || obj.parent === character)) {
character.startDrag();
dragTarget = character;
// Prevent placing blocks when clicking character
return;
}
};
// Add move handler for dragging character
game.move = function (x, y, obj) {
if (dragTarget === character) {
// Convert global coordinates to character's local space
var localX = x - worldContainer.x;
var localY = y - worldContainer.y;
// Determine movement direction based on touch position relative to character
if (localX > character.x + 10) {
character.startMoving(1); // Move right
} else if (localX < character.x - 10) {
character.startMoving(-1); // Move left
} else {
character.stopMoving(); // Stop if touch is directly on character
}
}
};
// Add up handler to stop dragging
game.up = function (x, y, obj) {
if (dragTarget === character) {
character.stopDrag();
character.stopMoving(); // Make sure character stops moving when touch is released
dragTarget = null;
}
};
// Initialize drag target variable
var dragTarget = null;
// Track control state for continuous button presses
var controlState = {
left: false,
right: false,
jump: false
};
// Create platforms in the world
function createPlatforms() {
// Create several platforms at different positions
var platforms = [{
x: 10,
y: 20,
width: 8,
height: 1
}, {
x: 25,
y: 18,
width: 6,
height: 1
}, {
x: 5,
y: 15,
width: 4,
height: 1
}, {
x: 15,
y: 12,
width: 5,
height: 1
}, {
x: 30,
y: 10,
width: 7,
height: 1
}];
for (var i = 0; i < platforms.length; i++) {
var p = platforms[i];
var platform = new Platform(p.width, p.height);
platform.x = p.x * blockSize;
platform.y = p.y * blockSize;
worldContainer.addChild(platform);
// Add platform blocks to worldData
for (var x = 0; x < p.width; x++) {
for (var y = 0; y < p.height; y++) {
var gridX = p.x + x;
var gridY = p.y + y;
if (gridY < worldHeight && gridX < worldWidth) {
if (!worldData[gridY]) worldData[gridY] = [];
worldData[gridY][gridX] = 'stone';
}
}
}
}
// Save world data after creating platforms
storage.worldData = JSON.stringify(worldData);
}
// Main game loop
game.update = function () {
// Only update game elements if the game has started
if (gameStarted) {
// Update player
player.update();
// Update character
character.update();
// Check if any keys/buttons are being held down
if (controlState) {
if (controlState.left) {
character.startMoving(-1);
} else if (controlState.right) {
character.startMoving(1);
}
if (controlState.jump && LK.ticks % 15 === 0) {
character.jump();
}
}
// Simple AI: Make character move back and forth
if (LK.ticks % 120 === 0 && !dragTarget) {
// Change direction every 2 seconds (only if not being controlled by player)
character.startMoving(character.direction * -1);
}
// Save game data periodically
if (LK.ticks % 300 === 0) {
// Convert worldData to string before storing
storage.worldData = JSON.stringify(worldData);
storage.inventory = inventory;
}
}
};
// Create main menu
var mainMenu = new MainMenu();
mainMenu.x = 0;
mainMenu.y = 0;
game.addChild(mainMenu);
// Create world enter menu
var worldEnterMenu = new WorldEnterMenu();
worldEnterMenu.x = 0;
worldEnterMenu.y = 0;
worldEnterMenu.visible = false;
game.addChild(worldEnterMenu);
// Show main menu to start
mainMenu.show();
// Start background music
LK.playMusic('bgmusic'); ===================================================================
--- original.js
+++ change.js
@@ -102,8 +102,9 @@
// Update method - called every frame
self.update = function () {
// Store last position for collision detection
var lastY = self.y;
+ var lastX = self.x;
// Handle movement
if (self.isMoving) {
self.x += self.speed * self.direction;
// Update animation
@@ -144,8 +145,19 @@
if (lastY < self.y && self.y - lastY < gravity * 1.5) {
// We just landed, stop any vertical momentum
self.y = Math.floor(self.y / blockSize) * blockSize - 10;
}
+ // Check for horizontal collisions
+ if (lastX !== self.x) {
+ var checkGridY = Math.floor((self.y - 50) / blockSize); // Check at character's mid height
+ var newGridX = Math.floor(self.x / blockSize);
+ // Check if there's a block at character's new position
+ if (checkGridY >= 0 && checkGridY < worldData.length && newGridX >= 0 && newGridX < worldData[checkGridY].length && worldData[checkGridY][newGridX]) {
+ // Collision detected, revert to previous position
+ self.x = lastX;
+ self.isMoving = false;
+ }
+ }
};
// Interactive events
self.down = function (x, y, obj) {
// Handle touch down on character
@@ -208,8 +220,68 @@
}
};
return self;
});
+var ControlButton = Container.expand(function (text, callback, width, height) {
+ var self = Container.call(this);
+ width = width || 150;
+ height = height || 150;
+ // Create button background
+ var background = self.attachAsset('buttonBg', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ width: width,
+ height: height
+ });
+ // Create button text
+ self.buttonText = new Text2(text, {
+ size: 48,
+ fill: 0xFFFFFF
+ });
+ self.buttonText.anchor.set(0.5, 0.5);
+ self.addChild(self.buttonText);
+ // Store callback function
+ self.callback = callback;
+ // Add control type to identify button function
+ self.controlType = '';
+ if (text === '←') self.controlType = 'left';else if (text === '→') self.controlType = 'right';else if (text === '↑') self.controlType = 'jump';else if (text === '■') self.controlType = 'stop';
+ // Button interaction
+ self.down = function (x, y, obj) {
+ if (self.callback) {
+ background.alpha = 0.7;
+ self.callback();
+ // Set control state for continuous button press
+ if (self.controlType && controlState) {
+ if (self.controlType === 'left') {
+ controlState.left = true;
+ controlState.right = false;
+ } else if (self.controlType === 'right') {
+ controlState.right = true;
+ controlState.left = false;
+ } else if (self.controlType === 'jump') {
+ controlState.jump = true;
+ } else if (self.controlType === 'stop') {
+ controlState.left = false;
+ controlState.right = false;
+ }
+ }
+ }
+ };
+ self.up = function (x, y, obj) {
+ background.alpha = 1.0;
+ // Clear control state when button is released
+ if (self.controlType && controlState) {
+ if (self.controlType === 'left') {
+ controlState.left = false;
+ } else if (self.controlType === 'right') {
+ controlState.right = false;
+ } else if (self.controlType === 'jump') {
+ controlState.jump = false;
+ }
+ }
+ };
+ return self;
+});
var InventorySlot = Container.expand(function (index, type) {
var self = Container.call(this);
self.index = index;
self.type = type;
@@ -953,36 +1025,61 @@
updateToolbarSelection();
}
function setupControls() {
var controlsContainer = new Container();
+ // Create movement controls container (at the bottom left)
+ var moveControls = new Container();
// Left button
- var leftBtn = new Button("←", function () {
+ var leftBtn = new ControlButton("←", function () {
player.move('left');
+ player.stopMove = function () {
+ player.velocity.x = 0;
+ };
});
leftBtn.x = 0;
- controlsContainer.addChild(leftBtn);
+ leftBtn.y = 0;
+ moveControls.addChild(leftBtn);
// Right button
- var rightBtn = new Button("→", function () {
+ var rightBtn = new ControlButton("→", function () {
player.move('right');
+ player.stopMove = function () {
+ player.velocity.x = 0;
+ };
});
- rightBtn.x = 250;
- controlsContainer.addChild(rightBtn);
+ rightBtn.x = 200;
+ rightBtn.y = 0;
+ moveControls.addChild(rightBtn);
+ // Stop button
+ var stopBtn = new ControlButton("■", function () {
+ player.stopMove();
+ });
+ stopBtn.x = 100;
+ stopBtn.y = 0;
+ moveControls.addChild(stopBtn);
+ // Position movement controls at bottom left
+ moveControls.x = 150;
+ moveControls.y = 2732 - 100;
+ LK.gui.bottomLeft.addChild(moveControls);
+ // Create action controls container (at bottom right)
+ var actionControls = new Container();
// Jump button
- var jumpBtn = new Button("Jump", function () {
+ var jumpBtn = new ControlButton("↑", function () {
player.jump();
});
- jumpBtn.x = 500;
- controlsContainer.addChild(jumpBtn);
+ jumpBtn.x = 0;
+ jumpBtn.y = 0;
+ actionControls.addChild(jumpBtn);
// Character jump button
- var characterJumpBtn = new Button("Character Jump", function () {
+ var characterJumpBtn = new ControlButton("C↑", function () {
character.jump();
});
- characterJumpBtn.x = 750;
- controlsContainer.addChild(characterJumpBtn);
- // Position controls at bottom left
- controlsContainer.x = 200;
- controlsContainer.y = 2732 - 100;
- LK.gui.bottomLeft.addChild(controlsContainer);
+ characterJumpBtn.x = -200;
+ characterJumpBtn.y = 0;
+ actionControls.addChild(characterJumpBtn);
+ // Position action controls at bottom right
+ actionControls.x = 2048 - 150;
+ actionControls.y = 2732 - 100;
+ LK.gui.bottomRight.addChild(actionControls);
}
function updateSelectedSlot() {
var selectedSlotBg = inventoryContainer.getChildAt(0);
var slot = inventoryContainer.getChildAt(selectedInventorySlot + 1);
@@ -1128,8 +1225,14 @@
}
};
// Initialize drag target variable
var dragTarget = null;
+// Track control state for continuous button presses
+var controlState = {
+ left: false,
+ right: false,
+ jump: false
+};
// Create platforms in the world
function createPlatforms() {
// Create several platforms at different positions
var platforms = [{
@@ -1186,11 +1289,22 @@
// Update player
player.update();
// Update character
character.update();
+ // Check if any keys/buttons are being held down
+ if (controlState) {
+ if (controlState.left) {
+ character.startMoving(-1);
+ } else if (controlState.right) {
+ character.startMoving(1);
+ }
+ if (controlState.jump && LK.ticks % 15 === 0) {
+ character.jump();
+ }
+ }
// Simple AI: Make character move back and forth
- if (LK.ticks % 120 === 0) {
- // Change direction every 2 seconds
+ if (LK.ticks % 120 === 0 && !dragTarget) {
+ // Change direction every 2 seconds (only if not being controlled by player)
character.startMoving(character.direction * -1);
}
// Save game data periodically
if (LK.ticks % 300 === 0) {
the character has a red hair, green t shirt and blue pant. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
wooden. In-Game asset. 2d. High contrast. No shadows
rock. In-Game asset. 2d. High contrast. No shadows
grasses. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
dirt. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
make the down side is square
bedrock. In-Game asset. 2d. High contrast. No shadows
left button. In-Game asset. 2d. High contrast. No shadows
right button. In-Game asset. 2d. High contrast. No shadows
up button. In-Game asset. 2d. High contrast. No shadows
sun. In-Game asset. 2d. High contrast. No shadows
moon. In-Game asset. 2d. High contrast. No shadows
cloud. In-Game asset. 2d. High contrast. No shadows
make a butterfly (horizontal). In-Game asset. 2d. High contrast. No shadows