User prompt
Make it so there is a button on the bottom of the game and it says blue screen of death and when you click it, it brings you to the blue screen of deaf
User prompt
Don’t add the restart button in the blue screen of death. Wait until it goes to 100% and then it will restart the game on its own.
Code edit (1 edits merged)
Please save this source code
User prompt
Make it so the QR code leads to roblox.com in the blue screen of death
User prompt
Blue screen of death looks like the blue screen of death. It does right now.
User prompt
When you wait, five seconds on, please restart the game there’s a button that says virus scan again and if you press it, it gives you a blue screen of death
User prompt
if you press the glitch 50 times it deletes all the UI and there’s a black background was text saying cleaning all viruses and once it gets out 100% it says error please restart the game
User prompt
Please fix the bug: 'TypeError: easing is not a function. (In 'easing(t)', 'easing' is "easeOutElastic")' in or related to this line: 'if (Math.random() < 0.005) {' Line Number: 1035
User prompt
Make the glitch button on the left scene visible
User prompt
Make it sure there’s a button that says glitch and when you press it, it glitches all the UI
User prompt
Make it so there’s a glitchy button that has all glitch particles circling around the glitch button and when you click it, it glitches all the UI and send them all over the place and when you spawn balls they go all over the place ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
make it so there’s buttons that have object objects so an object objects, and when you click the button and objects, they appear in the palette with physics
User prompt
Make it so when you press car it spawns a 2-D car that looks like a car
User prompt
There’s a little button at the very top called advanced objects, and the only advanced object that you have a drop-down menu when you press it is a car and you can drag the car on the 2-D pallet
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'if (currentMode === 'edit') {' Line Number: 154
User prompt
Make it so if you spawn 100 all of the UI move around and glitches a lot and the balls go all over the place
User prompt
Make it so there is a circle button and when you drag the circle button, it makes this it makes a circle that drags with your finger until you let go of it and then it bounces with physics ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
All of the code is missing
Code edit (1 edits merged)
Please save this source code
User prompt
Physics Playground
Initial prompt
Make something like the interactive physics the first version of Roblox you know like the interactive physics
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var DraggableButton = Container.expand(function () { var self = Container.call(this); // Create visual representation var circleButton = self.attachAsset('circle', { anchorX: 0.5, anchorY: 0.5 }); // Add a label var label = new Text2("Create", { size: 24, fill: 0xFFFFFF }); label.anchor.set(0.5, 0.5); self.addChild(label); // Track if we're currently creating an object self.isCreating = false; self.currentObject = null; // Handle touch/click down self.down = function (x, y, obj) { self.isCreating = true; // Create a new physics circle that follows the pointer self.currentObject = new PhysicsObject('circle', { mass: 1, restitution: 0.7, friction: 0.1 }); // Position at pointer self.currentObject.x = x; self.currentObject.y = y; // Add to game game.addChild(self.currentObject); // Visual feedback tween(circleButton, { alpha: 0.7, scaleX: 0.9, scaleY: 0.9 }, { duration: 100 }); }; // Handle movement self.move = function (x, y, obj) { if (self.isCreating && self.currentObject) { // Update the position to follow pointer self.currentObject.x = x; self.currentObject.y = y; } }; // Handle release self.up = function (x, y, obj) { if (self.isCreating && self.currentObject) { // Add physics to the object physicsObjects.push(self.currentObject); // Set initial velocity based on recent movement self.currentObject.velocityX = Math.random() * 10 - 5; self.currentObject.velocityY = -10; // Initial upward velocity // Reset state self.isCreating = false; self.currentObject = null; // Play sound LK.getSound('place').play(); } // Visual feedback tween(circleButton, { alpha: 1, scaleX: 1, scaleY: 1 }, { duration: 100 }); }; return self; }); var PhysicsObject = Container.expand(function (type, properties) { var self = Container.call(this); self.type = type || 'block'; // Default properties self.mass = (properties === null || properties === void 0 ? void 0 : properties.mass) || 1; self.friction = (properties === null || properties === void 0 ? void 0 : properties.friction) || 0.1; self.restitution = (properties === null || properties === void 0 ? void 0 : properties.restitution) || 0.5; self.isStatic = (properties === null || properties === void 0 ? void 0 : properties.isStatic) || false; // Physics variables self.velocityX = 0; self.velocityY = 0; self.rotation = 0; self.angularVelocity = 0; // Create visual representation var visual = self.attachAsset(type, { anchorX: 0.5, anchorY: 0.5 }); if (type === 'triangle') { // Make the triangle shape by scaling visual.scaleY = 0.5; visual.y = visual.height / 4; } self.visual = visual; // Visual feedback for static objects if (self.isStatic) { visual.alpha = 0.7; } // Modify the color based on mass var color = visual.tint; if (self.mass > 1) { // Darken for heavier objects visual.tint = darkenColor(color, 0.2 * (self.mass - 1)); } else if (self.mass < 1) { // Lighten for lighter objects visual.tint = lightenColor(color, 0.2 * (1 - self.mass)); } // Handle interaction self.down = function (x, y, obj) { // Only allow dragging if we're in edit mode if (currentMode === 'edit') { isDragging = true; draggedObject = self; // Store offset for dragging dragOffsetX = self.x - x; dragOffsetY = self.y - y; } }; return self; }); var UIButton = Container.expand(function (label, width, height, color) { var self = Container.call(this); // Create button background var background = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5 }); background.tint = color || 0x4d90ff; background.width = width || 150; background.height = height || 80; // Create button label var labelText = new Text2(label, { size: 30, fill: 0xFFFFFF }); labelText.anchor.set(0.5, 0.5); self.addChild(labelText); // Handle button press self.down = function (x, y, obj) { tween(background, { alpha: 0.7 }, { duration: 100 }); }; self.up = function (x, y, obj) { tween(background, { alpha: 1 }, { duration: 100 }); if (self.onPress) { self.onPress(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Physics constants var GRAVITY = 0.5; var DRAG = 0.02; var GROUND_Y = 2500; // Game state var physicsObjects = []; var isSimulating = true; // Create the ground var ground = new PhysicsObject('ground', { isStatic: true }); ground.x = 2048 / 2; ground.y = GROUND_Y; game.addChild(ground); physicsObjects.push(ground); // Create a draggable button that creates physics objects var circleCreator = new DraggableButton(); circleCreator.x = 200; circleCreator.y = 200; LK.gui.addChild(circleCreator); // Create a title var title = new Text2("Physics Playground", { size: 50, fill: 0xFFFFFF }); title.anchor.set(0.5, 0); title.x = 2048 / 2; title.y = 50; LK.gui.addChild(title); // Instructions var instructions = new Text2("Drag from the circle to create\nobjects that bounce with physics", { size: 30, fill: 0xFFFFFF }); instructions.anchor.set(0.5, 0); instructions.x = 2048 / 2; instructions.y = 120; LK.gui.addChild(instructions); // Game area doesn't need specific event handlers since // the DraggableButton handles its own events game.down = function (x, y, obj) {}; game.move = function (x, y, obj) {}; game.up = function (x, y, obj) {}; // Update physics in the game loop game.update = function () { if (isSimulating) { updatePhysics(); } }; // Create a new physics object at specified position function createPhysicsObject(x, y, type, properties) { var newObject = new PhysicsObject(type || 'circle', properties || { mass: 1, friction: 0.1, restitution: 0.7 }); newObject.x = x; newObject.y = y; game.addChild(newObject); physicsObjects.push(newObject); // Play place sound LK.getSound('place').play(); return newObject; } // Update physics for all objects function updatePhysics() { for (var i = 0; i < physicsObjects.length; i++) { var obj = physicsObjects[i]; if (!obj.isStatic) { // Apply gravity obj.velocityY += GRAVITY * obj.mass; // Apply drag obj.velocityX *= 1 - DRAG; obj.velocityY *= 1 - DRAG; obj.angularVelocity *= 1 - DRAG; // Update position obj.x += obj.velocityX; obj.y += obj.velocityY; // Update rotation obj.rotation += obj.angularVelocity; obj.visual.rotation = obj.rotation; // Check boundaries checkBoundaries(obj); // Check collisions with other objects for (var j = 0; j < physicsObjects.length; j++) { if (i !== j) { checkCollision(obj, physicsObjects[j]); } } } } } // Check if an object is out of bounds function checkBoundaries(obj) { // Left boundary if (obj.x - obj.visual.width / 2 < 0) { obj.x = obj.visual.width / 2; obj.velocityX = -obj.velocityX * obj.restitution; } // Right boundary if (obj.x + obj.visual.width / 2 > 2048) { obj.x = 2048 - obj.visual.width / 2; obj.velocityX = -obj.velocityX * obj.restitution; } // Bottom boundary if (obj.y + obj.visual.height / 2 > GROUND_Y - ground.visual.height / 2) { obj.y = GROUND_Y - ground.visual.height / 2 - obj.visual.height / 2; obj.velocityY = -obj.velocityY * obj.restitution; // Apply friction obj.velocityX *= 1 - obj.friction; // Play bounce sound if the impact is significant if (Math.abs(obj.velocityY) > 3) { LK.getSound('bounce').play(); } } } // Simple collision detection and response function checkCollision(obj1, obj2) { if (obj1.intersects(obj2)) { // Only process if not already processed in reverse if (obj1.isStatic && obj2.isStatic) { return; // Two static objects don't interact } // Calculate collision vector var dx = obj2.x - obj1.x; var dy = obj2.y - obj1.y; var distance = Math.sqrt(dx * dx + dy * dy); // Normalize collision vector var nx = dx / distance; var ny = dy / distance; // Calculate minimum translation distance var minDist = obj1.visual.width / 2 + obj2.visual.width / 2; var mtd = minDist - distance; // Resolve collision if (!obj1.isStatic && !obj2.isStatic) { // Both objects move var totalMass = obj1.mass + obj2.mass; var obj1Ratio = obj1.mass / totalMass; var obj2Ratio = obj2.mass / totalMass; obj1.x -= nx * mtd * obj2Ratio; obj1.y -= ny * mtd * obj2Ratio; obj2.x += nx * mtd * obj1Ratio; obj2.y += ny * mtd * obj1Ratio; // Calculate relative velocity var vrx = obj2.velocityX - obj1.velocityX; var vry = obj2.velocityY - obj1.velocityY; // Calculate impulse var impulse = -(1 + Math.min(obj1.restitution, obj2.restitution)) * (vrx * nx + vry * ny) / (1 / obj1.mass + 1 / obj2.mass); // Apply impulse obj1.velocityX -= impulse * nx / obj1.mass; obj1.velocityY -= impulse * ny / obj1.mass; obj2.velocityX += impulse * nx / obj2.mass; obj2.velocityY += impulse * ny / obj2.mass; // Add angular velocity based on off-center collision var impact = Math.abs(impulse); obj1.angularVelocity += impact * 0.01 * (Math.random() - 0.5); obj2.angularVelocity += impact * 0.01 * (Math.random() - 0.5); // Play bounce sound if the impact is significant if (impact > 3) { LK.getSound('bounce').play(); } } else { // One object is static var movingObj, staticObj; if (obj1.isStatic) { movingObj = obj2; staticObj = obj1; nx = -nx; ny = -ny; } else { movingObj = obj1; staticObj = obj2; } // Move the moving object movingObj.x += nx * mtd; movingObj.y += ny * mtd; // Calculate new velocity var dot = movingObj.velocityX * nx + movingObj.velocityY * ny; movingObj.velocityX -= (1 + movingObj.restitution) * dot * nx; movingObj.velocityY -= (1 + movingObj.restitution) * dot * ny; // Apply friction var tangentX = -ny; var tangentY = nx; var tangentDot = movingObj.velocityX * tangentX + movingObj.velocityY * tangentY; movingObj.velocityX -= tangentDot * tangentX * staticObj.friction; movingObj.velocityY -= tangentDot * tangentY * staticObj.friction; // Add angular velocity movingObj.angularVelocity += dot * 0.02 * (Math.random() - 0.5); // Play bounce sound if the impact is significant if (Math.abs(dot) > 3) { LK.getSound('bounce').play(); } } } } // Clear all objects except ground function clearAll() { for (var i = physicsObjects.length - 1; i >= 0; i--) { var obj = physicsObjects[i]; if (obj !== ground) { game.removeChild(obj); physicsObjects.splice(i, 1); } } LK.getSound('delete').play(); } // Add a clear button var clearButton = new UIButton("Clear", 150, 80, 0xdc3545); clearButton.x = 2048 - 100; clearButton.y = 300; clearButton.onPress = clearAll; LK.gui.addChild(clearButton); // Helper function to darken a color function darkenColor(color, percent) { var r = color >> 16 & 0xFF; var g = color >> 8 & 0xFF; var b = color & 0xFF; r = Math.floor(r * (1 - percent)); g = Math.floor(g * (1 - percent)); b = Math.floor(b * (1 - percent)); return r << 16 | g << 8 | b; } // Helper function to lighten a color function lightenColor(color, percent) { var r = color >> 16 & 0xFF; var g = color >> 8 & 0xFF; var b = color & 0xFF; r = Math.floor(r + (255 - r) * percent); g = Math.floor(g + (255 - g) * percent); b = Math.floor(b + (255 - b) * percent); return r << 16 | g << 8 | b; } // Check for eraser collisions game.update = function () { updatePhysics(); }; // Play background music LK.playMusic('bgmusic');
===================================================================
--- original.js
+++ change.js
@@ -1,242 +1,169 @@
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
-var Button = Container.expand(function () {
+var DraggableButton = Container.expand(function () {
var self = Container.call(this);
- self.init = function (props) {
- if (!props) props = {};
- // Create button background
- self.bg = self.attachAsset('buttonBg', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- // Create icon if provided
- if (props.icon) {
- self.icon = self.attachAsset(props.icon, {
- anchorX: 0.5,
- anchorY: 0.5,
- scaleX: 0.8,
- scaleY: 0.8
- });
- }
- // Create label if provided
- if (props.label) {
- self.label = new Text2(props.label, {
- size: props.fontSize || 35,
- fill: props.textColor || "#FFFFFF"
- });
- self.label.anchor.set(0.5);
- self.addChild(self.label);
- }
- // Set position
- if (props.x !== undefined) self.x = props.x;
- if (props.y !== undefined) self.y = props.y;
- // Store callback
- self.callback = props.callback;
- return self;
- };
+ // Create visual representation
+ var circleButton = self.attachAsset('circle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Add a label
+ var label = new Text2("Create", {
+ size: 24,
+ fill: 0xFFFFFF
+ });
+ label.anchor.set(0.5, 0.5);
+ self.addChild(label);
+ // Track if we're currently creating an object
+ self.isCreating = false;
+ self.currentObject = null;
+ // Handle touch/click down
self.down = function (x, y, obj) {
+ self.isCreating = true;
+ // Create a new physics circle that follows the pointer
+ self.currentObject = new PhysicsObject('circle', {
+ mass: 1,
+ restitution: 0.7,
+ friction: 0.1
+ });
+ // Position at pointer
+ self.currentObject.x = x;
+ self.currentObject.y = y;
+ // Add to game
+ game.addChild(self.currentObject);
// Visual feedback
- tween.to(self, 0.1, {
+ tween(circleButton, {
+ alpha: 0.7,
scaleX: 0.9,
scaleY: 0.9
+ }, {
+ duration: 100
});
- LK.getSound('click').play();
};
+ // Handle movement
+ self.move = function (x, y, obj) {
+ if (self.isCreating && self.currentObject) {
+ // Update the position to follow pointer
+ self.currentObject.x = x;
+ self.currentObject.y = y;
+ }
+ };
+ // Handle release
self.up = function (x, y, obj) {
+ if (self.isCreating && self.currentObject) {
+ // Add physics to the object
+ physicsObjects.push(self.currentObject);
+ // Set initial velocity based on recent movement
+ self.currentObject.velocityX = Math.random() * 10 - 5;
+ self.currentObject.velocityY = -10; // Initial upward velocity
+ // Reset state
+ self.isCreating = false;
+ self.currentObject = null;
+ // Play sound
+ LK.getSound('place').play();
+ }
// Visual feedback
- tween.to(self, 0.1, {
+ tween(circleButton, {
+ alpha: 1,
scaleX: 1,
scaleY: 1
+ }, {
+ duration: 100
});
- // Execute callback
- if (self.callback) {
- self.callback();
- }
};
return self;
});
-// Set background color
-var PhysicsObject = Container.expand(function () {
+var PhysicsObject = Container.expand(function (type, properties) {
var self = Container.call(this);
- // Physics properties
+ self.type = type || 'block';
+ // Default properties
+ self.mass = (properties === null || properties === void 0 ? void 0 : properties.mass) || 1;
+ self.friction = (properties === null || properties === void 0 ? void 0 : properties.friction) || 0.1;
+ self.restitution = (properties === null || properties === void 0 ? void 0 : properties.restitution) || 0.5;
+ self.isStatic = (properties === null || properties === void 0 ? void 0 : properties.isStatic) || false;
+ // Physics variables
self.velocityX = 0;
self.velocityY = 0;
- self.acceleration = 0;
- self.gravity = 0.5;
- self.bounce = 0.7;
- self.friction = 0.98;
- self.mass = 1;
- self.isStatic = false;
- self.type = 'square';
- self.lastX = 0;
- self.lastY = 0;
- self.lastWasIntersecting = false;
- self.isDragging = false;
- self.dragOffsetX = 0;
- self.dragOffsetY = 0;
- // Initialize the physics object with given properties
- self.init = function (props) {
- if (!props) props = {};
- self.type = props.type || 'square';
- self.asset = self.attachAsset(self.type, {
- anchorX: 0.5,
- anchorY: 0.5
- });
- if (props.x !== undefined) self.x = props.x;
- if (props.y !== undefined) self.y = props.y;
- if (props.isStatic !== undefined) self.isStatic = props.isStatic;
- if (props.gravity !== undefined) self.gravity = props.gravity;
- if (props.bounce !== undefined) self.bounce = props.bounce;
- if (props.friction !== undefined) self.friction = props.friction;
- if (props.mass !== undefined) self.mass = props.mass;
- self.lastX = self.x;
- self.lastY = self.y;
- return self;
- };
- // Apply physics simulation
- self.update = function () {
- if (self.isStatic || self.isDragging) return;
- self.lastX = self.x;
- self.lastY = self.y;
- // Apply gravity
- self.velocityY += self.gravity * self.mass;
- // Apply friction
- self.velocityX *= self.friction;
- // Update position
- self.x += self.velocityX;
- self.y += self.velocityY;
- // Check boundaries
- self.checkBoundaries();
- };
- // Handle collisions with boundaries
- self.checkBoundaries = function () {
- var width = self.asset.width;
- var height = self.asset.height;
- var halfWidth = width / 2;
- var halfHeight = height / 2;
- // Floor collision
- if (self.y + halfHeight > game.groundY) {
- self.y = game.groundY - halfHeight;
- self.velocityY = -self.velocityY * self.bounce;
- // Stop very small bounces
- if (Math.abs(self.velocityY) < 0.5) {
- self.velocityY = 0;
- } else {
- // Play bounce sound if it's a significant bounce
- if (Math.abs(self.velocityY) > 2) {
- LK.getSound('bounce').play();
- }
- }
+ self.rotation = 0;
+ self.angularVelocity = 0;
+ // Create visual representation
+ var visual = self.attachAsset(type, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ if (type === 'triangle') {
+ // Make the triangle shape by scaling
+ visual.scaleY = 0.5;
+ visual.y = visual.height / 4;
+ }
+ self.visual = visual;
+ // Visual feedback for static objects
+ if (self.isStatic) {
+ visual.alpha = 0.7;
+ }
+ // Modify the color based on mass
+ var color = visual.tint;
+ if (self.mass > 1) {
+ // Darken for heavier objects
+ visual.tint = darkenColor(color, 0.2 * (self.mass - 1));
+ } else if (self.mass < 1) {
+ // Lighten for lighter objects
+ visual.tint = lightenColor(color, 0.2 * (1 - self.mass));
+ }
+ // Handle interaction
+ self.down = function (x, y, obj) {
+ // Only allow dragging if we're in edit mode
+ if (currentMode === 'edit') {
+ isDragging = true;
+ draggedObject = self;
+ // Store offset for dragging
+ dragOffsetX = self.x - x;
+ dragOffsetY = self.y - y;
}
- // Wall collisions
- if (self.x - halfWidth < 0) {
- self.x = halfWidth;
- self.velocityX = -self.velocityX * self.bounce;
- } else if (self.x + halfWidth > 2048) {
- self.x = 2048 - halfWidth;
- self.velocityX = -self.velocityX * self.bounce;
- }
};
- // Handle collision with another physics object
- self.collideWith = function (other) {
- if (self.isStatic || other.isStatic) {
- self.handleStaticCollision(other);
- return;
- }
- // Calculate collision response
- var dx = other.x - self.x;
- var dy = other.y - self.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- var minDistance = (self.asset.width + other.asset.width) / 2;
- // If objects are overlapping
- if (distance < minDistance) {
- // Normalize direction vector
- var nx = dx / distance;
- var ny = dy / distance;
- // Calculate relative velocity
- var relativeVelocityX = other.velocityX - self.velocityX;
- var relativeVelocityY = other.velocityY - self.velocityY;
- // Calculate impulse
- var impulse = 2 * (relativeVelocityX * nx + relativeVelocityY * ny) / (self.mass + other.mass);
- // Apply impulse to velocities
- self.velocityX += impulse * other.mass * nx * self.bounce;
- self.velocityY += impulse * other.mass * ny * self.bounce;
- other.velocityX -= impulse * self.mass * nx * other.bounce;
- other.velocityY -= impulse * self.mass * ny * other.bounce;
- // Separate objects to prevent sticking
- var overlap = minDistance - distance;
- var separationX = overlap * nx * 0.5;
- var separationY = overlap * ny * 0.5;
- self.x -= separationX;
- self.y -= separationY;
- other.x += separationX;
- other.y += separationY;
- // Play sound if the collision is strong enough
- if (Math.abs(impulse) > 2) {
- LK.getSound('bounce').play();
- }
- }
- };
- // Handle collision with static objects
- self.handleStaticCollision = function (other) {
- if (self.isStatic) return; // Both static, no collision
- var dx = other.x - self.x;
- var dy = other.y - self.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- var minDistance = (self.asset.width + other.asset.width) / 2;
- // If objects are overlapping
- if (distance < minDistance) {
- // Normalize direction vector
- var nx = dx / distance;
- var ny = dy / distance;
- // Calculate separation distance
- var overlap = minDistance - distance;
- // Move only the non-static object
- self.x -= overlap * nx;
- self.y -= overlap * ny;
- // Reflect velocity
- var dot = self.velocityX * nx + self.velocityY * ny;
- self.velocityX = self.velocityX - 2 * dot * nx;
- self.velocityY = self.velocityY - 2 * dot * ny;
- // Apply bounce factor
- self.velocityX *= self.bounce;
- self.velocityY *= self.bounce;
- // Play sound if the collision is strong enough
- if (Math.abs(dot) > 2) {
- LK.getSound('bounce').play();
- }
- }
- };
- // Event handlers
+ return self;
+});
+var UIButton = Container.expand(function (label, width, height, color) {
+ var self = Container.call(this);
+ // Create button background
+ var background = self.attachAsset('block', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ background.tint = color || 0x4d90ff;
+ background.width = width || 150;
+ background.height = height || 80;
+ // Create button label
+ var labelText = new Text2(label, {
+ size: 30,
+ fill: 0xFFFFFF
+ });
+ labelText.anchor.set(0.5, 0.5);
+ self.addChild(labelText);
+ // Handle button press
self.down = function (x, y, obj) {
- self.isDragging = true;
- self.dragOffsetX = self.x - x;
- self.dragOffsetY = self.y - y;
- // Bring to front
- if (self.parent) {
- self.parent.removeChild(self);
- self.parent.addChild(self);
- }
- // Stop any current movement
- self.velocityX = 0;
- self.velocityY = 0;
+ tween(background, {
+ alpha: 0.7
+ }, {
+ duration: 100
+ });
};
self.up = function (x, y, obj) {
- if (self.isDragging) {
- // Calculate release velocity based on last few positions
- if (self.lastX !== self.x || self.lastY !== self.y) {
- self.velocityX = (self.x - self.lastX) * 0.5;
- self.velocityY = (self.y - self.lastY) * 0.5;
- }
- self.isDragging = false;
- LK.getSound('drop').play();
+ tween(background, {
+ alpha: 1
+ }, {
+ duration: 100
+ });
+ if (self.onPress) {
+ self.onPress();
}
};
return self;
});
@@ -244,213 +171,248 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000
+ backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
-// Set background color
-// Define shapes for physics objects
-// Define sounds for interactions
-// Import tween plugin for smooth animations
-game.setBackgroundColor(0x2c3e50);
-// Game variables
+// Physics constants
+var GRAVITY = 0.5;
+var DRAG = 0.02;
+var GROUND_Y = 2500;
+// Game state
var physicsObjects = [];
-var buttons = [];
-var gravityEnabled = true;
-game.groundY = 2500; // Y-position of the ground
-// Create ground
-var ground = new PhysicsObject().init({
- type: 'ground',
- x: 1024,
- y: game.groundY + 50,
+var isSimulating = true;
+// Create the ground
+var ground = new PhysicsObject('ground', {
isStatic: true
});
+ground.x = 2048 / 2;
+ground.y = GROUND_Y;
game.addChild(ground);
-// Create walls
-var leftWall = new PhysicsObject().init({
- type: 'wall',
- x: 25,
- y: 1400,
- isStatic: true
-});
-game.addChild(leftWall);
-var rightWall = new PhysicsObject().init({
- type: 'wall',
- x: 2023,
- y: 1400,
- isStatic: true
-});
-game.addChild(rightWall);
-// Create platforms
-var platform1 = new PhysicsObject().init({
- type: 'platform',
- x: 600,
- y: 1800,
- isStatic: true
-});
-game.addChild(platform1);
-var platform2 = new PhysicsObject().init({
- type: 'platform',
- x: 1400,
- y: 1500,
- isStatic: true
-});
-game.addChild(platform2);
-// Create title text
-var titleText = new Text2("Physics Playground", {
- size: 70,
+physicsObjects.push(ground);
+// Create a draggable button that creates physics objects
+var circleCreator = new DraggableButton();
+circleCreator.x = 200;
+circleCreator.y = 200;
+LK.gui.addChild(circleCreator);
+// Create a title
+var title = new Text2("Physics Playground", {
+ size: 50,
fill: 0xFFFFFF
});
-titleText.anchor.set(0.5, 0);
-titleText.x = 1024;
-titleText.y = 50;
-game.addChild(titleText);
-// Create instructions text
-var instructions = new Text2("Drag and drop objects to create physics interactions", {
- size: 40,
+title.anchor.set(0.5, 0);
+title.x = 2048 / 2;
+title.y = 50;
+LK.gui.addChild(title);
+// Instructions
+var instructions = new Text2("Drag from the circle to create\nobjects that bounce with physics", {
+ size: 30,
fill: 0xFFFFFF
});
instructions.anchor.set(0.5, 0);
-instructions.x = 1024;
-instructions.y = 150;
-game.addChild(instructions);
-// Create objects toolbar
-var toolbar = new Container();
-toolbar.x = 1024;
-toolbar.y = 250;
-game.addChild(toolbar);
-// Create shape buttons
-var shapes = ['square', 'circle', 'rectangle'];
-var shapeNames = ['Square', 'Circle', 'Rectangle'];
-for (var i = 0; i < shapes.length; i++) {
- var shapeBtn = new Button().init({
- label: shapeNames[i],
- x: (i - 1) * 250,
- y: 0,
- callback: createObjectFactory(shapes[i])
+instructions.x = 2048 / 2;
+instructions.y = 120;
+LK.gui.addChild(instructions);
+// Game area doesn't need specific event handlers since
+// the DraggableButton handles its own events
+game.down = function (x, y, obj) {};
+game.move = function (x, y, obj) {};
+game.up = function (x, y, obj) {};
+// Update physics in the game loop
+game.update = function () {
+ if (isSimulating) {
+ updatePhysics();
+ }
+};
+// Create a new physics object at specified position
+function createPhysicsObject(x, y, type, properties) {
+ var newObject = new PhysicsObject(type || 'circle', properties || {
+ mass: 1,
+ friction: 0.1,
+ restitution: 0.7
});
- toolbar.addChild(shapeBtn);
- buttons.push(shapeBtn);
+ newObject.x = x;
+ newObject.y = y;
+ game.addChild(newObject);
+ physicsObjects.push(newObject);
+ // Play place sound
+ LK.getSound('place').play();
+ return newObject;
}
-// Create control buttons
-var gravityBtn = new Button().init({
- icon: 'gravityIcon',
- label: "Gravity",
- x: -300,
- y: 100,
- callback: toggleGravity
-});
-toolbar.addChild(gravityBtn);
-buttons.push(gravityBtn);
-var bounceBtn = new Button().init({
- icon: 'bounceIcon',
- label: "Bounce+",
- x: 0,
- y: 100,
- callback: increaseBounce
-});
-toolbar.addChild(bounceBtn);
-buttons.push(bounceBtn);
-var resetBtn = new Button().init({
- icon: 'resetIcon',
- label: "Reset",
- x: 300,
- y: 100,
- callback: resetScene
-});
-toolbar.addChild(resetBtn);
-buttons.push(resetBtn);
-// Factory function to create object creation callbacks
-function createObjectFactory(type) {
- return function () {
- var obj = new PhysicsObject().init({
- type: type,
- x: 1024,
- y: 700
- });
- game.addChild(obj);
- physicsObjects.push(obj);
- // Slight animation to show object creation
- obj.scale.x = 0.1;
- obj.scale.y = 0.1;
- tween.to(obj, 0.3, {
- scaleX: 1,
- scaleY: 1,
- ease: "backOut"
- });
- LK.getSound('drop').play();
- };
-}
-// Toggle gravity
-function toggleGravity() {
- gravityEnabled = !gravityEnabled;
+// Update physics for all objects
+function updatePhysics() {
for (var i = 0; i < physicsObjects.length; i++) {
- physicsObjects[i].gravity = gravityEnabled ? 0.5 : 0;
- // If turning gravity off, stop all movement
- if (!gravityEnabled) {
- physicsObjects[i].velocityX = 0;
- physicsObjects[i].velocityY = 0;
+ var obj = physicsObjects[i];
+ if (!obj.isStatic) {
+ // Apply gravity
+ obj.velocityY += GRAVITY * obj.mass;
+ // Apply drag
+ obj.velocityX *= 1 - DRAG;
+ obj.velocityY *= 1 - DRAG;
+ obj.angularVelocity *= 1 - DRAG;
+ // Update position
+ obj.x += obj.velocityX;
+ obj.y += obj.velocityY;
+ // Update rotation
+ obj.rotation += obj.angularVelocity;
+ obj.visual.rotation = obj.rotation;
+ // Check boundaries
+ checkBoundaries(obj);
+ // Check collisions with other objects
+ for (var j = 0; j < physicsObjects.length; j++) {
+ if (i !== j) {
+ checkCollision(obj, physicsObjects[j]);
+ }
+ }
}
}
}
-// Increase bounce factor
-function increaseBounce() {
- for (var i = 0; i < physicsObjects.length; i++) {
- physicsObjects[i].bounce = Math.min(physicsObjects[i].bounce + 0.1, 1.5);
+// Check if an object is out of bounds
+function checkBoundaries(obj) {
+ // Left boundary
+ if (obj.x - obj.visual.width / 2 < 0) {
+ obj.x = obj.visual.width / 2;
+ obj.velocityX = -obj.velocityX * obj.restitution;
}
-}
-// Reset the scene
-function resetScene() {
- for (var i = physicsObjects.length - 1; i >= 0; i--) {
- physicsObjects[i].destroy();
+ // Right boundary
+ if (obj.x + obj.visual.width / 2 > 2048) {
+ obj.x = 2048 - obj.visual.width / 2;
+ obj.velocityX = -obj.velocityX * obj.restitution;
}
- physicsObjects = [];
-}
-// Add some initial objects
-LK.setTimeout(function () {
- createObjectFactory('square')();
- LK.setTimeout(function () {
- createObjectFactory('circle')();
- }, 300);
- LK.setTimeout(function () {
- createObjectFactory('rectangle')();
- }, 600);
-}, 500);
-// Move object being dragged
-game.move = function (x, y, obj) {
- for (var i = 0; i < physicsObjects.length; i++) {
- var object = physicsObjects[i];
- if (object.isDragging) {
- object.x = x + object.dragOffsetX;
- object.y = y + object.dragOffsetY;
+ // Bottom boundary
+ if (obj.y + obj.visual.height / 2 > GROUND_Y - ground.visual.height / 2) {
+ obj.y = GROUND_Y - ground.visual.height / 2 - obj.visual.height / 2;
+ obj.velocityY = -obj.velocityY * obj.restitution;
+ // Apply friction
+ obj.velocityX *= 1 - obj.friction;
+ // Play bounce sound if the impact is significant
+ if (Math.abs(obj.velocityY) > 3) {
+ LK.getSound('bounce').play();
}
}
-};
-// Game update loop
-game.update = function () {
- // Update all physics objects
- for (var i = 0; i < physicsObjects.length; i++) {
- physicsObjects[i].update();
- }
- // Check collisions between objects
- for (var i = 0; i < physicsObjects.length; i++) {
- for (var j = i + 1; j < physicsObjects.length; j++) {
- var obj1 = physicsObjects[i];
- var obj2 = physicsObjects[j];
- if (obj1.intersects(obj2)) {
- obj1.collideWith(obj2);
+}
+// Simple collision detection and response
+function checkCollision(obj1, obj2) {
+ if (obj1.intersects(obj2)) {
+ // Only process if not already processed in reverse
+ if (obj1.isStatic && obj2.isStatic) {
+ return; // Two static objects don't interact
+ }
+ // Calculate collision vector
+ var dx = obj2.x - obj1.x;
+ var dy = obj2.y - obj1.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ // Normalize collision vector
+ var nx = dx / distance;
+ var ny = dy / distance;
+ // Calculate minimum translation distance
+ var minDist = obj1.visual.width / 2 + obj2.visual.width / 2;
+ var mtd = minDist - distance;
+ // Resolve collision
+ if (!obj1.isStatic && !obj2.isStatic) {
+ // Both objects move
+ var totalMass = obj1.mass + obj2.mass;
+ var obj1Ratio = obj1.mass / totalMass;
+ var obj2Ratio = obj2.mass / totalMass;
+ obj1.x -= nx * mtd * obj2Ratio;
+ obj1.y -= ny * mtd * obj2Ratio;
+ obj2.x += nx * mtd * obj1Ratio;
+ obj2.y += ny * mtd * obj1Ratio;
+ // Calculate relative velocity
+ var vrx = obj2.velocityX - obj1.velocityX;
+ var vry = obj2.velocityY - obj1.velocityY;
+ // Calculate impulse
+ var impulse = -(1 + Math.min(obj1.restitution, obj2.restitution)) * (vrx * nx + vry * ny) / (1 / obj1.mass + 1 / obj2.mass);
+ // Apply impulse
+ obj1.velocityX -= impulse * nx / obj1.mass;
+ obj1.velocityY -= impulse * ny / obj1.mass;
+ obj2.velocityX += impulse * nx / obj2.mass;
+ obj2.velocityY += impulse * ny / obj2.mass;
+ // Add angular velocity based on off-center collision
+ var impact = Math.abs(impulse);
+ obj1.angularVelocity += impact * 0.01 * (Math.random() - 0.5);
+ obj2.angularVelocity += impact * 0.01 * (Math.random() - 0.5);
+ // Play bounce sound if the impact is significant
+ if (impact > 3) {
+ LK.getSound('bounce').play();
}
+ } else {
+ // One object is static
+ var movingObj, staticObj;
+ if (obj1.isStatic) {
+ movingObj = obj2;
+ staticObj = obj1;
+ nx = -nx;
+ ny = -ny;
+ } else {
+ movingObj = obj1;
+ staticObj = obj2;
+ }
+ // Move the moving object
+ movingObj.x += nx * mtd;
+ movingObj.y += ny * mtd;
+ // Calculate new velocity
+ var dot = movingObj.velocityX * nx + movingObj.velocityY * ny;
+ movingObj.velocityX -= (1 + movingObj.restitution) * dot * nx;
+ movingObj.velocityY -= (1 + movingObj.restitution) * dot * ny;
+ // Apply friction
+ var tangentX = -ny;
+ var tangentY = nx;
+ var tangentDot = movingObj.velocityX * tangentX + movingObj.velocityY * tangentY;
+ movingObj.velocityX -= tangentDot * tangentX * staticObj.friction;
+ movingObj.velocityY -= tangentDot * tangentY * staticObj.friction;
+ // Add angular velocity
+ movingObj.angularVelocity += dot * 0.02 * (Math.random() - 0.5);
+ // Play bounce sound if the impact is significant
+ if (Math.abs(dot) > 3) {
+ LK.getSound('bounce').play();
+ }
}
}
- // Remove objects that are too far off-screen
+}
+// Clear all objects except ground
+function clearAll() {
for (var i = physicsObjects.length - 1; i >= 0; i--) {
var obj = physicsObjects[i];
- if (obj.y > 3000 || obj.y < -500) {
- obj.destroy();
+ if (obj !== ground) {
+ game.removeChild(obj);
physicsObjects.splice(i, 1);
}
}
-};
\ No newline at end of file
+ LK.getSound('delete').play();
+}
+// Add a clear button
+var clearButton = new UIButton("Clear", 150, 80, 0xdc3545);
+clearButton.x = 2048 - 100;
+clearButton.y = 300;
+clearButton.onPress = clearAll;
+LK.gui.addChild(clearButton);
+// Helper function to darken a color
+function darkenColor(color, percent) {
+ var r = color >> 16 & 0xFF;
+ var g = color >> 8 & 0xFF;
+ var b = color & 0xFF;
+ r = Math.floor(r * (1 - percent));
+ g = Math.floor(g * (1 - percent));
+ b = Math.floor(b * (1 - percent));
+ return r << 16 | g << 8 | b;
+}
+// Helper function to lighten a color
+function lightenColor(color, percent) {
+ var r = color >> 16 & 0xFF;
+ var g = color >> 8 & 0xFF;
+ var b = color & 0xFF;
+ r = Math.floor(r + (255 - r) * percent);
+ g = Math.floor(g + (255 - g) * percent);
+ b = Math.floor(b + (255 - b) * percent);
+ return r << 16 | g << 8 | b;
+}
+// Check for eraser collisions
+game.update = function () {
+ updatePhysics();
+};
+// Play background music
+LK.playMusic('bgmusic');
\ No newline at end of file