0

Youtube の The Coding Train のチュートリアルに従って、フラッピー バード ゲームを作成しました。( https://www.youtube.com/watch?v=cXgA1d_E-jY )

元の命令はオブジェクトとして作成pipesballれましたが、代わりにクラスとして作成されるように変更しました。

2 つの問題があります。1)pipes配列で作成したパイプ インデックスが画面に表示ball.goup();されkeyPressed()ませんでしたconsole.log。コードをどのように修正すればよいですか? ありがとう。

let ball;
let pipes = [];

function setup() {
  createCanvas(700, 600);
  ball = new Ball();
  pipes.push(new Pipe());
}

function draw() {
  background(0);

  ball.show();
  ball.falldown();

  if (frameCount % 60 == 0) {
    pipes.push(new Pipe());
  }
  
  //since have splice, count from back to avoid uncounted indices
  for (let i = pipes.length - 1; i >= 0; i--) {
     pipes[i].show();
     pipes[i].move();

     if (pipes[i].hits(ball)) {
       console.log("hit");
     }

     if (pipes[i].offscreen()) {
       pipes.splice(i, 1);
     }
   }
}

function keyPressed() {
  if (key == ' ') {
    ball.goup;
    console.log('up');
  }
}


class Ball {
  constructor(x = 50, y = height / 2, g = 0.6, v = 0, l = -20) {
    this.x = x;
    this.y = y;
    this.gravity = g;
    this.velocity = v;
    this.lift = l;
  }

  goup() {
    this.velocity += this.lift;
  }

  falldown() {
    this.velocity += this.gravity;
    this.velocity *= 0.9;
    this.y += this.velocity;

    if (this.y > height) {
      this.y = height;
      this.velocity = 0;
    }

    if (this.y < 0) {
      this.y = 0;
      this.velocity = 0;
    }

  }

  show() {
    noStroke();
    fill(255);
    ellipse(this.x, this.y, 25, 25);
  }

}


class Pipe {
  constructor(x =width, y=0, w=100, t=random(2/height), b=random(2/height), s=2, c=false) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.top = t;
    this.bottom = b;
    this.speed = s;
    this.colorchange = c;
  }

  hits(ball) {
    if (ball.y < this.top || ball.y > height - this.bottom) {
      if(ball.x > this.x && ball.x < this.x + this.w){
      this.colorchange = true;
      return true;
      }
    }
    this.colorchange = false;
    return false
  }

  move() {
    this.x -= this.speed;
  }

  show() {
    fill(255);
    if (this.colorchange) {
      fill(255, 0, 0);
    }
      rect(this.x, this.y, this.w, this.top);
      rect(this.x, height - this.bottom, this.w, this.bottom);
    }
  
  //delete pipe indices from the array when pipes are off screen
  offscreen() {
    if (this.x < -this.w) {
      return true;
    } else {
      return false;
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>

4

1 に答える 1