I'm writing a simple Pong game. I have a method bounce()
that makes everything run: the ball, the paddles, score etc. and it uses 12 arguments: 3 counters, 2 paddles, ball, 5 textures, 1 color. I'm a beginner to programming and only have couple months college experience so I don't know if that's too much. In classes, I usually had 1-3 arguments per method. Am I doing something wrong?
In method run()
I'm creating everything I need for the graphics, then I call looped bounce()
and I need to pass everything I just created as an argument. This seems very wrong to me, but I fail to find a way around it.
Please ignore non-English comments in code.
import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.util.Random;
import acm.graphics.GImage;
import acm.graphics.GLabel;
import acm.graphics.GOval;
import acm.graphics.GRect;
import acm.program.GraphicsProgram;
/* TO DO LIST
* ------------------
* Corner Bounce
* Difficulty Level
*
*
*
*/
@SuppressWarnings("serial")
public class Pong extends GraphicsProgram {
private static final double PAUSE = 1000 / 96.0;
public Random rand = new Random();
public double mouseY;
// ball
private static final double BALL_SIZE = 20;
private static final double SPEED = 5;
public double dx = SPEED * 1.5;
public double dy = SPEED;
public double startX;
public double startY;
// paddle
private static double HEIGHT = 150;
private static double WIDTH = 15;
private static int COUNTER = 0;
public static double AI_SPEED = 9.0; // difficulty 0-20
// label
private float TRANSPARENCY = 0.0f;
public int AI_SCORE = 0;
public int PLAYER_SCORE = 0;
public static void main(String[] args) {
Pong p = new Pong();
p.run();
}
public void run() {
addMouseListeners();
GLabel counter = new GLabel(String.valueOf(COUNTER));
GLabel AiScore = new GLabel(String.valueOf(AI_SCORE));
GLabel PlayerScore = new GLabel(String.valueOf(COUNTER));
counter.setFont("Impact-600");
AiScore.setFont("Impact-100");
PlayerScore.setFont("Impact-100");
Color labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
Color scoreC = new Color(0, 0.0f, 0.0f, 0.1f);
counter.setColor(labelC);
AiScore.setColor(scoreC);
PlayerScore.setColor(scoreC);
counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
getHeight() / 2 + counter.getHeight() / 3.2);
counter.sendToFront();
Image texture = getImage(getCodeBase(), "texture.png");
Image texture2 = getImage(getCodeBase(), "texture2.png");
Image ballTexture = getImage(getCodeBase(), "ballTexture.png");
Image greenFlash = getImage(getCodeBase(), "greenFlash.png");
Image blueFlash = getImage(getCodeBase(), "blueFlash.png");
GImage image = new GImage(texture);
GImage image2 = new GImage(texture2);
GImage image3 = new GImage(ballTexture);
GImage image4 = new GImage(greenFlash, -250, 0);
GImage image5 = new GImage(blueFlash, -250, 0);
add(image);
add(image2);
add(image3);
add(image4);
add(image5);
add(counter);
image.setSize(WIDTH + 1, HEIGHT + 1);
image2.setSize(WIDTH + 1, HEIGHT + 1);
image3.setSize(BALL_SIZE, BALL_SIZE);
image4.setSize(100, 300);
image5.setSize(100, 300);
GOval ball = makeBall();
GRect paddleLeft = makePaddle();
GRect paddleRight = makePaddle();
add(paddleLeft);
add(paddleRight);
bounce(labelC, AiScore, PlayerScore, counter, ball, paddleLeft,
paddleRight, image, image2, image3, image4, image5);
}
public static GRect makePaddle() {
GRect result = new GRect(0, 0, WIDTH, HEIGHT);
result.setFilled(true);
result.setColor(Color.BLACK);
return result;
}
public static GOval makeBall() {
GOval result = new GOval(150, 100, BALL_SIZE, BALL_SIZE);
result.setFilled(true);
result.setColor(Color.WHITE);
return result;
}
public void mouseMoved(MouseEvent e) {
mouseY = e.getY();
}
public void bounce(Color labelC, GLabel AiScore, GLabel PlayerScore,
GLabel counter, GOval ball, GRect paddleLeft, GRect paddleRight,
GImage image, GImage image2, GImage ballTexture, GImage greenFlash,
GImage blueFlash) {
add(ball);
add(ballTexture);
add(AiScore);
add(PlayerScore);
PlayerScore.setLabel(String.valueOf(PLAYER_SCORE));
PlayerScore.setLocation(3*WIDTH+10,getHeight()-10);
AiScore.setLabel(String.valueOf(AI_SCORE));
AiScore.setLocation(getWidth()-AiScore.getWidth()-3*WIDTH-10, getHeight()-10);
startX = rand.nextInt((int) (getWidth() * 0.8))
+ (int) (0.1 * getWidth()); // zapobiega pojawieniu się piłki po
// lewej stronie lewej paletki
startY = rand.nextInt(getHeight());
ball.setLocation(startX, startY);
image2.setLocation(getWidth() - 3 * WIDTH, startY - HEIGHT / 2);
paddleRight.setLocation(getWidth() - 3 * WIDTH, startY - HEIGHT / 2);
image2.sendToFront();
counter.setLabel(String.valueOf(COUNTER));
counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
getHeight() / 2 + counter.getHeight() / 3.2);
dx = SPEED * 1.5;
dy = SPEED;
while (true) {
ball.move(dx, dy);
ballTexture.setLocation(ball.getX(), ball.getY());
ballTexture.sendToFront();
if (TRANSPARENCY >= 0.0f)
TRANSPARENCY -= TRANSPARENCY / 100f;
labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
counter.setColor(labelC);
if (mouseY < getHeight() - HEIGHT) { // Player
paddleLeft.setLocation(2 * WIDTH, mouseY);
image.setLocation(2 * WIDTH, mouseY);
image.sendToFront();
} else {
paddleLeft.setLocation(2 * WIDTH, getHeight() - HEIGHT);
image.setLocation(2 * WIDTH, getHeight() - HEIGHT);
image.sendToFront();
}
// AI, z którym da się wygrać
/*
double targetY = ball.getY() + BALL_SIZE / 2;
if (targetY < getHeight() - HEIGHT / 2 && targetY > HEIGHT / 2) {
if (targetY < paddleRight.getY() + HEIGHT / 2) {
paddleRight.move(0, -AI_SPEED);
image2.move(0, -AI_SPEED);
} else if (targetY > paddleRight.getY() + HEIGHT / 2) {
paddleRight.move(0, AI_SPEED);
image2.move(0, AI_SPEED);
}
}
*/
// AI, z którym nie da się wygrać
// Zamiennie z kodem powyżej
// Jeden z algorytmów musi być w komentarzu: /* .: code :. */
if (ball.getY() < getHeight() - HEIGHT / 2
&& ball.getY() > HEIGHT / 2) { // AI
paddleRight.setLocation(getWidth() - 3 * WIDTH, ball.getY()
- HEIGHT / 2);
image2.setLocation(getWidth() - 3 * WIDTH, ball.getY() - HEIGHT
/ 2);
image2.sendToFront();
} else if (ball.getY() <= HEIGHT / 2) {
paddleRight.setLocation(getWidth() - 3 * WIDTH, 0);
image2.setLocation(getWidth() - 3 * WIDTH, -0);
image2.sendToFront();
} else {
paddleRight.setLocation(getWidth() - 3 * WIDTH, getHeight()
- HEIGHT);
image2.setLocation(getWidth() - 3 * WIDTH, getHeight() - HEIGHT);
image2.sendToFront();
}
if (ballHitBottom(ball) && dy >= 0) {
dy *= -1;
}
if (ballHitTop(ball) && dy <= 0) {
dy *= -1;
}
if (ballHitPaddleRight(ball, paddleRight)) {
dx *= -1;
}
if (ballHitPaddleLeft(ball, paddleLeft)) {
dx *= -1;
COUNTER++;
counter.setLabel(String.valueOf(COUNTER));
counter.setLocation(getWidth() / 2 - counter.getWidth() / 2,
getHeight() / 2 + counter.getHeight() / 3.2);
TRANSPARENCY = 0.1f;
labelC = new Color(0, 0.0f, 0.0f, TRANSPARENCY);
counter.setColor(labelC);
boolean bool = rand.nextBoolean();
if (bool)
if (dx > 0)
dx += 1;
else
dx -= 1;
else if (dy > 0)
dy += 0.5;
else
dy -= 0.5;
}
pause(PAUSE);
if (ballOffScreen(ball)) {
if (ball.getX() + SPEED * 2 < 0) { // left
double pos = ball.getY() - greenFlash.getHeight() / 2;
remove(ball);
remove(ballTexture);
AI_SCORE+=COUNTER;
AiScore.setLabel(String.valueOf(AI_SCORE));
AiScore.setLocation(getWidth()-AiScore.getWidth()-3*WIDTH-10, getHeight()-10);
for (int i = 20; i < 100; i += 5) {
greenFlash.setLocation(-i, pos);
pause(25);
}
} else { // right
double pos = ball.getY() - blueFlash.getHeight() / 2;
remove(ball);
remove(ballTexture);
PLAYER_SCORE+=COUNTER;
PlayerScore.setLabel(String.valueOf(PLAYER_SCORE));
PlayerScore.setLocation(10+3*WIDTH,getHeight()-10);
for (int i = 20; i < 100; i += 5) {
blueFlash.setLocation(getWidth() - blueFlash.getWidth()
+ i, pos);
pause(25);
}
}
COUNTER = 0;
bounce(labelC, AiScore, PlayerScore, counter, ball, paddleLeft,
paddleRight, image, image2, ballTexture, greenFlash,
blueFlash);
}
}
}
private boolean ballHitBottom(GOval ball) {
double bottomY = ball.getY() + ball.getHeight();
return bottomY >= getHeight();
}
private boolean ballHitTop(GOval ball) {
double topY = ball.getY();
return topY <= 0;
}
private boolean ballHitPaddleRight(GOval ball, GRect paddle) {
double rightX = ball.getX() + ball.getWidth();
double rightY = ball.getY() + ball.getHeight() / 2;
double paddlePosX = paddle.getX();
double paddlePosY = paddle.getY();
if (rightX > paddlePosX && rightY > paddlePosY
&& rightY < paddlePosY + paddle.getHeight())
return true;
else
return false;
}
private boolean ballOffScreen(GOval ball) {
if (ball.getX() + SPEED * 2 < 0
|| ball.getX() + ball.getWidth() - SPEED * 2 > getWidth())
return true;
else
return false;
}
private boolean ballHitPaddleLeft(GOval ball, GRect paddle) {
double leftX = ball.getX();
double leftY = ball.getY();
double paddlePosX = paddle.getX() + WIDTH;
double paddlePosY = paddle.getY();
if (leftX < paddlePosX && leftY > paddlePosY
&& leftY < paddlePosY + paddle.getHeight())
return true;
else
return false;
}
}