From 46e689dec3feb3dc5d544abda07249be9c3b6f9a Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Fri, 13 Dec 2019 01:14:58 +0100 Subject: [PATCH] ball - new color when ball hits paddle --- src/break_out/Constants.java | 5 +++ src/break_out/model/Ball.java | 60 ++++++++++++++++++++++++++++++++++ src/break_out/model/Level.java | 13 ++++++++ src/break_out/view/Field.java | 9 ++++- 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/break_out/Constants.java b/src/break_out/Constants.java index 0ee8596..46a9655 100644 --- a/src/break_out/Constants.java +++ b/src/break_out/Constants.java @@ -74,6 +74,11 @@ public class Constants { */ public static final Double BALL_SPEED = 1.20; + /** + * The default component color + */ + public static final Color COLOR_COMPONENT = new Color(200, 200, 200); + /** * The component color for the bottom paddle */ diff --git a/src/break_out/model/Ball.java b/src/break_out/model/Ball.java index 4e48bac..5d2f68b 100644 --- a/src/break_out/model/Ball.java +++ b/src/break_out/model/Ball.java @@ -2,6 +2,9 @@ package break_out.model; import break_out.Constants; +import java.awt.Color; +import java.util.Random; + /** * This class contains the information about the balls characteristics and behavior * @@ -19,6 +22,16 @@ public class Ball implements IBall { */ private Vector2D direction; + /** + * The balls hit state for paddles; custom implementation + */ + private boolean hitsPaddle; + + /** + * The balls color with default component color + */ + private Color color = Constants.COLOR_COMPONENT; + /** * The constructor of a ball * The balls position and direction are initialized here. @@ -51,6 +64,53 @@ public class Ball implements IBall { return this.direction; } + /** + * The getter for the balls color + * + * @return color The balls current color + */ + public Color getColor() { return this.color; } + + /** + * The setter for the balls color + * + * @param color The balls new color + */ + public void setColor(Color color) { this.color = color; } + + /** + * Creates new random color for the ball and sets it + * + * @see Stackoverflow Answer 4247219 + */ + public void newRandomColor() { + Random random = new Random(); + + // random hue without red colors + float hue = (random.nextInt(18000) + 6000) / 10000f; + + // saturation between 0.5 and 0.7 + float saturation = (random.nextInt(2000) + 5000) / 10000f; + float luminance = 0.9f; + Color randColor = Color.getHSBColor(hue, saturation, luminance); + + setColor(randColor); + } + + /** + * The getter for the balls hit state + * + * @return hitsPaddle The balls current hit state + */ + public boolean getHitState() { return this.hitsPaddle; } + + /** + * The setter for the balls hit state + * + * @param state The balls new hit state + */ + public void setHitState(boolean state) { this.hitsPaddle = state; } + /** * updates ball position */ diff --git a/src/break_out/model/Level.java b/src/break_out/model/Level.java index b4b7d9a..36aa2ae 100644 --- a/src/break_out/model/Level.java +++ b/src/break_out/model/Level.java @@ -122,6 +122,19 @@ public class Level extends Thread implements ILevel { // if ball hits paddle (top|bottom), reflect ball if(this.ball.hitsPaddle(paddleTop)) this.ball.reflectOnPaddle(paddleTop); if(this.ball.hitsPaddle(paddleBottom)) this.ball.reflectOnPaddle(paddleBottom); + if(this.ball.hitsPaddle(paddleTop) || this.ball.hitsPaddle(paddleBottom)) { + this.ball.setHitState(true); + // DEBUG OUTPUT + //System.out.println(String.format("hitstate: %s, color: %s", this.ball.getHitState(), this.ball.getColor().getRGB())); + } + + // if ball has hit a paddle set a new random color to it + else if(this.ball.getHitState()) { + this.ball.setHitState(false); + this.ball.newRandomColor(); + } + + // Tells the observer to repaint the components on the playground diff --git a/src/break_out/view/Field.java b/src/break_out/view/Field.java index b3c62e5..127543c 100644 --- a/src/break_out/view/Field.java +++ b/src/break_out/view/Field.java @@ -94,7 +94,7 @@ public class Field extends JPanel { g2.fillRect(0, 0, getWidth(), getHeight()); // Setting the color for the following components - g2.setColor(new Color(200, 200, 200)); + g2.setColor(Constants.COLOR_COMPONENT); // Calls the method for drawing the ball drawBall(g2); @@ -116,10 +116,17 @@ public class Field extends JPanel { * @param g2 The graphics object */ private void drawBall(Graphics2D g2) { + // temporarily save default component color to draw ball in specific color + Color temp = g2.getColor(); + g2.setColor(view.getGame().getLevel().getBall().getColor()); + g2.fillOval((int) view.getGame().getLevel().getBall().getPosition().getX(), (int) view.getGame().getLevel().getBall().getPosition().getY(), Constants.BALL_DIAMETER, Constants.BALL_DIAMETER); + + // reset color to default + g2.setColor(temp); } /**