Pongを作る(13) 敵の動きを複雑に

敵のラケットが前に出てくるようにしてみましょう。

前回はボールがたくさんでるようになっていましたが、いったんやめます。
ballsの配列の表記を削除し、ball というインスタンス1つだけにします。

敵ラケットが追いかけるターゲットとしてtargetYという変数を作っていましたが、同じようにtargetXという変数もつくり、ボールを追いかけるようにします。

Ball ball = new Ball();
Racket me = new Racket();
Enemy enemy = new Enemy();

class Ball{
  float x, y, xSpeed, ySpeed, r, hr, xDir, yDir;
  Ball(){
    x = width/2;
    y = height/2;
    xSpeed = random(-5, 5);
    ySpeed = random(-5, 5);
    r = 8;
    xDir = 1;
    yDir = 1;
    hr = r/2;
  }
  void move(){
    fill(255);
    ellipse(x, y, r, r);
    
    if ((y+hr>height) || (y-hr<0))
   yDir *= -1; 
  if ((x+hr >= 440) && (x+hr <= 450) && (y > me.y-25) && (y < me.y+25))
      xDir *= -1;
    if (((x-hr) <= enemy.x+10) && ((x-hr) >= enemy.x) && (y > enemy.y) && (y < enemy.y+50)) { 
   xDir *= -1; 
  } 
  if (x > width) {
      enemy.score ++;
      x = width/2;
      y = height/2-25;
      xSpeed = random(1, 4);
      ySpeed = random(1, 4);
      enemy.x = 40;
      enemy.y = height/2-25;
    }
    else if(x < 0) { 
   me.score ++; 
   x = width/2; 
   y = height/2; 
   xSpeed = random(1, 4); 
   ySpeed = random(1, 4); 
   enemy.x = 40; enemy.y = height/2-25; 
  } 
  x += xSpeed * xDir; 
  y += ySpeed * yDir; 
  xSpeed += 0.01; 
  ySpeed += 0.01; 
 } 
} 

class Racket{ 
 float x, y, easing, targetY; 
 int score; 
 Racket()
 { 
  x = 440; 
  y = height/2; 
  easing = 0.1; 
  score = 0; 
 } 
 void move(){ 
  targetY = mouseY; 
  y += (targetY - y) * easing; 
  rect(x, y-25, 10, 50); 
 } 
} 
class Enemy{ 
 float x, y, dir, easingX, easingY, targetX, targetY; 
 int score; 
 Enemy(){ 
  x = constrain(x, 0, width/2-10); 
  x = 40; 
  y = height/2; 
  dir = 1; 
  score = 0; 
  easingX = 0.05; 
  easingY = 0.1; 
  targetX = ball.x; 
  targetY = ball.y; 
 } 
 void move(){ 
  targetX = ball.x; 
  targetY = ball.y; 
    x += (targetX - x) * easingX;
    y += (targetY - y) * easingY;
    rect(x, y, 10, 50);
    y += dir;
    if((y < 0) || ((y+250) > width))  dir *= -1;
  }
}

void setup() {
  size (480,320);
  ball = new Ball();
  me = new Racket();
  enemy = new Enemy();
}

void draw() {
  background(0);
  enemy.x = constrain(enemy.x, 0, width/2-10);
  ball.move();
  
  strokeWeight (1);
  stroke(255);
  for ( int i = 0; i < height; i += 10 ){ 
  line(width/2, i, width/2, i+5); 
 } 
 me.move(); 
 enemy.move(); 
 // score display 
 textAlign(CENTER, CENTER); 
 textSize(30); 
 text(enemy.score, 120, 20); 
 text(me.score, 360, 20); 

 // end message 
 textSize(80); 
 if (me.score >= 100){
    text("You WIN!", width/2, height/2-30);
    textSize(40);
    text("Click to restart", width/2, height/2+30);
  }
  else if (enemy.score >= 100){
    text("You LOSE!", width/2, 130);
    textSize(40);
    text("Click to restart", width/2, 200);
  }
  
  // reset
  if(mousePressed){
    ball.x = width/2;
    ball.y = height/2;
    me.score = 0;
    enemy.score = 0;   
  }
}

画面左側で敵のラケットが X 軸方向もボールを追いかけるようになりました。点が入るごとに左側に戻るようにしています。
左右の動きがなかった時よりも強くなったかも。でも、動きがちょっとまだ変だし、ボールをうまく跳ね返さないときが。もう少し調整が必要ですね。

Leave a Reply

Your email address will not be published. Required fields are marked *