Multi Bat One Player Pong

Hey guys,

I am currently making a single player pong game where every time the ball hits all the bats on the screen (only once otherwise the user dies), it moves to the next level where it is one extra bat from the last level. I have run into a little bit of a speed bump. Would be really grateful if someone helped me. Thanks in advance.

Here is the code:

import processing.core.PApplet;

public class multiBatBinaryPong
extends PApplet
{
multiBatBinaryPong.ball ball;
multiBatBinaryPong.bat[] bats = new multiBatBinaryPong.bat[0];
int level;
int levelStartTime;
int batIndex;
int score;

public void setup()
{
this.level = 1;this.batIndex = -1;this.score = 0;
setupLevel(this.level);

noStroke();
textSize(20.0F);

}

public void draw()
{
background(0);
this.ball = moveBall(this.ball, this.bats);
this.bats = moveBats(this.bats);
fill(min((millis() - this.levelStartTime) / 2, 255));
text("Score: " + str(this.score), 10.0F, this.height - 10);
if (levelComplete())
{
this.score += PApplet.parseInt(pow(2.0F, this.level - 1));
this.level += 1;
setupLevel(this.level);
}
}

public void setupLevel(int level)
{
this.ball = new multiBatBinaryPong.ball();
this.ball.x = (this.width / 4 + PApplet.parseInt(random(this.width / 2)));
this.ball.y = PApplet.parseInt(random(this.height / 2));
this.ball.xSpeed = (random(1.0F) < 0.5F ? -2 - PApplet.parseInt(random(1.0F)) : 2 + PApplet.parseInt(random(1.0F)));
this.ball.ySpeed = (random(1.0F) < 0.5F ? -2 - PApplet.parseInt(random(1.0F)) : 2 + PApplet.parseInt(random(1.0F)));
this.ball.radius = 20;

this.bats = new multiBatBinaryPong.bat[0];
for (int i = 0; i < level; i++)
{
  this.bats = ((multiBatBinaryPong.bat[])append(this.bats, new multiBatBinaryPong.bat()));
  this.bats[i].x = (this.width * i / level + this.width / level / 4);
  this.bats[i].y = (this.height * 8 / 10);
  this.bats[i].width = (this.width / level / 2);
  this.bats[i].height = 20;
  this.bats[i].xSpeed = 0;
  this.bats[i].ySpeed = 0;
  this.bats[i].hit = false;
}
this.batIndex = PApplet.parseInt(random(this.bats.length));
this.levelStartTime = millis();

}

public boolean touching(multiBatBinaryPong.ball ball, multiBatBinaryPong.bat bat)
{
return (bat.x < ball.x + ball.radius) && (bat.x + bat.width > ball.x - ball.radius) && (bat.y < ball.y + ball.radius) && (bat.y + bat.height > ball.y - ball.radius);
}

public int batIndex(int mouseX, int mouseY, multiBatBinaryPong.bat[] bats)
{
int index = -1;
for (int i = 0; i < bats.length; i++) {
if ((mouseX > bats[i].x) && (mouseX < bats[i].x + bats[i].width) && (mouseY > bats[i].y) && (mouseY < bats[i].y + bats[i].height)) {
index = i;
}
}
return index;
}

public multiBatBinaryPong.bat[] moveBats(multiBatBinaryPong.bat[] bats)
{
for (int i = 0; i < bats.length; i++)
{
bats[i].x += bats[i].xSpeed;
bats[i].y += bats[i].ySpeed;
if ((bats[i].x < 0) || (bats[i].x + bats[i].width > this.width)) {
bats[i].xSpeed *= -1;
}
if ((bats[i].y < 0) || (bats[i].y + bats[i].height > this.height)) {
bats[i].ySpeed *= -1;
}
if (i == this.batIndex) {
fill(255);
} else {
fill(100);
}
rect(bats[i].x, bats[i].y, bats[i].width, bats[i].height, 5.0F);
}
return bats;
}

public multiBatBinaryPong.ball moveBall(multiBatBinaryPong.ball ball, multiBatBinaryPong.bat[] bats)
{
ball.x += ball.xSpeed;
ball.y += ball.ySpeed;
if ((ball.x < 0) || (ball.x > this.width)) {
ball.xSpeed *= -1;
}
if (ball.y < 0) {
ball.ySpeed *= -1;
}
for (int i = 0; i < bats.length; i++) {
if (touching(ball, bats[i])) {
if (bats[i].hit)
{
noLoop();
text(“Finito”, this.width / 2, this.height / 2);
}
else
{
ball.ySpeed *= -1;
ball.y += ball.ySpeed;
bats[i].hit = true;
bats[i].ySpeed = 0;
}
}
}
ellipse(ball.x, ball.y, ball.radius * 2, ball.radius * 2);
if (ball.y - ball.radius > this.height)
{
noLoop();
text(“Finito”, this.width / 2, this.height / 2);
}
return ball;
}

public void mousePressed()
{
if (batIndex(this.mouseX, this.mouseY, this.bats) != -1) {
this.batIndex = batIndex(this.mouseX, this.mouseY, this.bats);
}
}

public void keyPressed()
{
switch (this.keyCode)
{
case 37:
this.bats[this.batIndex].xSpeed = -2; break;
case 39:
this.bats[this.batIndex].xSpeed = 2; break;
case 38:
this.bats[this.batIndex].ySpeed = -2; break;
case 40:
this.bats[this.batIndex].ySpeed = 2;
}
}

public boolean levelComplete()
{
boolean isComplete = true;
for (int i = 0; i < this.bats.length; i++) {
if (!this.bats[i].hit) {
isComplete = false;
}
}
return isComplete;
}

public void settings()
{
fullScreen();
}

public static void main(String[] passedArgs)
{
String[] appletArgs = { “multiBatBinaryPong” };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}

class ball
{
int x;
int y;
int radius;
int xSpeed;
int ySpeed;

ball() {}

}

class bat
{
int x;
int y;
int width;
int height;
int xSpeed;
int ySpeed;
boolean hit;

bat() {}

}
}

class multiBatBinaryPong.ball{
int x;
int y;
int radius;
int xSpeed;
int ySpeed;

multiBatBinaryPong.ball(multiBatBinaryPong parammultiBatBinaryPong) {}
}

class multiBatBinaryPong.bat{
int x;
int y;
int width;
int height;
int xSpeed;
int ySpeed;
boolean hit;

multiBatBinaryPong.bat(multiBatBinaryPong parammultiBatBinaryPong) {}
}

Sorry for it being in a really weird format. I don’t understand this forum yet

Please format your code. In the PDE, you need to press ctrl+t to indent your code properly. Here, you need to select your code and hit the </> button to properly format it.

No enough details specify for your problem plus you have provided a large code set. Consider providing an MCVE.

Kf