xxxxxxxxxx
158
// ZainaIqbal
// midterm game
// Intro to IM - Spring 2022
//step 1 make the basic layout with images
//step 2 butterfly class and store all its features - make it move up and down
//step 3 trap class and store the features - random heights
//step 4 make a start up instruction page
//step 5 add a scoring system
//step 6 add sound when key is pressed
//step 7 ensure there is a restart game function
//step 8 change color of trap if touched - game over
class Butterfly {
constructor(x, y) {
this.posX = x;
this.posY = y;
this.height = 140;
this.width = 150;
this.gravity = 2;
this.lift = -10;
this.velocity = 0;
this.canvasHeight = 600;
this.up = function () {
this.velocity += this.lift;
};
this.update = function () {
this.velocity += this.gravity;
this.posY += this.velocity;
//don't want the butterfly fly off the screen
if (this.posY > this.canvasHeight) {
this.posY = this.canvasHeight;
// this.velocity = 0;
}
if (this.posY < 0) {
this.posY = 0;
this.velocity = 0;
}
};
}
draw() {
image(butterfly, this.posX, this.posY, this.width, this.height);
// console.log(this.posX, this.posY, this.width, this.height);
}
}
class Traps {
constructor() {
this.x = width;
this.w = 30;
this.topMin = 50;
this.gapStart = random(this.topMin, this.botMin);
this.gapLength = 175;
this.speed = 6;
this.highlight = false;
this.hits = function (b) {
if (b.y < this.top || b.y > height - this.bottom) {
if (b.x > this.x && b.x < this.x + this.w) {
this.highlight = true;
return true;
}
}
this.highlight = false;
return false;
};
draw();
{
image(flytrap, this.x, this.w, 250, 250);
}
this.update = this.function();
{
this.x -= this.speed;
}
this.offscreen = function () {
if (this.x < -this.w) {
return true;
} else {
return false;
}
};
}
}
function preload() {
flytrap = loadImage("assets/flytrap.png");
butterfly = loadImage("assets/butterfly.png");
backgroundIMG = loadImage("assets/greenery.png");
flytrapsups = loadImage("assets/flytrapups.png");
}
var b = new Butterfly(200, 200);
// var t = new Traps (0,0)
function setup() {
createCanvas(800, 600);
// resetSketch();
background(backgroundIMG);
frameRate(40);
b = new Butterfly(200, 200);
// flytrap.push(new Flytrap());
//restarting the game with a restart button
var button = createButton("Restart Game");
button.mousePressed(resetSketch);
}
// to reset the sketch
function resetSketch(){
// put in the code i want to restart
}
function draw() {
image(backgroundIMG, 0, 0);
b.update();
push();
tint(250, 180, 0);
//flytrap 1 bottom row
image(flytrap, 200, 350, 250, 250);
//flytrap 2 bottom row
image(flytrap, -50, 350, 250, 250);
//flytrap 3 bottom row
image(flytrap, 460, 350, 250, 250);
//flytrap 4 bottom row
image(flytrap, 700, 350, 250, 250);
//flytrap 1 top row
image(flytrapsups, 160, 0, 250, 250);
//flytrap 2 top row
image(flytrapsups, -80, 0, 250, 250);
//flytrap 3 top row
image(flytrapsups, 410, 0, 250, 250);
//flytrap 4 top row
image(flytrapsups, 660, 0, 250, 250);
pop();
b.draw();
}
// making the butterfly bounce when key is pressed
function keyPressed() {
if (key == " ") {
b.up();
}
}