xxxxxxxxxx
339
// Bitmap Game Level 1 Greybox
let drops = [];
var bucket, dropper;
var dropSpeed = 4;
var gap = -2000;
var dropSize = 10;
var bucketSpeed = 3;
var counter = 0;
var highscore = 0;
var interval = 20000;
var timer;
var countdown;
var gameOn = false;
var gameEnd = false;
var scene = 0; // 0=opening; 1=game play; 2=game over/restart screen; 3=level passed
var bgColor = 255;
var startBtn, restartBtn, nextBtn;
function setup() {
createCanvas(700, 500);
changeDirection = false;
bucket = new Bucket(width / 2, height - 100, 40, 40);
body = new Body(bucket.x-10, bucket.y+bucket.h, 60, 80)
dropper = new Dropper(100, 150, 80, 100);
timer = millis() + interval;
startBtn = new Button(width / 2, height / 2, 100, 50, "PLAY", 255);
restartBtn = new Button(width / 2, height / 2, 200, 50, "RESTART", 255);
nextBtn = new Button1(width / 2, height / 2, 200, 50, "LEVEL 2");
for (let i = 0; i < 5; i++) {
drops[i] = new Drop(random(width), random(-20, gap), dropSize, dropSpeed);
}
}
function draw() {
background(bgColor);
textSize(32);
for (let i = 0; i < 5; i++) {
drops[i].reset();
drops[i].move();
drops[i].render();
drops[i].caught();
}
// for (let i = 0; i < 5; i++) {
// drops[i].caught();
// }
// Main character movement
bucket.render();
body.render();
if (keyIsDown(LEFT_ARROW)) {
bucket.x -= 5;
body.x -= 5;
} if (keyIsDown(RIGHT_ARROW)) {
bucket.x += 5;
body.x += 5;
}
// Prof movement
dropper.move();
dropper.render();
if (timer < millis()) {
timer = millis() + interval;
}
if (gameOn) {
countdown = ceil((timer - millis()) / 1000) - 1;
}
if (countdown == 0) {
gameOver1();
}
if (scene === 0) { // begin
bgColor = color(255, 117, 26);
//fill(255);
startBtn.render();
startBtn.click();
} else if (scene === 1) { // level 1 game play
bgColor = 255;
fill(0);
push();
textSize(22);
text("Time: " + countdown, 20, 50);
text("Intellectual Capability: ", 20, 80);
rect(20, 85, 10+10*counter, 15);
pop();
if (countdown <= 17 && countdown >= 16) text("Aquire knowledge, or Professor Owl will keep it!", width/2-335, height/2);
} else if (scene === 2) { // restart
bgColor = 0;
fill(255);
text("Score: " + counter, width/2-60, height/2-150);
text("Looks like you didn't acquire enough knowledge.", width/2-345, height/2-100);
text("Remember: C's get degrees. Try again!", width/2-280, height/2 - 50);
restartBtn.render();
restartBtn.click();
} else if (scene === 3) {
bgColor = 0;
fill(255);
text("Score: " + counter, width/2-60, height/2-150);
nextBtn.render();
nextBtn.click();
}
}
function gameOver1() {
for (let i = 0; i < 5; i++) {
drops[i].alive = false;
}
gameOn = false;
if (counter < 8) scene = 2; // determines min score to move on
else if (counter >= 8) scene = 3;
}
function start1() {
timer = millis() + interval;
for (let i = 0; i < 5; i++) {
drops[i].alive = true;
}
bucket.alive = true;
body.alive = true;
dropper.alive = true;
gameOn = true;
counter = 0;
}
class Drop {
constructor(x, y, w, speed) {
this.x = x;
this.y = y;
this.w = w;
this.speed = speed;
this.alive = false;
}
reset() {
if (this.y > height) {
this.x = random(width);
this.y = random(-20, gap);
this.alive = true;
}
}
move() {
if (this.alive) {
this.y += this.speed;
} else {
this.x = random(width);
this.y = random(-20, gap);
}
}
render() {
if (this.alive) {
fill(0)
ellipse(this.x, this.y, this.w, this.w);
}
}
caught() {
if (this.y >= bucket.y &&
this.x > bucket.x &&
this.x < bucket.x + bucket.w) {
counter++;
this.x = random(width);
this.y = random(-20, gap);
}
else if (this.y > dropper.y &&
this.y < dropper.y+dropper.h &&
this.x > dropper.x &&
this.x < dropper.x + dropper.w) {
counter--;
this.x = random(width);
this.y = random(-20, gap);
}
}
}
class Bucket {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.alive = false;
}
render() {
if (this.alive) {
fill(0);
rect(this.x-2.5*counter, this.y-5*counter, this.w+5*counter, this.h+5*counter);
}
}
}
class Body {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.alive = false;
}
render() {
if (this.alive) {
fill(0);
rect(this.x, this.y, this.w, this.h);
}
}
}
class Dropper {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.alive = false;
}
move() {
if(dropper.x > width - dropper.w) changeDirection = true;
else if (dropper.x <= 0) changeDirection = false;
if (dropper.x >= 0 && changeDirection == false) dropper.x++;
else if(changeDirection == true) dropper.x--;
}
render() {
if (this.alive) {
fill(0);
rect(this.x, this.y, this.w, this.h);
}
}
}
class Button {
constructor(x, y, w, h, text, c) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = text;
this.c = c;
}
render() {
push();
textAlign(CENTER);
stroke(0);
fill(this.c);
rectMode(CENTER);
rect(this.x, this.y, this.w, this.h);
fill(255);
strokeWeight(2);
text(this.text, this.x, this.y+10);
pop();
}
click() {
if (
mouseIsPressed &&
mouseX > this.x-this.w/2 &&
mouseX < this.x + this.w/2 &&
mouseY > this.y-this.h/2 &&
mouseY < this.y + this.h/2) {
start1();
scene = 1;
}
}
}
class Button1 {
constructor(x, y, w, h, text) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = text;
}
render() {
push();
textAlign(CENTER);
stroke(0);
fill(255);
rectMode(CENTER);
rect(this.x, this.y, this.w, this.h);
text(this.text, this.x, this.y+10);
pop();
}
click() {
if (
mouseIsPressed &&
mouseX > this.x-this.w/2 &&
mouseX < this.x + this.w/2 &&
mouseY > this.y-this.h/2 &&
mouseY < this.y + this.h/2) {
// switch to next level here
// start();
// state = 1;
}
}
}