xxxxxxxxxx
230
let button;
let bubbleMan;
let bubbleManImg;
let bubblemanXPos;
let balls = [];
let numLives = 3;
let game;
let arrowAnimation;
let arrow;
let ballimg;
class Game {
constructor() {
this.level = 1;
}
restartLevel() {
balls.forEach((bubble) => {
bubble.ball.remove();
});
balls = [];
switch (this.level) {
case 1:
balls[0] = new Ball(100, width / 2, 50);
break
case 2:
balls[0] = new Ball(100, width / 4, 50);
balls[1] = new Ball(100, width / 2 + (width / 4), 50);
break
case 3:
balls[0] = new Ball(100, width / 4, 50);
balls[1] = new Ball(100, width / 2, 50);
balls[2] = new Ball(100, width / 2 + (width / 4), 50);
break
}
}
displayLives() {
text(`LIVES: ${numLives}`, width - 60, 20);
text(`LEVEL: ${this.level}`, width - 60, 50);
}
}
function restartGame() {
balls.forEach((bubble) => {
bubble.ball.remove();
});
balls = [];
numLives = 3;
this.level = 1;
game = new Game();
game.restartLevel();
button.hide();
}
class Ball {
constructor(r, x, y) {
this.radius = r;
this.ball = createSprite(
x, y, this.radius, this.radius
);
let randomR = random(250);
let randomB = random(250);
let randomG = random(250);
this.ball.draw = function() {
stroke(1); fill(randomR, randomB, randomG); ellipse(0,0, r, r);
};
}
}
function breakBubble(arrow, ball) {
arrow.remove();
arrow = null;
ball.remove();
ball = null;
}
function loseLife(spriteA, spriteB) {
numLives -= 1;
game.restartLevel();
}
function preload() {
bubbleManImg = loadImage('img/bubbleman.png');
arrowAnimation = loadAnimation(
"img/arrow1.png",
"img/arrow2.png");
}
function setup() {
createCanvas(600, 400);
level1bg = loadImage('img/bg1.jpg');
level2bg = loadImage('img/bg2.jpg');
level3bg = loadImage('img/bg3.jpg');
game = new Game();
game.restartLevel();
// add bubble man
bubbleManXPos = width / 2;
bubbleMan = createSprite(bubbleManXPos, height - 35);
bubbleManImg.resize(50, 70);
bubbleMan.addImage(bubbleManImg);
// create button and hide upon setup
button = createButton('RESTART GAME');
button.position(235, height / 2);
button.hide();
button.mousePressed(restartGame);
}
function draw() {
clear();
if (game.level === 1) {
background(level1bg);
} else if (game.level === 2) {
background(level2bg);
} else if (game.level === 3) {
background(level3bg);
}
if (numLives === -1) {
button.show();
} else if ((game.level === 1 || game.level === 2) && balls.length === 0) {
if (arrow) arrow.remove();
arrow = null;
game.level += 1;
game.restartLevel();
} else if (game.level === 3 && balls.length === 0) {
text("YOU'VE WON! CONGRATS!", width / 2 - 100, height / 2 - 100)
button.show();
} else {
if (arrow) {
arrow.height += 5;
arrow.position.y -= 5;
if (arrow.position.y <= 300) {
arrow.remove();
arrow = null;
}
}
if (bubbleMan.collider) {
bubbleMan.setCollider(
"rectangle"
);
}
for (var i = 0; i < balls.length; i++) {
balls[i].ball.setCollider(
"circle"
);
balls[i].ball.addSpeed(0.1, 90);
// dampen the y velocity slightly
if (balls[i].ball.position.y > height) {
balls[i].ball.velocity.y *= -0.98;
}
if (balls[i].ball.position.x > width ||
balls[i].ball.position.x < 0) {
balls[i].ball.velocity.x *= -1;
}
if (arrow) {
arrow.setCollider(
"rectangle", 0, 95, 20, arrow.height
);
let xPos = balls[i].ball.position.x;
let yPos = balls[i].ball.position.y;
let oldR = balls[i].radius;
if (arrow.collide(balls[i].ball, breakBubble)) {
if (oldR === 25) {
balls.splice(i, 1);
break;
}
let ball1 = new Ball(oldR / 2, xPos, yPos);
let ball2 = new Ball(oldR / 2, xPos, yPos);
ball1.ball.velocity.x = -2;
ball2.ball.velocity.x = 2;
balls.splice(i, 1);
balls.push(ball1);
balls.push(ball2);
arrow = null;
}
}
if (balls[i].ball.collide(bubbleMan, loseLife)) {
// after collision
if (numLives === -1) {
balls.forEach((bubble) => {
bubble.ball.remove();
});
balls = [];
}
bubbleManXPos = width / 2;
}
}
if (keyIsDown(LEFT_ARROW) && bubbleManXPos >= 30) {
bubbleManXPos -= 5;
}
if (keyIsDown(RIGHT_ARROW) && bubbleManXPos <= (width - 30)) {
bubbleManXPos += 5;
}
bubbleMan.position.x = bubbleManXPos;
game.displayLives();
}
drawSprites();
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
if (bubbleManXPos >= 30) {
bubbleManXPos -= 5;
}
} else if (keyCode === RIGHT_ARROW) {
if (bubbleManXPos <= width - 30) {
bubbleManXPos += 5;
}
}
if (keyCode === 32 && arrow == null) {
arrow = createSprite(bubbleManXPos, height + 250);
arrow.addAnimation("default", arrowAnimation);
arrow.height = 0;
}
}