xxxxxxxxxx
49
let sharkie;
function setup() {
createCanvas(710, 400);
sharkie = new Predator();
}
function draw() {
background(50, 89, 100);
sharkie.show();
sharkie.move();
sharkie.update();
}
function Predator(x,y){
this.pos = createVector(random(width),random(height));
this.vel = p5.Vector.random2D(0,0);
this.show = function(){
stroke(255,0,0);
strokeWeight(10);
point(this.pos.x, this.pos.y);
if (this.pos.x > width || this.pos.x < width/18){
this.pos = createVector(400, 400);
}
if (this.pos.y > height || this.pos.y < height/18){
this.pos = createVector(400,400);
}
}
this.update = function(){
this.pos.add(this.vel);
}
this.move = function(){
if (keyIsDown(38)){
this.vel.add(0.06, -0.06);
} else if (keyIsDown(40)){
this.vel.add(-0.06, 0.06);
}
if (keyIsDown(37)){
this.vel.add(-0.06);
} else if (keyIsDown(39)){
this.vel.add(0.06);
}
}
}