之前已经问过这个问题。我已经看到了答案,但对我来说并不适用。我正在创建一个Breakout游戏。我已经创造了球,球拍和砖块。我已经实现了如何让球弹出墙壁和第一排砖块。
我无法弄清楚如何让砖块被球击中后消失。我试图将砖块存储在一个数组中,所以我可以将它从数组中移除,当它被球击中时。非常感谢帮助。
这是我到目前为止的代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.io.*;
import sun.audio.*; //Used to play sounds
public class Breakout {
//Creating all the things we will need
public static JPanel display = new DisplayPanel();
public static JPanel controls = new JPanel();
public static JLabel scoreNum;
public static JLabel livesNum;
public static int xPos = 250, score = 0, lives = 3;
public static Boolean grabbed = false;
public static int cx, cy;
public static int bx, by, angle;
public static int[] bricks = new int[10];
public static Timer time = new Timer(20,new Ticker());
public static void main(String[] args)
{
Breakout bo = new Breakout();
}
public Breakout()
{
//This part should be quite familiar by now
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(650,550));
frame.setForeground(Color.black);
frame.setTitle("Breakout");
//Display panel data
display.setPreferredSize(new Dimension(500,550));
display.setBackground(Color.WHITE);
//Bricks
//Control components
JLabel title = new JLabel("BREAKOUT");
title.setHorizontalAlignment(SwingConstants.CENTER);
JLabel scoreLbl = new JLabel(" Score: ");
scoreNum = new JLabel(" "+score);
JLabel livesLbl = new JLabel(" Lives: ");
livesNum = new JLabel(" "+lives);
//Control panel data
controls.setPreferredSize(new Dimension(133,550)); //Not exact, to account for bordering
controls.setBackground(Color.LIGHT_GRAY);
controls.setLayout(new GridLayout(25,1)); //The no. of rows can control the button height
controls.add(title);
controls.add(scoreLbl);
controls.add(scoreNum);
controls.add(livesLbl);
controls.add(livesNum);
display.addMouseListener(new MouseSensor());
display.addMouseMotionListener(new MouseSensor());
frame.setLayout(new BorderLayout());
frame.add(controls,BorderLayout.WEST);
frame.add(display,BorderLayout.EAST);
frame.setVisible(true);
initBall();
beep();
time.start();
}
public static void initBall()
{
bx = 150 + (int)(Math.random() * (100));
by = 50 + (int)(Math.random() * (200));
angle = (int)(Math.random() * (2));
}
public static void beep()
{
//This program requires a sound file in the directory
//that you use for external files.
//You can use ANY sound that you give the name below,
//but the one I use is found here:
//http://www.soundjay.com/button/beep-2.wav
try {
AudioPlayer p = AudioPlayer.player;
AudioStream snd = new AudioStream(new FileInputStream("beep-2a.wav"));
p.start(snd); //This sequence is used to play sounds in JAVA
}
catch(IOException e) {}
}
public static class DisplayPanel extends JPanel
{
public void paintComponent (Graphics g)
{
int num_rows = 2;
int num_cols = 5;
int y_offset = 10;
int x_offset = 20;
int brick_height = 30;
int brick_width = 75;
int brick_sep = 20;
super.paintComponent(g);
g.setColor(Color.green);
for (int i = 0; i < num_rows; i++){
int y = y_offset + (i * (brick_height + brick_sep));
for (int j = 0; j < num_cols; j++){
int x = (x_offset) + (j * (brick_width + brick_sep));
g.fillRect (x, y, brick_width, brick_height );
}
}
if (grabbed)
g.setColor(Color.blue); //Highlight if active
else
g.setColor(Color.black);
g.fillRect(xPos-30,480,60,15); //Draw paddle at current location
if(lives > 0)
g.setColor(Color.red);
else
g.setColor(Color.white); //Ball is "invisible" if game over
g.fillOval(bx,by,14,14);
}
}
public static class MouseSensor extends MouseInputAdapter {
public MouseSensor() {}
public void mousePressed(MouseEvent e)
{
cx = e.getX();
cy = e.getY();
if ((cy > 470)&&(cy < 490)&&(cx > xPos-30)&&(cx < xPos+30))
grabbed = true; //If you click on the paddle, grab it!
display.repaint();
}
public void mouseReleased(MouseEvent e)
{
grabbed = false;
display.repaint();
}
public void mouseDragged(MouseEvent e)
{
cx = e.getX();
if (grabbed)
{
if (cx < 30)
cx = 30;
if (cx > 470)
cx = 470;
xPos = cx;
}
display.repaint(); //Move the grabbed paddle with the mouse
}
}
public static class Ticker implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Angle 0: upper right, Angle 1: upper left
//Angle 2: lower right, Angle 3: lower left
int d = 5; //Displacement: No. of pixels to move per tick of timer
if(angle == 0)
{ bx+=d; by-=d; } //Add to x, reduce y; moves upper right
else if (angle == 1)
{ bx-=d; by-=d; } //Reduce x, reduce y; moves upper left
else if (angle == 2)
{ bx+=d; by+=d; } //You get the idea
else if (angle == 3)
{ bx-=d; by+=d; } //Ditto
//Collision Detection
if (bx > 482) //Hitting the right wall
{
if (angle == 0) //If going upper-right...
angle = 1; //go upper-left after bouncing
else if (angle == 2) //If going lower-right...
angle = 3; //go lower-left after hitting right wall
}
else if (bx < 3) //Hitting left wall
{
if (angle == 1)
angle = 0;
else if (angle == 3)
angle = 2;
}
else if (by < 3) //Hitting top
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
//Note; no detection at bottom, since that's how you lose
else if ((by > 460)&&(by < 480)) //Deflection height of paddle
{
if ((bx > (xPos-30))&&(bx < (xPos+30))) //Deflected by paddle
{
Toolkit.getDefaultToolkit().beep(); //Play sound
if (angle == 2)
angle = 0;
else if (angle == 3)
angle = 1;
score+= 10;//Modify this for your assignment (see notes)
scoreNum.setText(" "+score);
}
}
else if ((by < 90)&& (bx > 20) && (bx < 95))//Block 1
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 115) && (bx < 190))//Block 2
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 210) && (bx < 285))//Block 3
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 305) && (bx < 380))//Block 4
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 400) && (bx < 475))//Block 5
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if (by > 530) //Missed; ball goes too low in y-value
{
lives--;
if (lives > 0)
{
livesNum.setText(" "+lives);
initBall();
}
else
{
time.stop();
livesNum.setText(" GAME OVER");
}
}
display.repaint();
}
}
}
这是我创建砖块的代码
g.setColor(Color.green);
for (int i = 0; i < num_rows; i++){
int y = y_offset + (i * (brick_height + brick_sep));
for (int j = 0; j < num_cols; j++){
int x = (x_offset) + (j * (brick_width + brick_sep));
g.fillRect (x, y, brick_width, brick_height );
}
当我击球时,我想要做的就是消失,球也会反弹。
以下是球反弹的代码:
else if ((by < 90)&& (bx > 20) && (bx < 95))//Block 1
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 115) && (bx < 190))//Block 2
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 210) && (bx < 285))//Block 3
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 305) && (bx < 380))//Block 4
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
else if ((by < 90)&& (bx > 400) && (bx < 475))//Block 5
{
if (angle == 0)
angle = 2;
else if (angle == 1)
angle = 3;
}
我不知道如何让他们在被球击中时消失。