xxxxxxxxxx
65
function Ball(x, y) {
this.x = x;
this.y = y;
this.pos = createVector(this.x, this.y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.r = 30;
this.up = false;
this.show = function() {
fill(255);
stroke(0);
strokeWeight(2);
ellipse(this.pos.x, this.pos.y - this.r / 2, this.r, this.r);
}
this.update = function() {
this.vel.add(this.acc);
this.vel.mult(0.99);
this.pos.add(this.vel);
this.acc.mult(0);
if (this.pos.y > height) {
this.pos = createVector(this.x, this.y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
}
}
this.applyForce = function(f) {
this.acc.add(f);
}
this.collide = function(p) {
if (((this.pos.y) > p.y) && (this.pos.y < (p.y + p.w)) && ((this.pos.x) < (p.x + p.l)) && (this.pos.x > p.x)) {
this.vel.y = 0;
this.pos.y = p.y;
this.up = true;
if (p.win == true) {
translate(b.pos.x - 50, 0);
this.pos = createVector(width/2, height/2 - 100);
noLoop();
background(51);
textAlign(CENTER);
textSize(100, 100);
text("YOU WIN!", width/2, height/2);
}
}
}
this.controls = function() {
if (keyIsDown(RIGHT_ARROW)) {
this.applyForce(createVector(0.1, 0));
this.vel.x = constrain(this.vel.x, -5, 5);
}
if (keyIsDown(LEFT_ARROW)) {
this.applyForce(createVector(-0.1, 0));
this.vel.x = constrain(this.vel.x, -5, 5);
}
if (keyIsDown(UP_ARROW) && (this.up == true)) {
this.applyForce(createVector(0, -10));
this.up = false;
}
}
}