Code edit (1 edits merged)
Please save this source code
User prompt
Snowman Builder
Initial prompt
Toca snow-man (2013). Use your finger to roll the small snowball into big snowball, use your finger to roll the small snowball into big snowball for the head, ginger 🐱 says “it’s too heavy.” Use your finger to place the Big snowball head onto the big snowball body, drag on the hat 🎩 the broom 🧹 2 nuts 🌰 and a carrot 🥕, shake your device 10 times to make the snow fall
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Accessory = Container.expand(function (type) {
var self = Container.call(this);
self.type = type;
self.placed = false;
var graphics = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
if (!self.placed) {
draggedObject = self;
}
};
return self;
});
var Snowball = Container.expand(function () {
var self = Container.call(this);
self.size = 80;
self.minSize = 80;
self.maxSize = 300;
self.isRolling = false;
self.lastX = 0;
self.lastY = 0;
self.rollDistance = 0;
var graphics = self.attachAsset('snowball', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1,
scaleY: 1
});
self.down = function (x, y, obj) {
self.isRolling = true;
self.lastX = x;
self.lastY = y;
draggedObject = self;
};
self.updateSize = function () {
var scale = self.size / 80;
graphics.scaleX = scale;
graphics.scaleY = scale;
};
self.grow = function (amount) {
if (self.size < self.maxSize) {
self.size += amount;
self.updateSize();
}
};
return self;
});
var Snowflake = Container.expand(function () {
var self = Container.call(this);
self.speed = Math.random() * 3 + 1;
self.drift = Math.random() * 2 - 1;
var graphics = self.attachAsset('snowflake', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += self.speed;
self.x += self.drift;
if (self.y > 2732 + 50) {
self.y = -50;
self.x = Math.random() * 2048;
}
if (self.x < -50) {
self.x = 2048 + 50;
} else if (self.x > 2048 + 50) {
self.x = -50;
}
};
return self;
});
var SnowmanBody = Container.expand(function () {
var self = Container.call(this);
self.hasHead = false;
self.accessories = [];
var bodyGraphics = self.attachAsset('snowman_body', {
anchorX: 0.5,
anchorY: 1
});
self.addHead = function (head) {
if (!self.hasHead) {
head.x = 0;
head.y = -200;
self.addChild(head);
self.hasHead = true;
LK.getSound('stack').play();
}
};
self.addAccessory = function (accessory) {
self.accessories.push(accessory);
self.addChild(accessory);
LK.getSound('accessory').play();
};
return self;
});
var SnowmanHead = Container.expand(function () {
var self = Container.call(this);
var headGraphics = self.attachAsset('snowman_head', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
draggedObject = self;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game state
var gamePhase = 'rolling'; // 'rolling', 'building', 'decorating', 'completed'
var draggedObject = null;
var snowballs = [];
var snowman = null;
var accessories = [];
var snowflakes = [];
var shakeCount = 0;
var isSnowing = false;
// Ground
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 1,
x: 0,
y: 2732
}));
// Instructions
var instructionText = new Text2('Drag snowballs to roll them bigger!', {
size: 60,
fill: 0x000000
});
instructionText.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionText);
// Create initial snowballs
for (var i = 0; i < 3; i++) {
var snowball = new Snowball();
snowball.x = 300 + i * 400;
snowball.y = 2400;
snowballs.push(snowball);
game.addChild(snowball);
}
// Create accessories (initially hidden)
var accessoryTypes = ['hat', 'broom', 'button', 'carrot'];
for (var j = 0; j < accessoryTypes.length; j++) {
var accessory = new Accessory(accessoryTypes[j]);
accessory.x = 200 + j * 400;
accessory.y = 2600;
accessory.alpha = 0;
accessories.push(accessory);
game.addChild(accessory);
}
function checkSnowballSizes() {
var largeBalls = 0;
var mediumBalls = 0;
for (var i = 0; i < snowballs.length; i++) {
if (snowballs[i].size >= 250) {
largeBalls++;
} else if (snowballs[i].size >= 150) {
mediumBalls++;
}
}
if (largeBalls >= 1 && mediumBalls >= 1 && gamePhase === 'rolling') {
gamePhase = 'building';
instructionText.setText('Stack a smaller snowball on top of a larger one!');
}
}
function createSnowman(bodyBall, headBall) {
snowman = new SnowmanBody();
snowman.x = bodyBall.x;
snowman.y = bodyBall.y;
game.addChild(snowman);
// Remove original snowballs
bodyBall.destroy();
headBall.destroy();
// Remove from arrays
var bodyIndex = snowballs.indexOf(bodyBall);
var headIndex = snowballs.indexOf(headBall);
if (bodyIndex > -1) snowballs.splice(bodyIndex, 1);
if (headIndex > -1) snowballs.splice(headIndex, 1);
// Add head to snowman
var head = new SnowmanHead();
snowman.addHead(head);
gamePhase = 'decorating';
instructionText.setText('Drag accessories to decorate your snowman!');
// Show accessories
for (var i = 0; i < accessories.length; i++) {
tween(accessories[i], {
alpha: 1
}, {
duration: 500
});
}
}
function checkAccessoryPlacement(accessory) {
if (!snowman) return;
var distance = Math.sqrt(Math.pow(accessory.x - snowman.x, 2) + Math.pow(accessory.y - snowman.y, 2));
if (distance < 200) {
// Place accessory on snowman
var localPos = snowman.toLocal({
x: accessory.x,
y: accessory.y
});
accessory.x = localPos.x;
accessory.y = localPos.y;
accessory.placed = true;
snowman.addAccessory(accessory);
// Position specific accessories
if (accessory.type === 'hat') {
accessory.x = 0;
accessory.y = -250;
} else if (accessory.type === 'carrot') {
accessory.x = 0;
accessory.y = -200;
} else if (accessory.type === 'button') {
accessory.x = 0;
accessory.y = -100;
} else if (accessory.type === 'broom') {
accessory.x = 120;
accessory.y = -100;
}
// Check if all accessories are placed
var placedCount = 0;
for (var i = 0; i < accessories.length; i++) {
if (accessories[i].placed) placedCount++;
}
if (placedCount === accessories.length) {
gamePhase = 'completed';
instructionText.setText('Shake your device 10 times to make it snow!');
LK.getSound('complete').play();
}
}
}
function startSnowfall() {
if (isSnowing) return;
isSnowing = true;
instructionText.setText('Beautiful! Your snowman is complete!');
// Create snowflakes
for (var i = 0; i < 50; i++) {
var snowflake = new Snowflake();
snowflake.x = Math.random() * 2048;
snowflake.y = Math.random() * -500;
snowflakes.push(snowflake);
game.addChild(snowflake);
}
}
// Game event handlers
game.move = function (x, y, obj) {
if (draggedObject) {
draggedObject.x = x;
draggedObject.y = y;
// Handle snowball rolling
if (draggedObject instanceof Snowball && draggedObject.isRolling) {
var distance = Math.sqrt(Math.pow(x - draggedObject.lastX, 2) + Math.pow(y - draggedObject.lastY, 2));
if (distance > 10) {
draggedObject.rollDistance += distance;
if (draggedObject.rollDistance > 50) {
draggedObject.grow(2);
draggedObject.rollDistance = 0;
}
}
draggedObject.lastX = x;
draggedObject.lastY = y;
}
}
};
game.up = function (x, y, obj) {
if (draggedObject) {
if (draggedObject instanceof Snowball) {
draggedObject.isRolling = false;
// Check for stacking
if (gamePhase === 'building') {
for (var i = 0; i < snowballs.length; i++) {
var otherBall = snowballs[i];
if (otherBall !== draggedObject) {
var distance = Math.sqrt(Math.pow(draggedObject.x - otherBall.x, 2) + Math.pow(draggedObject.y - otherBall.y, 2));
if (distance < 150) {
// Determine which is larger
if (draggedObject.size < otherBall.size) {
createSnowman(otherBall, draggedObject);
} else {
createSnowman(draggedObject, otherBall);
}
break;
}
}
}
}
} else if (draggedObject instanceof Accessory) {
checkAccessoryPlacement(draggedObject);
}
draggedObject = null;
}
};
// Shake detection (simulated with rapid mouse movements)
var lastShakeTime = 0;
var shakeThreshold = 100;
game.update = function () {
// Check snowball sizes
if (gamePhase === 'rolling') {
checkSnowballSizes();
}
// Simple shake detection simulation
if (gamePhase === 'completed' && !isSnowing) {
// Simulate shake with rapid movements or random trigger
if (LK.ticks % 180 === 0) {
// Every 3 seconds for demo
shakeCount++;
if (shakeCount >= 10) {
startSnowfall();
} else {
instructionText.setText('Shake ' + (10 - shakeCount) + ' more times to make it snow!');
}
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,331 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Accessory = Container.expand(function (type) {
+ var self = Container.call(this);
+ self.type = type;
+ self.placed = false;
+ var graphics = self.attachAsset(type, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.down = function (x, y, obj) {
+ if (!self.placed) {
+ draggedObject = self;
+ }
+ };
+ return self;
+});
+var Snowball = Container.expand(function () {
+ var self = Container.call(this);
+ self.size = 80;
+ self.minSize = 80;
+ self.maxSize = 300;
+ self.isRolling = false;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.rollDistance = 0;
+ var graphics = self.attachAsset('snowball', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 1,
+ scaleY: 1
+ });
+ self.down = function (x, y, obj) {
+ self.isRolling = true;
+ self.lastX = x;
+ self.lastY = y;
+ draggedObject = self;
+ };
+ self.updateSize = function () {
+ var scale = self.size / 80;
+ graphics.scaleX = scale;
+ graphics.scaleY = scale;
+ };
+ self.grow = function (amount) {
+ if (self.size < self.maxSize) {
+ self.size += amount;
+ self.updateSize();
+ }
+ };
+ return self;
+});
+var Snowflake = Container.expand(function () {
+ var self = Container.call(this);
+ self.speed = Math.random() * 3 + 1;
+ self.drift = Math.random() * 2 - 1;
+ var graphics = self.attachAsset('snowflake', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ self.y += self.speed;
+ self.x += self.drift;
+ if (self.y > 2732 + 50) {
+ self.y = -50;
+ self.x = Math.random() * 2048;
+ }
+ if (self.x < -50) {
+ self.x = 2048 + 50;
+ } else if (self.x > 2048 + 50) {
+ self.x = -50;
+ }
+ };
+ return self;
+});
+var SnowmanBody = Container.expand(function () {
+ var self = Container.call(this);
+ self.hasHead = false;
+ self.accessories = [];
+ var bodyGraphics = self.attachAsset('snowman_body', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ self.addHead = function (head) {
+ if (!self.hasHead) {
+ head.x = 0;
+ head.y = -200;
+ self.addChild(head);
+ self.hasHead = true;
+ LK.getSound('stack').play();
+ }
+ };
+ self.addAccessory = function (accessory) {
+ self.accessories.push(accessory);
+ self.addChild(accessory);
+ LK.getSound('accessory').play();
+ };
+ return self;
+});
+var SnowmanHead = Container.expand(function () {
+ var self = Container.call(this);
+ var headGraphics = self.attachAsset('snowman_head', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.down = function (x, y, obj) {
+ draggedObject = self;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game state
+var gamePhase = 'rolling'; // 'rolling', 'building', 'decorating', 'completed'
+var draggedObject = null;
+var snowballs = [];
+var snowman = null;
+var accessories = [];
+var snowflakes = [];
+var shakeCount = 0;
+var isSnowing = false;
+// Ground
+var ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 1,
+ x: 0,
+ y: 2732
+}));
+// Instructions
+var instructionText = new Text2('Drag snowballs to roll them bigger!', {
+ size: 60,
+ fill: 0x000000
+});
+instructionText.anchor.set(0.5, 0);
+LK.gui.top.addChild(instructionText);
+// Create initial snowballs
+for (var i = 0; i < 3; i++) {
+ var snowball = new Snowball();
+ snowball.x = 300 + i * 400;
+ snowball.y = 2400;
+ snowballs.push(snowball);
+ game.addChild(snowball);
+}
+// Create accessories (initially hidden)
+var accessoryTypes = ['hat', 'broom', 'button', 'carrot'];
+for (var j = 0; j < accessoryTypes.length; j++) {
+ var accessory = new Accessory(accessoryTypes[j]);
+ accessory.x = 200 + j * 400;
+ accessory.y = 2600;
+ accessory.alpha = 0;
+ accessories.push(accessory);
+ game.addChild(accessory);
+}
+function checkSnowballSizes() {
+ var largeBalls = 0;
+ var mediumBalls = 0;
+ for (var i = 0; i < snowballs.length; i++) {
+ if (snowballs[i].size >= 250) {
+ largeBalls++;
+ } else if (snowballs[i].size >= 150) {
+ mediumBalls++;
+ }
+ }
+ if (largeBalls >= 1 && mediumBalls >= 1 && gamePhase === 'rolling') {
+ gamePhase = 'building';
+ instructionText.setText('Stack a smaller snowball on top of a larger one!');
+ }
+}
+function createSnowman(bodyBall, headBall) {
+ snowman = new SnowmanBody();
+ snowman.x = bodyBall.x;
+ snowman.y = bodyBall.y;
+ game.addChild(snowman);
+ // Remove original snowballs
+ bodyBall.destroy();
+ headBall.destroy();
+ // Remove from arrays
+ var bodyIndex = snowballs.indexOf(bodyBall);
+ var headIndex = snowballs.indexOf(headBall);
+ if (bodyIndex > -1) snowballs.splice(bodyIndex, 1);
+ if (headIndex > -1) snowballs.splice(headIndex, 1);
+ // Add head to snowman
+ var head = new SnowmanHead();
+ snowman.addHead(head);
+ gamePhase = 'decorating';
+ instructionText.setText('Drag accessories to decorate your snowman!');
+ // Show accessories
+ for (var i = 0; i < accessories.length; i++) {
+ tween(accessories[i], {
+ alpha: 1
+ }, {
+ duration: 500
+ });
+ }
+}
+function checkAccessoryPlacement(accessory) {
+ if (!snowman) return;
+ var distance = Math.sqrt(Math.pow(accessory.x - snowman.x, 2) + Math.pow(accessory.y - snowman.y, 2));
+ if (distance < 200) {
+ // Place accessory on snowman
+ var localPos = snowman.toLocal({
+ x: accessory.x,
+ y: accessory.y
+ });
+ accessory.x = localPos.x;
+ accessory.y = localPos.y;
+ accessory.placed = true;
+ snowman.addAccessory(accessory);
+ // Position specific accessories
+ if (accessory.type === 'hat') {
+ accessory.x = 0;
+ accessory.y = -250;
+ } else if (accessory.type === 'carrot') {
+ accessory.x = 0;
+ accessory.y = -200;
+ } else if (accessory.type === 'button') {
+ accessory.x = 0;
+ accessory.y = -100;
+ } else if (accessory.type === 'broom') {
+ accessory.x = 120;
+ accessory.y = -100;
+ }
+ // Check if all accessories are placed
+ var placedCount = 0;
+ for (var i = 0; i < accessories.length; i++) {
+ if (accessories[i].placed) placedCount++;
+ }
+ if (placedCount === accessories.length) {
+ gamePhase = 'completed';
+ instructionText.setText('Shake your device 10 times to make it snow!');
+ LK.getSound('complete').play();
+ }
+ }
+}
+function startSnowfall() {
+ if (isSnowing) return;
+ isSnowing = true;
+ instructionText.setText('Beautiful! Your snowman is complete!');
+ // Create snowflakes
+ for (var i = 0; i < 50; i++) {
+ var snowflake = new Snowflake();
+ snowflake.x = Math.random() * 2048;
+ snowflake.y = Math.random() * -500;
+ snowflakes.push(snowflake);
+ game.addChild(snowflake);
+ }
+}
+// Game event handlers
+game.move = function (x, y, obj) {
+ if (draggedObject) {
+ draggedObject.x = x;
+ draggedObject.y = y;
+ // Handle snowball rolling
+ if (draggedObject instanceof Snowball && draggedObject.isRolling) {
+ var distance = Math.sqrt(Math.pow(x - draggedObject.lastX, 2) + Math.pow(y - draggedObject.lastY, 2));
+ if (distance > 10) {
+ draggedObject.rollDistance += distance;
+ if (draggedObject.rollDistance > 50) {
+ draggedObject.grow(2);
+ draggedObject.rollDistance = 0;
+ }
+ }
+ draggedObject.lastX = x;
+ draggedObject.lastY = y;
+ }
+ }
+};
+game.up = function (x, y, obj) {
+ if (draggedObject) {
+ if (draggedObject instanceof Snowball) {
+ draggedObject.isRolling = false;
+ // Check for stacking
+ if (gamePhase === 'building') {
+ for (var i = 0; i < snowballs.length; i++) {
+ var otherBall = snowballs[i];
+ if (otherBall !== draggedObject) {
+ var distance = Math.sqrt(Math.pow(draggedObject.x - otherBall.x, 2) + Math.pow(draggedObject.y - otherBall.y, 2));
+ if (distance < 150) {
+ // Determine which is larger
+ if (draggedObject.size < otherBall.size) {
+ createSnowman(otherBall, draggedObject);
+ } else {
+ createSnowman(draggedObject, otherBall);
+ }
+ break;
+ }
+ }
+ }
+ }
+ } else if (draggedObject instanceof Accessory) {
+ checkAccessoryPlacement(draggedObject);
+ }
+ draggedObject = null;
+ }
+};
+// Shake detection (simulated with rapid mouse movements)
+var lastShakeTime = 0;
+var shakeThreshold = 100;
+game.update = function () {
+ // Check snowball sizes
+ if (gamePhase === 'rolling') {
+ checkSnowballSizes();
+ }
+ // Simple shake detection simulation
+ if (gamePhase === 'completed' && !isSnowing) {
+ // Simulate shake with rapid movements or random trigger
+ if (LK.ticks % 180 === 0) {
+ // Every 3 seconds for demo
+ shakeCount++;
+ if (shakeCount >= 10) {
+ startSnowfall();
+ } else {
+ instructionText.setText('Shake ' + (10 - shakeCount) + ' more times to make it snow!');
+ }
+ }
+ }
+};
\ No newline at end of file