xxxxxxxxxx
109
let player;
let boost;
function setup() {
createCanvas(1000, 1000);
player = new Player();
boost = new Boost();
}
function draw() {
background(150, 200, 255);
player.update();
player.show();
boost.update();
boost.show();
}
class Player {
constructor() {
this.x = 500;
this.y = 500;
this.r = 25;
this.deltaX = 0;
this.deltaY = 0;
}
update() {
//movement
this.x += this.deltaX;
this.y += this.deltaY;
//inputs
if (keyIsDown(RIGHT_ARROW) || keyIsDown(68)) {
this.deltaX += 1;
}
if (keyIsDown(LEFT_ARROW) || keyIsDown(65)) {
this.deltaX -= 1;
}
if (keyIsDown(DOWN_ARROW) || keyIsDown(83)) {
this.deltaY += 1;
}
if (keyIsDown(UP_ARROW) || keyIsDown(87)) {
this.deltaY -= 1;
}
//"friction"
this.deltaX *= 0.92;
this.deltaY *= 0.92;
//growing mechanism
if (dist(mouseX, mouseY, this.x, this.y) <= this.r) {
this.r += 0.5;
}
//death mechanism
if (this.x < this.r || this.x > width - this.r || this.y < this.r || this.y > height - this.r) {
console.log("cooked!");
frameRate(0);
}
//win condition
if (this.r <= 0) {
console.log("You win!");
frameRate(0);
}
}
show() {
//circle
noStroke();
fill(255, 220);
circle(this.x, this.y, this.r*2);
}
}
class Boost{
constructor() {
this.x = random(0, width);
this.y = random(0, height);
this.r = 5;
}
update() {
if (dist(this.x, this.y, player.x, player.y) < this.r + player.r) {
console.log("OI OI OIII!");
player.r *= 0.5;
player.r -= 1;
this.x = random(0, width);
this.y = random(0, height);
}
}
show () {
fill (0);
circle(this.x, this.y, this.r*2);
}
}