Pongを作る(14) 自分のラケットも自由に動かす

今度は自分のラケットも X 方向に自由に動くようにします。

コードは Pongを作る(12) の回のものを使います。

Pongを作る(12) の敵が強いので、自分のラケットも X 方向(左右)に動けるようにします。少し強くなるかも。以下を修正します。

<Ballクラスのmoveメソッド>
if ((x+hr >= 440) && (x+hr <= 450) && (y > me.y-25) && (y < me.y+25)) 
↓ 修正 
if ((x+hr >= me.x) && (x+hr <= me.x+10) && (y > me.y-25) && (y < me.y+25))

<Racketクラス>
float型の targetX を追加
// move メソッドに以下を追加
x = constrain(x, 320, width);
targetX = mouseX; 
x += (targetX - x) * easing; 

x = constrain(x, 320, width); と記述することで、画面の右側3分の1だけで動けるようにしました。
右半分で動くことができると、ボールが出る瞬間にはね返すことができるようになってしまい、ちょっと簡単すぎてしまいます。現状では、ラケットを早く左右に動かすとボールがすり抜けてしまうときがあるので修正が必要です。

今日のコードです。

Ball[] balls = new Ball[10];
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 = random(4, 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 >= me.x) && (x+hr <= me.x+10) && (y > me.y-25) && (y < me.y+25))
      xDir *= -1;
    if (((x-hr) <= 50) && ((x-hr) >= 40) && (y > enemy.y) && (y < enemy.y+100)) xDir *= -1; if (x > width) {
      enemy.score ++;
      x = width/2;
      y = height/2;
      xSpeed = random(1, 4);
      ySpeed = random(1, 4);
    }
    else if(x < 0) { 
      me.score ++; 
      x = width/2; 
      y = height/2; 
      xSpeed = random(1, 4); 
      ySpeed = random(1, 4); 
    } 

    x += xSpeed * xDir; 
    y += ySpeed * yDir;   
    xSpeed += 0.01;
    ySpeed += 0.01;
  }
}

class Racket{
  float x, y, easing, targetY, targetX;
  int score;
  Racket(){
    x = 440;
    y = height/2;
    easing = 0.1;
    score = 0;
  }
  void move(){
    x = constrain(x, 320, width);
    targetY = mouseY; 
    y += (targetY - y) * easing; 
    targetX = mouseX; 
    x += (targetX - x) * easing; 
    rect(x, y-25, 10, 50);
  }
}

class Enemy{
  float x, y, dir;
  int score;
  Enemy(){
    x = 40;
    y = height/2;
    dir = 1;
    score = 0;
  }
  void move(){
    rect(40, y, 10, 100);
    y += dir;
    if((y < 0) || ((y+250) > width))  dir *= -1;
  }
}

void setup() {
  size (480,320);
  for (int i = 0; i < balls.length; i++){
    balls[i] = new Ball();
  }
  me = new Racket();
  enemy = new Enemy();
}

void draw() {
  background(0);
  
  for(int i = 0; i < 10; i++)  balls[i].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){
    for (int i = 0; i < 10; i++){
      balls[i].x = width/2;
      balls[i].y = height/2;
    }
    me.score = 0;
    enemy.score = 0;   
  }
}

Leave a Reply

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