Code edit (1 edits merged)
Please save this source code
User prompt
Powerpuff Snowball Rolling Adventure
Initial prompt
The powerpuff girls: they have make a snow-man with a top hat and a grey scarf. But.... Tap on the giant snowball to make it roll through 9 trees. Tap on it to make it roll again. Until it reaches to the fence.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Snowball = Container.expand(function () {
var self = Container.call(this);
var snowballGraphics = self.attachAsset('snowball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.friction = 0.95;
self.isMoving = false;
self.update = function () {
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Stop if moving very slowly
if (Math.abs(self.velocityX) < 0.1 && Math.abs(self.velocityY) < 0.1) {
self.velocityX = 0;
self.velocityY = 0;
self.isMoving = false;
} else {
self.isMoving = true;
}
// Rotate based on movement
if (self.isMoving) {
snowballGraphics.rotation += 0.1;
}
// Keep within bounds
if (self.x < 100) self.x = 100;
if (self.x > 1948) self.x = 1948;
if (self.y < 100) self.y = 100;
};
self.addForce = function (forceX, forceY) {
self.velocityX += forceX;
self.velocityY += forceY;
};
return self;
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf0f8ff
});
/****
* Game Code
****/
// Game variables
var snowball;
var trees = [];
var fence;
var gameWon = false;
var lastTapTime = 0;
// Create ground
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
}));
// Create snowball
snowball = game.addChild(new Snowball());
snowball.x = 200;
snowball.y = 2400;
// Create trees in strategic positions
var treePositions = [{
x: 400,
y: 2100
}, {
x: 800,
y: 1900
}, {
x: 1200,
y: 2000
}, {
x: 600,
y: 1700
}, {
x: 1000,
y: 1500
}, {
x: 1400,
y: 1600
}, {
x: 500,
y: 1300
}, {
x: 900,
y: 1100
}, {
x: 1300,
y: 1200
}];
for (var i = 0; i < treePositions.length; i++) {
var tree = game.addChild(new Tree());
tree.x = treePositions[i].x;
tree.y = treePositions[i].y;
trees.push(tree);
}
// Create fence at the end
fence = game.addChild(LK.getAsset('fence', {
anchorX: 0.5,
anchorY: 1.0,
x: 1024,
y: 800
}));
// Score display
var instructionText = new Text2('Tap to roll the snowball to the fence!', {
size: 60,
fill: 0x2D5016
});
instructionText.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionText);
instructionText.y = 150;
// Game controls
game.down = function (x, y, obj) {
if (gameWon) return;
var currentTime = Date.now();
if (currentTime - lastTapTime < 200) return; // Prevent rapid tapping
lastTapTime = currentTime;
// Convert tap position to direction
var tapX = x;
var tapY = y;
// Calculate direction from snowball to tap
var dirX = tapX - snowball.x;
var dirY = tapY - snowball.y;
// Normalize direction
var distance = Math.sqrt(dirX * dirX + dirY * dirY);
if (distance > 0) {
dirX /= distance;
dirY /= distance;
}
// Apply force in that direction
snowball.addForce(dirX * 8, dirY * 8);
// Play roll sound
LK.getSound('roll').play();
};
// Game update loop
game.update = function () {
if (gameWon) return;
// Check collision with trees
for (var i = 0; i < trees.length; i++) {
var tree = trees[i];
if (snowball.intersects(tree)) {
// Stop the snowball
snowball.velocityX *= -0.3;
snowball.velocityY *= -0.3;
// Push snowball away from tree
var pushX = snowball.x - tree.x;
var pushY = snowball.y - tree.y;
var pushDistance = Math.sqrt(pushX * pushX + pushY * pushY);
if (pushDistance > 0) {
pushX /= pushDistance;
pushY /= pushDistance;
snowball.x = tree.x + pushX * 150;
snowball.y = tree.y + pushY * 150;
}
LK.getSound('hit').play();
LK.effects.flashObject(tree, 0xff0000, 300);
}
}
// Check if reached fence
if (snowball.y <= 900 && !gameWon) {
gameWon = true;
LK.getSound('win').play();
LK.effects.flashScreen(0x00ff00, 1000);
// Update instruction text
instructionText.setText('Success! Snowball reached the fence!');
// Add score and show win
LK.setScore(100);
LK.setTimeout(function () {
LK.showYouWin();
}, 1500);
}
// Game over if snowball gets stuck at bottom for too long
if (snowball.y > 2500 && !snowball.isMoving) {
var stuckTimer = snowball.stuckTimer || 0;
snowball.stuckTimer = stuckTimer + 1;
if (snowball.stuckTimer > 180) {
// 3 seconds at 60fps
LK.effects.flashScreen(0xff0000, 1000);
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
} else {
snowball.stuckTimer = 0;
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,209 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Snowball = Container.expand(function () {
+ var self = Container.call(this);
+ var snowballGraphics = self.attachAsset('snowball', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.friction = 0.95;
+ self.isMoving = false;
+ self.update = function () {
+ // Apply velocity
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ // Apply friction
+ self.velocityX *= self.friction;
+ self.velocityY *= self.friction;
+ // Stop if moving very slowly
+ if (Math.abs(self.velocityX) < 0.1 && Math.abs(self.velocityY) < 0.1) {
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.isMoving = false;
+ } else {
+ self.isMoving = true;
+ }
+ // Rotate based on movement
+ if (self.isMoving) {
+ snowballGraphics.rotation += 0.1;
+ }
+ // Keep within bounds
+ if (self.x < 100) self.x = 100;
+ if (self.x > 1948) self.x = 1948;
+ if (self.y < 100) self.y = 100;
+ };
+ self.addForce = function (forceX, forceY) {
+ self.velocityX += forceX;
+ self.velocityY += forceY;
+ };
+ return self;
+});
+var Tree = Container.expand(function () {
+ var self = Container.call(this);
+ var treeGraphics = self.attachAsset('tree', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0xf0f8ff
+});
+
+/****
+* Game Code
+****/
+// Game variables
+var snowball;
+var trees = [];
+var fence;
+var gameWon = false;
+var lastTapTime = 0;
+// Create ground
+var ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 0
+}));
+// Create snowball
+snowball = game.addChild(new Snowball());
+snowball.x = 200;
+snowball.y = 2400;
+// Create trees in strategic positions
+var treePositions = [{
+ x: 400,
+ y: 2100
+}, {
+ x: 800,
+ y: 1900
+}, {
+ x: 1200,
+ y: 2000
+}, {
+ x: 600,
+ y: 1700
+}, {
+ x: 1000,
+ y: 1500
+}, {
+ x: 1400,
+ y: 1600
+}, {
+ x: 500,
+ y: 1300
+}, {
+ x: 900,
+ y: 1100
+}, {
+ x: 1300,
+ y: 1200
+}];
+for (var i = 0; i < treePositions.length; i++) {
+ var tree = game.addChild(new Tree());
+ tree.x = treePositions[i].x;
+ tree.y = treePositions[i].y;
+ trees.push(tree);
+}
+// Create fence at the end
+fence = game.addChild(LK.getAsset('fence', {
+ anchorX: 0.5,
+ anchorY: 1.0,
+ x: 1024,
+ y: 800
+}));
+// Score display
+var instructionText = new Text2('Tap to roll the snowball to the fence!', {
+ size: 60,
+ fill: 0x2D5016
+});
+instructionText.anchor.set(0.5, 0);
+LK.gui.top.addChild(instructionText);
+instructionText.y = 150;
+// Game controls
+game.down = function (x, y, obj) {
+ if (gameWon) return;
+ var currentTime = Date.now();
+ if (currentTime - lastTapTime < 200) return; // Prevent rapid tapping
+ lastTapTime = currentTime;
+ // Convert tap position to direction
+ var tapX = x;
+ var tapY = y;
+ // Calculate direction from snowball to tap
+ var dirX = tapX - snowball.x;
+ var dirY = tapY - snowball.y;
+ // Normalize direction
+ var distance = Math.sqrt(dirX * dirX + dirY * dirY);
+ if (distance > 0) {
+ dirX /= distance;
+ dirY /= distance;
+ }
+ // Apply force in that direction
+ snowball.addForce(dirX * 8, dirY * 8);
+ // Play roll sound
+ LK.getSound('roll').play();
+};
+// Game update loop
+game.update = function () {
+ if (gameWon) return;
+ // Check collision with trees
+ for (var i = 0; i < trees.length; i++) {
+ var tree = trees[i];
+ if (snowball.intersects(tree)) {
+ // Stop the snowball
+ snowball.velocityX *= -0.3;
+ snowball.velocityY *= -0.3;
+ // Push snowball away from tree
+ var pushX = snowball.x - tree.x;
+ var pushY = snowball.y - tree.y;
+ var pushDistance = Math.sqrt(pushX * pushX + pushY * pushY);
+ if (pushDistance > 0) {
+ pushX /= pushDistance;
+ pushY /= pushDistance;
+ snowball.x = tree.x + pushX * 150;
+ snowball.y = tree.y + pushY * 150;
+ }
+ LK.getSound('hit').play();
+ LK.effects.flashObject(tree, 0xff0000, 300);
+ }
+ }
+ // Check if reached fence
+ if (snowball.y <= 900 && !gameWon) {
+ gameWon = true;
+ LK.getSound('win').play();
+ LK.effects.flashScreen(0x00ff00, 1000);
+ // Update instruction text
+ instructionText.setText('Success! Snowball reached the fence!');
+ // Add score and show win
+ LK.setScore(100);
+ LK.setTimeout(function () {
+ LK.showYouWin();
+ }, 1500);
+ }
+ // Game over if snowball gets stuck at bottom for too long
+ if (snowball.y > 2500 && !snowball.isMoving) {
+ var stuckTimer = snowball.stuckTimer || 0;
+ snowball.stuckTimer = stuckTimer + 1;
+ if (snowball.stuckTimer > 180) {
+ // 3 seconds at 60fps
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 1000);
+ }
+ } else {
+ snowball.stuckTimer = 0;
+ }
+};
\ No newline at end of file