Pongに点数を表示させましょう。
点数のための変数を作ります。mySet,enemySet はセットです。10点取れば 1 セット取るようにします。
int myScore = 0;
int enemyScore = 0;
int mySet = 0;
int enemySet = 0;
左右の壁をボールが通過したら、点数が入るようにします。
if (ballX+3 > width) {
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
else if(ballX-3 < 0) {
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
↓ 修正
if (ballX+3 > width) {
enemyScore ++;
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
else if(ballX-3 < 0) {
myScore++;
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
スコアが 10 以上になればセットを 1 増やします。後半はセットとスコアを表示するコードです。
if (myScore > 9) {
mySet++;
myScore = 0;
}
if (enemyScore > 9) {
enemySet++;
enemyScore = 0;
}
textAlign(CENTER, CENTER);
textSize(30);
text(enemyRound, 80, 20);
text(enemyScore, 160, 20);
text(myRound, 320, 20);
text(myScore, 400, 20);
これで点数とセットが表示されるようになりました(左がセット、右がスコア)。

今日のここまでのコードです。ごちゃごちゃしている部分はあとから直します。
float ballX, ballY;
float racketY;
int xDir, yDir;
float xSpeed, ySpeed;
float enemyY;
float easing;
int myScore = 0;
int enemyScore = 0;
int mySet = 0;
int enemySet = 0;
void setup() {
size (480,320);
ballX = width/2;
ballY = height/2;
racketY = height/2;
easing = 0.1;
xDir = 1;
yDir = 1;
xSpeed = 1;
ySpeed = 1;
}
void draw() {
background(0);
strokeWeight (1);
stroke(255);
for ( int i = 0; i < height; i += 10 ){
line(width/2, i, width/2, i+5); }
//my racket
float targetY = mouseY;
racketY += (targetY - racketY) * easing;
rect(440, racketY-25, 8, 50);
//enemy racket
enemyY += (ballY - enemyY) * easing;
rect(40, enemyY-25, 8, 50);
ellipse(ballX, ballY, 6, 6);
if((ballY+3>height) || (ballY-3<0)) yDir *= -1;
if ((ballX+3 >= 440) && (ballX+3 <= 448) && (ballY > racketY-25) && (ballY < racketY+30))
xDir *= -1;
if ((ballX-3 <= 48) && (ballX-3 >= 40) && (ballY > enemyY-25) && (ballY < enemyY+25))
xDir *= -1;
if (ballX+3 > width) {
enemyScore ++;
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
else if(ballX-3 < 0) {
myScore ++;
ballX = width/2;
ballY = height/2;
xSpeed = random(1, 4);
ySpeed = random(1, 4);
}
ballX += xSpeed * xDir;
ballY += ySpeed * yDir;
textAlign(CENTER, CENTER);
textSize(30);
text(enemySet, 80, 20);
text(enemyScore, 160, 20);
text(mySet, 320, 20);
text(myScore, 400, 20);
if (myScore > 2) {
mySet++;
myScore = 0;
}
if (enemyScore > 2) {
enemySet++;
enemyScore = 0;
}
xSpeed += 0.01;
ySpeed += 0.01;
}