User prompt
they cant hang on the air
User prompt
add pigs to physics
User prompt
put pig up
User prompt
put the pig 5 up
User prompt
delete all pıgs just one pig
User prompt
add physics to wood blocks
User prompt
add
User prompt
pigs cant go inside the woods
User prompt
pigs on the woods
User prompt
woods are blocks can stand on or crassh
User prompt
not in wood.on the wood
User prompt
put pig on wood
User prompt
pigss will not die when they fall from short range
User prompt
woods are a block
User prompt
add physics for pigs
User prompt
up
User prompt
up
User prompt
a little down
User prompt
put the sling down left
User prompt
birds should not go towards the pigs.no tracking.
User prompt
add physics for birds
User prompt
put pigs on the woods
User prompt
there a error when ı start ım winning ınstanly
User prompt
put pigs
Code edit (1 edits merged)
Please save this source code
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Bird class var Bird = Container.expand(function () { var self = Container.call(this); // Attach a red ellipse as the bird var birdAsset = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); // Physics properties self.vx = 0; self.vy = 0; self.launched = false; self.radius = birdAsset.width / 2; self.gravity = 0.7; self.friction = 0.995; self.bounce = 0.5; self.stopped = false; // Called every tick self.update = function () { if (self.launched && !self.stopped) { self.vy += self.gravity; self.x += self.vx; self.y += self.vy; // Friction self.vx *= self.friction; self.vy *= self.friction; // Floor collision if (self.y + self.radius > groundY) { self.y = groundY - self.radius; if (Math.abs(self.vy) > 2) { self.vy = -self.vy * self.bounce; self.vx *= 0.8; } else { self.vy = 0; self.vx *= 0.8; if (Math.abs(self.vx) < 1) { self.stopped = true; } } } // Wall collision if (self.x - self.radius < 0) { self.x = self.radius; self.vx = -self.vx * self.bounce; } if (self.x + self.radius > 2048) { self.x = 2048 - self.radius; self.vx = -self.vx * self.bounce; } } }; // Simple circle collision self.intersectsCircle = function (other) { var dx = self.x - other.x; var dy = self.y - other.y; var dist = Math.sqrt(dx * dx + dy * dy); return dist < self.radius + other.radius; }; return self; }); // Block class (rectangular, can be horizontal or vertical) var Block = Container.expand(function () { var self = Container.call(this); // Attach a brown box as the block var blockAsset = self.attachAsset('block', { anchorX: 0.5, anchorY: 0.5 }); self.width = blockAsset.width; self.height = blockAsset.height; self.vx = 0; self.vy = 0; self.gravity = 0.7; self.friction = 0.98; self.bounce = 0.3; self.stopped = false; // Called every tick self.update = function () { if (!self.stopped) { self.vy += self.gravity; self.x += self.vx; self.y += self.vy; self.vx *= self.friction; self.vy *= self.friction; // Floor collision if (self.y + self.height / 2 > groundY) { self.y = groundY - self.height / 2; if (Math.abs(self.vy) > 2) { self.vy = -self.vy * self.bounce; self.vx *= 0.8; } else { self.vy = 0; self.vx *= 0.8; if (Math.abs(self.vx) < 1) { self.stopped = true; } } } // Wall collision if (self.x - self.width / 2 < 0) { self.x = self.width / 2; self.vx = -self.vx * self.bounce; } if (self.x + self.width / 2 > 2048) { self.x = 2048 - self.width / 2; self.vx = -self.vx * self.bounce; } } }; // Simple rectangle collision with circle (bird) self.intersectsCircle = function (circle) { var cx = circle.x; var cy = circle.y; var rx = self.x; var ry = self.y; var hw = self.width / 2; var hh = self.height / 2; // Closest point on rectangle to circle center var closestX = Math.max(rx - hw, Math.min(cx, rx + hw)); var closestY = Math.max(ry - hh, Math.min(cy, ry + hh)); var dx = cx - closestX; var dy = cy - closestY; return dx * dx + dy * dy < circle.radius * circle.radius; }; return self; }); // Pig class var Pig = Container.expand(function () { var self = Container.call(this); // Attach a green ellipse as the pig var pigAsset = self.attachAsset('pig', { anchorX: 0.5, anchorY: 0.5 }); self.radius = pigAsset.width / 2; self.alive = true; // Called every tick self.update = function () { // No movement for pigs }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Sky blue }); /**** * Game Code ****/ // --- Game Variables --- // --- Asset Initialization (shapes) --- var birds = []; var pigs = []; var blocks = []; var currentBird = null; var birdIndex = 0; var isDragging = false; var dragStart = { x: 0, y: 0 }; var dragEnd = { x: 0, y: 0 }; var slingshotX = 350; var slingshotY = 1800; var slingshotBandLength = 180; var maxBirds = 3; var groundY = 2650; var level = 1; var launching = false; var score = 0; // --- GUI --- var scoreTxt = new Text2('0', { size: 120, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var birdsLeftTxt = new Text2('', { size: 80, fill: 0xFFFFFF }); birdsLeftTxt.anchor.set(0.5, 0); LK.gui.topRight.addChild(birdsLeftTxt); // --- Draw Ground --- var ground = LK.getAsset('ground', { anchorX: 0, anchorY: 0 }); ground.x = 0; ground.y = groundY; game.addChild(ground); // --- Draw Slingshot --- var slingshot = LK.getAsset('slingshot', { anchorX: 0.5, anchorY: 1 }); slingshot.x = slingshotX; slingshot.y = slingshotY + 90; game.addChild(slingshot); // --- Level Setup --- function setupLevel() { // Clear previous for (var i = 0; i < birds.length; i++) birds[i].destroy(); for (var i = 0; i < pigs.length; i++) pigs[i].destroy(); for (var i = 0; i < blocks.length; i++) blocks[i].destroy(); birds = []; pigs = []; blocks = []; birdIndex = 0; score = 0; LK.setScore(0); scoreTxt.setText('0'); launching = false; // Place birds for (var i = 0; i < maxBirds; i++) { var bird = new Bird(); bird.x = slingshotX - 120 - i * 80; bird.y = slingshotY + 40; birds.push(bird); game.addChild(bird); } currentBird = birds[birdIndex]; // Place pigs (simple arrangement for MVP) var pig1 = new Pig(); pig1.x = 1500; pig1.y = groundY - 40; pigs.push(pig1); game.addChild(pig1); // Place blocks (simple structure) var block1 = new Block(); block1.x = 1500; block1.y = groundY - 100; blocks.push(block1); game.addChild(block1); var block2 = new Block(); block2.x = 1500 - 100; block2.y = groundY - 60; blocks.push(block2); game.addChild(block2); var block3 = new Block(); block3.x = 1500 + 100; block3.y = groundY - 60; blocks.push(block3); game.addChild(block3); updateBirdsLeft(); } // --- Update birds left text --- function updateBirdsLeft() { birdsLeftTxt.setText('Birds: ' + (maxBirds - birdIndex)); } // --- Drag and Launch Logic --- function getDistance(x1, y1, x2, y2) { var dx = x2 - x1; var dy = y2 - y1; return Math.sqrt(dx * dx + dy * dy); } function getAngle(x1, y1, x2, y2) { return Math.atan2(y2 - y1, x2 - x1); } // Only allow drag if not launched game.down = function (x, y, obj) { if (launching) return; if (!currentBird || currentBird.launched) return; // Only allow drag if touch is near the bird var dist = getDistance(x, y, currentBird.x, currentBird.y); if (dist < currentBird.radius * 1.5) { isDragging = true; dragStart.x = x; dragStart.y = y; } }; game.move = function (x, y, obj) { if (isDragging && currentBird && !currentBird.launched) { // Limit drag distance var dx = x - slingshotX; var dy = y - slingshotY; var dist = getDistance(x, y, slingshotX, slingshotY); if (dist > slingshotBandLength) { var angle = getAngle(slingshotX, slingshotY, x, y); dx = Math.cos(angle) * slingshotBandLength; dy = Math.sin(angle) * slingshotBandLength; } currentBird.x = slingshotX + dx; currentBird.y = slingshotY + dy; dragEnd.x = currentBird.x; dragEnd.y = currentBird.y; } }; game.up = function (x, y, obj) { if (isDragging && currentBird && !currentBird.launched) { isDragging = false; // Calculate launch velocity var dx = slingshotX - currentBird.x; var dy = slingshotY - currentBird.y; currentBird.vx = dx * 0.18; currentBird.vy = dy * 0.18; currentBird.launched = true; launching = true; // Animate slingshot back tween(currentBird, { x: currentBird.x, y: currentBird.y }, { duration: 80 }); } }; // --- Main Game Loop --- game.update = function () { // Update all birds for (var i = 0; i < birds.length; i++) { birds[i].update(); } // Update all pigs for (var i = 0; i < pigs.length; i++) { pigs[i].update(); } // Update all blocks for (var i = 0; i < blocks.length; i++) { blocks[i].update(); } // Bird-block collision for (var i = 0; i < birds.length; i++) { var bird = birds[i]; if (!bird.launched) continue; if (bird.stopped) continue; // Collide with blocks for (var j = 0; j < blocks.length; j++) { var block = blocks[j]; if (block.stopped) continue; if (block.intersectsCircle(bird)) { // Simple response: knock block, slow bird var angle = getAngle(bird.x, bird.y, block.x, block.y); var force = Math.sqrt(bird.vx * bird.vx + bird.vy * bird.vy) * 0.5; block.vx += Math.cos(angle) * force * 0.7; block.vy += Math.sin(angle) * force * 0.7; bird.vx *= 0.6; bird.vy *= 0.6; } } } // Bird-pig collision for (var i = 0; i < birds.length; i++) { var bird = birds[i]; if (!bird.launched) continue; if (bird.stopped) continue; for (var j = 0; j < pigs.length; j++) { var pig = pigs[j]; if (!pig.alive) continue; if (bird.intersectsCircle(pig)) { pig.alive = false; pig.visible = false; LK.setScore(LK.getScore() + 1000); scoreTxt.setText(LK.getScore()); } } } // Block-pig collision (falling blocks can kill pigs) for (var i = 0; i < blocks.length; i++) { var block = blocks[i]; if (block.stopped) continue; for (var j = 0; j < pigs.length; j++) { var pig = pigs[j]; if (!pig.alive) continue; // Treat pig as circle, block as rectangle if (block.intersectsCircle(pig)) { pig.alive = false; pig.visible = false; LK.setScore(LK.getScore() + 1000); scoreTxt.setText(LK.getScore()); } } } // Remove dead pigs var allPigsDead = true; for (var i = 0; i < pigs.length; i++) { if (pigs[i].alive) { allPigsDead = false; break; } } // If all pigs dead, win if (allPigsDead) { LK.showYouWin(); return; } // If current bird stopped, move to next bird if (currentBird && currentBird.launched && currentBird.stopped && !isDragging && launching) { birdIndex++; updateBirdsLeft(); if (birdIndex < birds.length) { currentBird = birds[birdIndex]; // Move next bird to slingshot tween(currentBird, { x: slingshotX, y: slingshotY }, { duration: 300, easing: tween.easeOut, onFinish: function onFinish() { launching = false; } }); } else { // Out of birds, game over LK.showGameOver(); return; } launching = false; } }; // --- Start Game --- setupLevel();
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
// Attach a red ellipse as the bird
var birdAsset = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
// Physics properties
self.vx = 0;
self.vy = 0;
self.launched = false;
self.radius = birdAsset.width / 2;
self.gravity = 0.7;
self.friction = 0.995;
self.bounce = 0.5;
self.stopped = false;
// Called every tick
self.update = function () {
if (self.launched && !self.stopped) {
self.vy += self.gravity;
self.x += self.vx;
self.y += self.vy;
// Friction
self.vx *= self.friction;
self.vy *= self.friction;
// Floor collision
if (self.y + self.radius > groundY) {
self.y = groundY - self.radius;
if (Math.abs(self.vy) > 2) {
self.vy = -self.vy * self.bounce;
self.vx *= 0.8;
} else {
self.vy = 0;
self.vx *= 0.8;
if (Math.abs(self.vx) < 1) {
self.stopped = true;
}
}
}
// Wall collision
if (self.x - self.radius < 0) {
self.x = self.radius;
self.vx = -self.vx * self.bounce;
}
if (self.x + self.radius > 2048) {
self.x = 2048 - self.radius;
self.vx = -self.vx * self.bounce;
}
}
};
// Simple circle collision
self.intersectsCircle = function (other) {
var dx = self.x - other.x;
var dy = self.y - other.y;
var dist = Math.sqrt(dx * dx + dy * dy);
return dist < self.radius + other.radius;
};
return self;
});
// Block class (rectangular, can be horizontal or vertical)
var Block = Container.expand(function () {
var self = Container.call(this);
// Attach a brown box as the block
var blockAsset = self.attachAsset('block', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = blockAsset.width;
self.height = blockAsset.height;
self.vx = 0;
self.vy = 0;
self.gravity = 0.7;
self.friction = 0.98;
self.bounce = 0.3;
self.stopped = false;
// Called every tick
self.update = function () {
if (!self.stopped) {
self.vy += self.gravity;
self.x += self.vx;
self.y += self.vy;
self.vx *= self.friction;
self.vy *= self.friction;
// Floor collision
if (self.y + self.height / 2 > groundY) {
self.y = groundY - self.height / 2;
if (Math.abs(self.vy) > 2) {
self.vy = -self.vy * self.bounce;
self.vx *= 0.8;
} else {
self.vy = 0;
self.vx *= 0.8;
if (Math.abs(self.vx) < 1) {
self.stopped = true;
}
}
}
// Wall collision
if (self.x - self.width / 2 < 0) {
self.x = self.width / 2;
self.vx = -self.vx * self.bounce;
}
if (self.x + self.width / 2 > 2048) {
self.x = 2048 - self.width / 2;
self.vx = -self.vx * self.bounce;
}
}
};
// Simple rectangle collision with circle (bird)
self.intersectsCircle = function (circle) {
var cx = circle.x;
var cy = circle.y;
var rx = self.x;
var ry = self.y;
var hw = self.width / 2;
var hh = self.height / 2;
// Closest point on rectangle to circle center
var closestX = Math.max(rx - hw, Math.min(cx, rx + hw));
var closestY = Math.max(ry - hh, Math.min(cy, ry + hh));
var dx = cx - closestX;
var dy = cy - closestY;
return dx * dx + dy * dy < circle.radius * circle.radius;
};
return self;
});
// Pig class
var Pig = Container.expand(function () {
var self = Container.call(this);
// Attach a green ellipse as the pig
var pigAsset = self.attachAsset('pig', {
anchorX: 0.5,
anchorY: 0.5
});
self.radius = pigAsset.width / 2;
self.alive = true;
// Called every tick
self.update = function () {
// No movement for pigs
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue
});
/****
* Game Code
****/
// --- Game Variables ---
// --- Asset Initialization (shapes) ---
var birds = [];
var pigs = [];
var blocks = [];
var currentBird = null;
var birdIndex = 0;
var isDragging = false;
var dragStart = {
x: 0,
y: 0
};
var dragEnd = {
x: 0,
y: 0
};
var slingshotX = 350;
var slingshotY = 1800;
var slingshotBandLength = 180;
var maxBirds = 3;
var groundY = 2650;
var level = 1;
var launching = false;
var score = 0;
// --- GUI ---
var scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var birdsLeftTxt = new Text2('', {
size: 80,
fill: 0xFFFFFF
});
birdsLeftTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(birdsLeftTxt);
// --- Draw Ground ---
var ground = LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
});
ground.x = 0;
ground.y = groundY;
game.addChild(ground);
// --- Draw Slingshot ---
var slingshot = LK.getAsset('slingshot', {
anchorX: 0.5,
anchorY: 1
});
slingshot.x = slingshotX;
slingshot.y = slingshotY + 90;
game.addChild(slingshot);
// --- Level Setup ---
function setupLevel() {
// Clear previous
for (var i = 0; i < birds.length; i++) birds[i].destroy();
for (var i = 0; i < pigs.length; i++) pigs[i].destroy();
for (var i = 0; i < blocks.length; i++) blocks[i].destroy();
birds = [];
pigs = [];
blocks = [];
birdIndex = 0;
score = 0;
LK.setScore(0);
scoreTxt.setText('0');
launching = false;
// Place birds
for (var i = 0; i < maxBirds; i++) {
var bird = new Bird();
bird.x = slingshotX - 120 - i * 80;
bird.y = slingshotY + 40;
birds.push(bird);
game.addChild(bird);
}
currentBird = birds[birdIndex];
// Place pigs (simple arrangement for MVP)
var pig1 = new Pig();
pig1.x = 1500;
pig1.y = groundY - 40;
pigs.push(pig1);
game.addChild(pig1);
// Place blocks (simple structure)
var block1 = new Block();
block1.x = 1500;
block1.y = groundY - 100;
blocks.push(block1);
game.addChild(block1);
var block2 = new Block();
block2.x = 1500 - 100;
block2.y = groundY - 60;
blocks.push(block2);
game.addChild(block2);
var block3 = new Block();
block3.x = 1500 + 100;
block3.y = groundY - 60;
blocks.push(block3);
game.addChild(block3);
updateBirdsLeft();
}
// --- Update birds left text ---
function updateBirdsLeft() {
birdsLeftTxt.setText('Birds: ' + (maxBirds - birdIndex));
}
// --- Drag and Launch Logic ---
function getDistance(x1, y1, x2, y2) {
var dx = x2 - x1;
var dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
function getAngle(x1, y1, x2, y2) {
return Math.atan2(y2 - y1, x2 - x1);
}
// Only allow drag if not launched
game.down = function (x, y, obj) {
if (launching) return;
if (!currentBird || currentBird.launched) return;
// Only allow drag if touch is near the bird
var dist = getDistance(x, y, currentBird.x, currentBird.y);
if (dist < currentBird.radius * 1.5) {
isDragging = true;
dragStart.x = x;
dragStart.y = y;
}
};
game.move = function (x, y, obj) {
if (isDragging && currentBird && !currentBird.launched) {
// Limit drag distance
var dx = x - slingshotX;
var dy = y - slingshotY;
var dist = getDistance(x, y, slingshotX, slingshotY);
if (dist > slingshotBandLength) {
var angle = getAngle(slingshotX, slingshotY, x, y);
dx = Math.cos(angle) * slingshotBandLength;
dy = Math.sin(angle) * slingshotBandLength;
}
currentBird.x = slingshotX + dx;
currentBird.y = slingshotY + dy;
dragEnd.x = currentBird.x;
dragEnd.y = currentBird.y;
}
};
game.up = function (x, y, obj) {
if (isDragging && currentBird && !currentBird.launched) {
isDragging = false;
// Calculate launch velocity
var dx = slingshotX - currentBird.x;
var dy = slingshotY - currentBird.y;
currentBird.vx = dx * 0.18;
currentBird.vy = dy * 0.18;
currentBird.launched = true;
launching = true;
// Animate slingshot back
tween(currentBird, {
x: currentBird.x,
y: currentBird.y
}, {
duration: 80
});
}
};
// --- Main Game Loop ---
game.update = function () {
// Update all birds
for (var i = 0; i < birds.length; i++) {
birds[i].update();
}
// Update all pigs
for (var i = 0; i < pigs.length; i++) {
pigs[i].update();
}
// Update all blocks
for (var i = 0; i < blocks.length; i++) {
blocks[i].update();
}
// Bird-block collision
for (var i = 0; i < birds.length; i++) {
var bird = birds[i];
if (!bird.launched) continue;
if (bird.stopped) continue;
// Collide with blocks
for (var j = 0; j < blocks.length; j++) {
var block = blocks[j];
if (block.stopped) continue;
if (block.intersectsCircle(bird)) {
// Simple response: knock block, slow bird
var angle = getAngle(bird.x, bird.y, block.x, block.y);
var force = Math.sqrt(bird.vx * bird.vx + bird.vy * bird.vy) * 0.5;
block.vx += Math.cos(angle) * force * 0.7;
block.vy += Math.sin(angle) * force * 0.7;
bird.vx *= 0.6;
bird.vy *= 0.6;
}
}
}
// Bird-pig collision
for (var i = 0; i < birds.length; i++) {
var bird = birds[i];
if (!bird.launched) continue;
if (bird.stopped) continue;
for (var j = 0; j < pigs.length; j++) {
var pig = pigs[j];
if (!pig.alive) continue;
if (bird.intersectsCircle(pig)) {
pig.alive = false;
pig.visible = false;
LK.setScore(LK.getScore() + 1000);
scoreTxt.setText(LK.getScore());
}
}
}
// Block-pig collision (falling blocks can kill pigs)
for (var i = 0; i < blocks.length; i++) {
var block = blocks[i];
if (block.stopped) continue;
for (var j = 0; j < pigs.length; j++) {
var pig = pigs[j];
if (!pig.alive) continue;
// Treat pig as circle, block as rectangle
if (block.intersectsCircle(pig)) {
pig.alive = false;
pig.visible = false;
LK.setScore(LK.getScore() + 1000);
scoreTxt.setText(LK.getScore());
}
}
}
// Remove dead pigs
var allPigsDead = true;
for (var i = 0; i < pigs.length; i++) {
if (pigs[i].alive) {
allPigsDead = false;
break;
}
}
// If all pigs dead, win
if (allPigsDead) {
LK.showYouWin();
return;
}
// If current bird stopped, move to next bird
if (currentBird && currentBird.launched && currentBird.stopped && !isDragging && launching) {
birdIndex++;
updateBirdsLeft();
if (birdIndex < birds.length) {
currentBird = birds[birdIndex];
// Move next bird to slingshot
tween(currentBird, {
x: slingshotX,
y: slingshotY
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
launching = false;
}
});
} else {
// Out of birds, game over
LK.showGameOver();
return;
}
launching = false;
}
};
// --- Start Game ---
setupLevel();