Please help this is my original code and when the user claps the bird goes up and down:
Once we added a background image as the background the code stopped working:
PLEASE HELP
//Good Code:
var bird;
var pipes = [];
var mic;
var sliderTop;
var sliderBottom;
var clapping = false;
//var landscape;
function setup() {
createCanvas(400, 600);
// landscape = loadImage("big boy images.jpeg");
mic = new p5.AudioIn();
mic.start();
bird = new Bird();
pipes.push(new Pipe());
sliderTop = createSlider(0, .1, 0.3, 0.01);
sliderBottom = createSlider(0, 1, 0.1, 0.01);
}
function draw() {
background(0);
var vol = mic.getLevel();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].show();
pipes[i].update();
if (pipes[i].hits(bird)) {
console.log("HIT");
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
bird.update();
bird.show();
if (frameCount % 100 == 0) {
pipes.push(new Pipe());
}
var thresholdTop = sliderTop.value();
var thresholdBottom = sliderBottom.value();
if (vol > thresholdTop && !clapping) {
bird.up();
clapping = true;
}
if (vol < thresholdBottom) {
clapping = false;
}
//fill(0, 255, 0);
//console.log(vol);
//noStroke();
//var y = map(vol, 0, 1, height, 0);
//rect(width - 50, y, 50, height - y);
//var ty = map(thresholdTop, 0, 1, height, 0);
//stroke(255, 0, 0);
//strokeWeight(4);
//line(width - 50, ty, width, ty);
//var by = map(thresholdBottom, 0, 1, height, 0);
//stroke(0, 0, 255);
//strokeWeight(4);
//line(width - 50, by, width, by);
}
function keyPressed() {
if (key == ' ') {
bird.up();
//console.log("SPACE");
}
}
function Bird() {
this.y = height / 2;
this.x = 64;
this.gravity = 0.6;
this.lift = -15;
this.velocity = 0;
this.show = function() {
fill(246,255,0);
noStroke();
ellipse(this.x, this.y, 32, 32);
}
this.up = function() {
this.velocity += this.lift;
}
this.update = function() {
this.velocity += this.gravity;
this.velocity *= 0.9;
this.y += this.velocity;
if (this.y > height) {
this.y = height;
this.velocity = 0;
}
if (this.y < 0) {
this.y = 0;
this.velocity = 0;
}
}
}
function Pipe() {
var spacing = random(150, height / 2);
var centery = random(spacing, height - spacing);
this.top = centery - spacing / 2;
this.bottom = height - (centery + spacing / 2);
this.x = width;
this.w = 50;
this.speed = 2;
this.highlight = false;
this.hits = function(bird) {
if (bird.y < this.top || bird.y > height - this.bottom) {
if (bird.x > this.x && bird.x < this.x + this.w) {
this.highlight = true;
return true;
}
}
this.highlight = false;
return false;
}
this.show = function() {
noStroke();
fill(0,255,0);
if (this.highlight) {
fill(255, 0, 0);
}
rect(this.x, 0, this.w, this.top);
rect(this.x, height - this.bottom, this.w, this.bottom);
}
this.update = function() {
this.x -= this.speed;
}
this.offscreen = function() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
}
//Bad Code:
var bird;
var pipes = [];
var mic;
var sliderTop;
var sliderBottom;
var clapping = false;
var landscape;
function setup() {
createCanvas(400, 600);
landscape = loadImage("big boy images.jpeg");
mic = new p5.AudioIn();
mic.start();
bird = new Bird();
pipes.push(new Pipe());
sliderTop = createSlider(0, .1, 0.3, 0.01);
sliderBottom = createSlider(0, 1, 0.1, 0.01);
}
function draw() {
background(landscape);
var vol = mic.getLevel();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].show();
pipes[i].update();
if (pipes[i].hits(bird)) {
console.log("HIT");
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
bird.update();
bird.show();
if (frameCount % 100 == 0) {
pipes.push(new Pipe());
}
var thresholdTop = sliderTop.value();
var thresholdBottom = sliderBottom.value();
if (vol > thresholdTop && !clapping) {
bird.up();
clapping = true;
}
if (vol < thresholdBottom) {
clapping = false;
}
//fill(0, 255, 0);
//console.log(vol);
//noStroke();
//var y = map(vol, 0, 1, height, 0);
//rect(width - 50, y, 50, height - y);
//var ty = map(thresholdTop, 0, 1, height, 0);
//stroke(255, 0, 0);
//strokeWeight(4);
//line(width - 50, ty, width, ty);
//var by = map(thresholdBottom, 0, 1, height, 0);
//stroke(0, 0, 255);
//strokeWeight(4);
//line(width - 50, by, width, by);
}
function keyPressed() {
if (key == ' ') {
bird.up();
//console.log("SPACE");
}
}
function Bird() {
this.y = height / 2;
this.x = 64;
this.gravity = 0.6;
this.lift = -15;
this.velocity = 0;
this.show = function() {
fill(246,255,0);
noStroke();
ellipse(this.x, this.y, 32, 32);
}
this.up = function() {
this.velocity += this.lift;
}
this.update = function() {
this.velocity += this.gravity;
this.velocity *= 0.9;
this.y += this.velocity;
if (this.y > height) {
this.y = height;
this.velocity = 0;
}
if (this.y < 0) {
this.y = 0;
this.velocity = 0;
}
}
}
function Pipe() {
var spacing = random(150, height / 2);
var centery = random(spacing, height - spacing);
this.top = centery - spacing / 2;
this.bottom = height - (centery + spacing / 2);
this.x = width;
this.w = 50;
this.speed = 2;
this.highlight = false;
this.hits = function(bird) {
if (bird.y < this.top || bird.y > height - this.bottom) {
if (bird.x > this.x && bird.x < this.x + this.w) {
this.highlight = true;
return true;
}
}
this.highlight = false;
return false;
}
this.show = function() {
noStroke();
fill(0,255,0);
if (this.highlight) {
fill(255, 0, 0);
}
rect(this.x, 0, this.w, this.top);
rect(this.x, height - this.bottom, this.w, this.bottom);
}
this.update = function() {
this.x -= this.speed;
}
this.offscreen = function() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
}