bricks每次都会改变颜色

6qqygrtg  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(490)

所以,我在 java 做突破(这不是h/w,学校结束了)
我想砖块是随机的颜色,但我的砖块改变颜色,而游戏正在运行。所以现在,看起来砖头都亮起来了!!请帮忙!!

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Random;

public class Brick {
    public static final int X = 0, Y = 0, ROW_SIZE = 8, COL_SIZE = 5;
    private Random random = new Random();
    private ArrayList<Rectangle> arr = new ArrayList<>();

public Brick(int width, int height) {
    for(int i = 0; i < COL_SIZE; i++){
        for(int j = 0; j < ROW_SIZE; j++) {
            Rectangle r = new Rectangle(X + (j * (width / ROW_SIZE)), 
                        Y + (i * (height / COL_SIZE)), (width / ROW_SIZE), (height / COL_SIZE));
            arr.add(r); 
        }
    }
}

public ArrayList<Rectangle> getList(){
    return arr;
}

public void setList(ArrayList<Rectangle> rects){
    arr = rects;
}

public void paint(Graphics g){
    for(Rectangle rect : arr){
        g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }
}

}

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements ActionListener, KeyListener {

Player player = new Player();
Ball ball = new Ball();
Brick brick = new Brick(Pong.WIDTH, Pong.HEIGHT / 3);
JLabel label, gameOverLabel;
Timer time;

public GamePanel() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    time = new Timer(50, this);
    time.start();

    this.addKeyListener(this);
    setFocusable(true);

    // Displaying score
    label = new JLabel();
    gameOverLabel = new JLabel();
    gameOverLabel.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    gameOverLabel.setAlignmentX(CENTER_ALIGNMENT);
    label.setAlignmentX(CENTER_ALIGNMENT);
    add(label);
    add(gameOverLabel);

}

private void update() {
    player.update();

    if (ball.update()) {
        gameOverLabel.setText("GAME OVER");
        time.stop();
    }

    ball.checkCollisionWith(player);
    ball.checkBrickCollision(brick);
}

public void paintComponent(Graphics g) {
    g.setColor(Color.LIGHT_GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());

    player.paint(g);
    ball.paint(g);
    brick.paint(g);
}

public void actionPerformed(ActionEvent e) {
    update();
    label.setText("Score: " + ball.getScore());
    repaint();
}

@Override
public void keyPressed(KeyEvent e) {
    // Speed of player
    int playerVelocity = 8;

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        player.setXVelocity(playerVelocity);
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        player.setXVelocity(-playerVelocity);
    } else if (e.getKeyCode() == KeyEvent.VK_R) {
        ball = new Ball();
        player = new Player();
        ball.setScore(0);
        label.setText("Score: " + ball.getScore());
        gameOverLabel.setText("");
        repaint();
        time.start();
    }
}

@Override
public void keyReleased(KeyEvent e) {
    player.setXVelocity(0);
}

@Override
public void keyTyped(KeyEvent e) {
}

}
请帮帮我!谢谢您。。。

slmsl1lt

slmsl1lt1#

public void paint(Graphics g){
    for(Rectangle rect : arr){
        g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
    }
}

事实上,它在每次重新绘制时都会改变颜色—您在每次调用时都会创建新的随机颜色。我认为你应该在brick构造函数中创建颜色并重用它。

相关问题