Source




let rocket;
function setup() {
  createCanvas(600, 600);

  rocket = new Rocket(width/2, height/2);
}

function draw() {
  background(0);
  if (keyIsDown(LEFT_ARROW)){
    rocket.left(1);
  }else if (keyIsDown(RIGHT_ARROW)){
    rocket.right(1);
  }
  if (keyIsDown(UP_ARROW)){
    rocket.forward(1);
  }


  rocket.draw();
  rocket.update();
}

class Rocket {
  constructor(x, y) {
    this.position = createVector(x, y);
    this.velocity = createVector(1,0);
    this.heading = createVector(1,0);
    this.acceleration = createVector(0,0);
  }

  left(amt){
    this.heading.rotate(-.1 * amt);
  }

  right(amt){
    this.heading.rotate(.1 * amt);
  }

  forward(amt){
    this.acceleration.add(p5.Vector.mult(this.heading, amt));
  }


  update(){
    this.velocity.add(this.acceleration);
    this.position.add(this.velocity);

    this.acceleration.x = 0;
    this.acceleration.y = 0;

    // adjust for wrap
    if (this.position.x < 0){
      this.position.x += width;
    }
    if (this.position.x > width){
      this.position.x -= width;
    }
    if (this.position.y < 0){
      this.position.y += height;
    }
    if (this.position.y > height){
      this.position.y -= height;
    }

  }


  draw(){
    push();
    fill("white");
    translate(this.position.x, this.position.y);
    rotate(this.heading.heading());
    // draw the rocket
    triangle(-50, 0, -75, -15, -75, 15);
    bezier(0, 0, -25, 15, -50, 15, -75, 0);
    bezier(0, 0, -25, -15, -50, -15, -75, 0);

   
    pop();
  }


}