Reputation: 11
My ball code is here: I don't know how to make the ball change color when he hit the wall. What to do if we want to change the color randomly of the ball every time it bounces off the wall?
//Ball.java
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
private static final int DIAMETER = 30; //diametrul mingii
private static final int RECTANGLE = 30;
private static final int WIDTH = 50; //Pallet width
private static final int HEIGHT = 50; //Pallet height
int x = 0; //The initial position of the ball, up
int y = 0; //The initial position of the ball, left
int xa = 1;
int ya = 1;
private Game game;
private int score=0;
public Ball(Game game) {
this.game= game;
}
void move() {
//Each if limits a border of the window
if (x + xa < 0)
xa = 1; //The ball moves to the right with one pixel each round
if (x + xa > game.getWidth() - DIAMETER) //When the ball exceeds the edge, we change direction
xa = -1;
if (y + ya < 0)
ya = 1;
if (y + ya > game.getHeight() - DIAMETER) // When the ball exceeds the bottom edge of the window,
if (collision()){ //mingea se deplaseaza in sus, daca se intersecteaza cu jucatorul
ya = -1;
y = game.player.getTopY() - DIAMETER; //plasam mingea deasupra jucatorului,
//For the rectangles they are in, do not intersect
}
x = x + xa; //Make the trips above
y = y + ya;
}
private boolean collision() {
return game.player.getBounds().intersects(getBounds()); //returneaza true daca dreptunghiul
}
public void paint(Graphics2D g) {
g.fillOval(x, y, DIAMETER, DIAMETER);
}
public void paintComponent(Graphics g) {
g.fillRect(x, y, RECTANGLE, RECTANGLE );
}
public Rectangle getBounds() {
return new Rectangle(x, y, DIAMETER, DIAMETER);
}
}
I appreciate the help.
Upvotes: 0
Views: 2356
Reputation: 1293
Easy solution: add a new boolean to know if there has been a collision, for example boolean coll = false
. In your if (collision())
statement, add coll = true
. Then change this:
public void paint(Graphics2D g) {
g.fillOval(x, y, DIAMETER, DIAMETER);
}
to this:
public void paint(Graphics2D g) {
if (coll){
Random r = new Random();
g.setColor(new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
coll = false;
}
g.fillOval(x, y, DIAMETER, DIAMETER);
}
This might need you to import the Random package, and something might be wrong, I don't remember and I can't test it now, sorry, but in general that's the idea.
Upvotes: 2
Reputation: 1159
All you need it to change the color of your graphics when the ball touches the wall, according to your comments the collision with the wall occurs here:
if (x + xa > game.getWidth() - DIAMETER) //When the ball exceeds the edge, we change direction
xa = -1;
So you will need an extra action to do which is:
if (x + xa > game.getWidth() - DIAMETER) { //When the ball exceeds the edge, we change direction
xa = -1;
g.setColor(Color.red); //or any other color
}
Please note that you will need to pass that g
element into the move
function in order to manipulate it, as you do it in your paint
function. One more thing, is that you will need to add the color changing line into any collision be it walls, floor, or ceiling. As for making the color random you should be able to achieve it yourself with a bit of effort
Upvotes: 0