// birds
// global control of how many birds
let numBirds = 50;
let birds = []; // an array that holds all of our birds!!!
// here are the arrays that keep track of all the different variables associated with each bird
let flapA = 0;
let flapSpeed = 2;
function setup() {
createCanvas(800, 200);
//strokeWeight(10);
angleMode(DEGREES); // angles are between 0 - 360;
// noLoop();
// bird factory --- use our Bird class!!! to make new birds and! to them in an array
for(let i = 0; i<numBirds;i++){
birds[i] = new Bird();
}
console.log(birds);
}
function draw() {
background(220);
for (let i = 0; i < numBirds;i++){
birds[i].show();
birds[i].flap();
birds[i].move();
birds[i].check();
}
}
// blueprint for a bird
class Bird {
// inside the constructor instructiors for what to make when! we make a new Bird!
constructor(){
this.x = random(width);
this.y = random(0,200);
this.amp = random(5,30);
this.speed = random(1,2);
this.flapA = 0;
this.flapSpeed = random(1,3);
}
// methods ! things that our bird does!
show(){
// changing the angle of each wing
let a = map(sin(this.flapA),-1,1,30,120); // sin gives values between -1 and 1;
// a should between 30 and 120;
let a1 = map(a,0,120,180,60);
strokeWeight(this.amp/5);
// left wing
let leftX = this.x - sin(a) * this.amp;
let leftY = this.y - cos(a) * this.amp;
line(leftX, leftY ,this.x,this.y);
// // right wing
let rightX = this.x + sin(a1) * this.amp;
let rightY = this.y + cos(a1) * this.amp;
line(this.x,this.y,rightX,rightY);
}
flap(){
this.flapA += this.flapSpeed;
}
move(){
this.x+=this.speed;
}
check(){
if(this.x>width+this.amp){
this.x = -this.amp;
}
}
}
// GLOBAL VARIABLES
let candy;
let spriteW,spriteH;// width and height in pixels of each candy icon or sprite INSIDE the candy image
let sheetCols = 6;
let sheetRows = 4;
// for each Candy
let candyXOffset;
let candyYOffset;
let bonbons = [];// array that will hold all the bonbon objects~!
let numBonbons = 200;
function preload () {
candy = loadImage("/assets/p5img/candy2.jpg");
}
function setup() {
createCanvas(800, 800);
// global variable that we need in order to figure out where in the image we're grabbing our candy sample (which candy!)
spriteW = candy.width/sheetCols;
spriteH = candy.height/sheetRows;
// bonbon factory
for(let i = 0; i < numBonbons;i++){
// temporary variable in the local scope of this for loop inside the setup function
let b = new Bonbon();
bonbons[i] = b;
// bonbons.push(b); // add the end of that array
}
}
function draw() {
background(220);
// iterating through an array
for(let i = 0;i<numBonbons;i++){
bonbons[i].checkHover();
bonbons[i].show();
}
}
// written the blueprint for our Bonbon
class Bonbon {
constructor(){
this.candyColsOffset = floor(random(0,sheetCols));
this.candyRowsOffset = floor(random(0,sheetRows));
this.dx = random(width); // x on the canvas
this.dy = random(height); // y on canvas
this.dWidth = 80; // width of the image on the canvas
this.dHeight = 80; // height of the image on the canvas
this.sx = this.candyColsOffset * spriteW;
this.sy = this.candyRowsOffset * spriteH;
this.sWidth = spriteW;
this.sHeight = spriteH;
this.appear = true;
}
show(){
if(this.appear == true){
image(candy,this.dx, this.dy, this.dWidth, this.dHeight, this.sx, this.sy, this.sWidth, this.sHeight);
}
}
checkHover(){
if(mouseX > this.dx && mouseX < this.dx + this.dWidth && mouseY > this.dy && mouseY < this.dy + this.dHeight && mouseIsPressed == true){
this.appear = false;
}
}
}