xxxxxxxxxx
var bg;
var charcter
function preload(){
bg = loadImage("galaxy.jpeg")
}
// Set up variables
let bubbles = [];
let numBubbles = 10;
let bubblesPopped = 0;
let character;
let winScreen = false;
let loseScreen = false;
function setup() {
// Create canvas
createCanvas(400, 400);
// Create character
character = new Character();
// Create bubbles
for (let i = 0; i < numBubbles; i++) {
bubbles.push(new Bubble());
}
}
class Character {
constructor() {
this.x = width/2;
this.y = height/2;
this.radius = 25;
}
display() {
fill(0);
ellipse(this.x, this.y, this.radius*2);
}
move(x, y) {
this.x = x;
this.y = y;
}
}
// Bubble class
class Bubble {
/*constructor() {
this.x = random(width);
this.y = random(height);
this.radius = 15;*/
//variable
// need to put constructor to make it a function
constructor(){
this.x = random(600);
this.y = random(600);
this.size = random(10,150);
this.bubble_color= color(random(0,90), random(4,90), random(60,89));
this.speed = random(-1, 4)
}
display() {
noStroke()
fill(this.bubble_color);
circle(this.x, this.y, this.size);
}
}
function draw() {
// Set background
background(bg);
// Display bubbles
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].display();
}
// Check for collision with character
for (let i = 0; i < bubbles.length; i++) {
if (dist(character.x, character.y, bubbles[i].x, bubbles[i].y) < character.radius + bubbles[i].radius) {
bubbles.splice(i, 1);
bubblesPopped++;
}
}
// Display character
character.display();
// Check for win or lose
if (bubblesPopped === numBubbles) {
winScreen = true;
} else if (bubblesPopped < numBubbles && bubbles.length === 0) {
loseScreen = true;
}
// Display win or lose screen
if (winScreen) {
background(0, 255, 0);
textSize(32);
textAlign(CENTER);
text("You Win!", width/2, height/2);
} else if (loseScreen) {
background(255, 0, 0);
textSize(32);
textAlign(CENTER);
text("You Lose!", width/2, height/2);
}
}
function mousePressed() {
// Move character
character.move(mouseX, mouseY);
// Pop random bubble
let randomIndex = floor(random(bubbles.length));
bubbles.splice(randomIndex, 1);
}