xxxxxxxxxx
108
// "Moderately Annoyed Bird" (name by @GilmoreLion)
// artwork by @itsnotaboutwork
// remixed by @jmutterer from:
// Angry Birds
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/138-angry-birds.html
// https://youtu.be/TDQzoe9nslY
const { Engine, World, Bodies, Mouse, MouseConstraint, Constraint } = Matter;
let ground;
const boxes = [];
let bird;
let world, engine;
let mConstraint;
let slingshot;
let dotImg;
let boxImg;
let bkgImg;
let tImg;
function preload() {
dotImg = loadImage("image1727.png");
boxImg = loadImage("path918.png");
tImg = loadImage("image1072.png");
bkgImg = loadImage("image2074.png");
slings = loadImage("image1891.png");
}
function setup() {
const canvas = createCanvas(350, 400);
engine = Engine.create();
world = engine.world;
ground = new Ground(width / 2, height - 10, width, 20);
for (let i = 0; i < 4; i++) {
boxes[i] = new Box(300, 300 - i * 75, 60, 80, i < 3);
}
bird = new Bird(150, 300, 25);
slingshot = new SlingShot(120, 250, bird.body);
const mouse = Mouse.create(canvas.elt);
const options = {
mouse: mouse,
};
// A fix for HiDPI displays
mouse.pixelRatio = pixelDensity();
mConstraint = MouseConstraint.create(engine, options);
World.add(world, mConstraint);
button = createButton("new bird");
button.position(19, 19);
button.mousePressed(newBird);
button2 = createButton("restart");
button2.position(100, 19);
button2.mousePressed(setup);
}
function newBird() {
World.remove(world, bird.body);
bird = new Bird(150, 300, 25);
slingshot.attach(bird.body);
}
function keyPressed() {
if (key == " ") {
newBird();
}
if (key == "r") {
setup();
}
}
function mouseReleased() {
if (mouseY > 50) {
setTimeout(() => {
slingshot.fly();
}, 100);
}
}
function draw() {
background(255);
background(bkgImg);
push();
translate(0, width);
rotate(-PI / 2);
textSize(10);
textAlign(LEFT, TOP);
strokeWeight(0.3);
noStroke();
fill(255);
rect(-25, 0, height - 120, 10);
fill(128);
text("Art: @itsnotaboutwork | code: @shiffman | remix: @jmutterer", -25, 0);
pop();
image(slings, 25, 200, 200, 200);
Matter.Engine.update(engine);
ground.show();
for (let box of boxes) {
box.show();
}
slingshot.show();
bird.show();
}