所以,我用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) {
}
}
請幫助!謝謝...
什麼是repaint() – Kode
repaint()使遊戲在經過第一次迭代後刷新。如果我要評論重繪(),遊戲將開始,但球和玩家不會移動。 –
我相信我的問題在於我對磚的數組列表進行for循環,並將顏色設置爲隨機。因此,每當編譯器通過磚塊,它會改變顏色... –