xxxxxxxxxx
120
//sourced from: p5js reference library and help from Ben
let ffs = [];
let stars = [];
let img;
let img2;
let img3;
let x;
let y;
let s = 0.0;
let inTouch = false;
function preload() {
img = loadImage("grass2.png");
img2 = loadImage("firefly.png");
img3 = loadImage("jar.png");
img4 = loadImage("moon.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
for (
let i = 0; i < 100; i = i + 1) {
stars.push(new Star(random(windowWidth), random(windowHeight), 5));
}
colorMode(HSB);
for (let i = 0; i < 25; i = i + 1) {
let w = random(15, 20);
let h = w;
let ff = new Firefly(random(0, windowWidth),
random(0, windowHeight), w, h, random(-0.5, 0.5), random(-0.5, 0.5));
ffs.push(ff);
}
}
function draw() {
background(240, 75, 19);
push();
for (
let i = 0; i < stars.length; i = i + 1) {
stars[i].update();
}
pop();
image(img4, (windowWidth - 100), 100);
imageMode(CENTER);
image(img, 0, windowHeight - 100);
imageMode(CENTER);
if (inTouch)image(img3, mouseX, mouseY);
for (let i = 0; i < ffs.length; i = i + 1) {
ffs[i].update();
}
}
function touchStarted(){
inTouch = true;
return false;}
function touchEnded(){
inTouch = false;
return false;}
class Firefly {
constructor(x, y, w, h, xSpeed, ySpeed) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.H = 57;
this.S = 67;
this.B = 100;
this.found = false;
this.vx = random(-5, 5);
this.vy = random(-5, 5);
}
update() {
push();
translate(this.x, this.y);
noStroke();
fill(this.H, this.S, this.B);
//ellipse(0, 0, this.w, this.h);
image(img2, this.xSpeed, this.ySpeed);
pop();
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
if (this.x > width - this.w / 2 || this.x < this.w / 2) {
this.xSpeed = this.xSpeed * -1;
}
if (this.y > height - this.h / 2 || this.y < this.h / 2) {
this.ySpeed = this.ySpeed * -1;
}
if (mouseX > this.x - this.w && mouseX < this.x + this.w && mouseY > this.y - this.h && mouseY < this.y + this.h ) {
this.x = mouseX + this.vx;
this.y = mouseY + this.vy;
this.found = true;
}
}
}
class Star {
constructor(x, y, s) {
this.x = x;
this.y = y;
this.s = s;
}
update(stars) {
fill(66, random(100), 86);
ellipse(this.x, this.y, this.s, this.s);
}
}