xxxxxxxxxx
46
let bubble
function setup() {
createCanvas(400, 400);
bubble = new Bubble();
}
function draw() {
background(220);
bubble.show();
bubble.bounce();
bubble.speed();
}
class Bubble {
constructor(){ //all variable go here
this.xPos = 200;
this.yPos = 200;
this.rAdius = 50;
this.xSpeed = 1;
this.ySpeed = 2;
}
show(){
ellipse(this.xPos,this.yPos,this.rAdius)
}
speed(){
this.xPos += this.xSpeed
this.yPos += this.ySpeed
}
bounce(){
if(this.xPos<0 || this.xPos> width){
this.xSpeed*=-1
}
if(this.yPos<0 || this.yPos> width){
this.ySpeed*=-1
}
}
}