如何在Java中创建突破游戏

如何在Java中创建突破游戏,java,Java,这个问题以前有人问过。我已经看到了答案,但对我来说不太合适。我正在创建一个突破游戏。我已经创造了球、桨和砖。我已经实现了如何使球在墙壁和第一排砖上反弹 我不知道如何让砖块在被球击中后消失。我试图将砖块存储在阵列中,因此当它被球击中时,我可以将其从阵列中移除。非常感谢你的帮助 这是我到目前为止的代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; impor

这个问题以前有人问过。我已经看到了答案,但对我来说不太合适。我正在创建一个突破游戏。我已经创造了球、桨和砖。我已经实现了如何使球在墙壁和第一排砖上反弹

我不知道如何让砖块在被球击中后消失。我试图将砖块存储在阵列中,因此当它被球击中时,我可以将其从阵列中移除。非常感谢你的帮助

这是我到目前为止的代码

     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();
                }
            }
        }
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.MouseInputAdapter;
导入java.io.*;
导入sun.audio.*//用来播放声音
公开课突破{
//创造我们所需要的一切
publicstaticjpanel display=newdisplaypanel();
public static JPanel controls=new JPanel();
公共静态JLabel scoreNum;
公共静态jlabellivesnum;
公共静态int xPos=250,分数=0,寿命=3;
公共静态布尔值=false;
公共静态int cx,cy;
公共静态int bx,by,角度;
公共静态int[]砖块=新int[10];
公共静态计时器时间=新计时器(20,new Ticker());
公共静态void main(字符串[]args)
{
断开bo=新断开();
}
公众突破()
{        
//这部分现在应该很熟悉了
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(新尺寸(650550));
帧。设置前景(颜色。黑色);
框架。设置标题(“突破”);
//显示面板数据
显示.setPreferredSize(新尺寸(500550));
显示.背景(颜色.白色);
//砖块
//控制元件
JLabel标题=新JLabel(“突破”);
标题.设置水平对齐(SwingConstants.中心);
JLabel scoreLbl=新JLabel(“分数:”);
scoreNum=新的JLabel(“+”分数);
JLabel livesLbl=新的JLabel(“Lives:”);
livesNum=新的JLabel(“+lives”);
//控制面板数据
controls.setPreferredSize(新维度(133550));//不精确,用于说明边界
控件。背景(颜色。浅灰色);
controls.setLayout(新的GridLayout(25,1));//行数可以控制按钮高度
控件。添加(标题);
添加(scoreLbl);
添加(scoreNum);
控件。添加(livesLbl);
控件。添加(livesNum);
addMouseStener(新的MouseSensor());
display.addMouseMotionListener(新的MouseSensor());
frame.setLayout(新的BorderLayout());
frame.add(控件,BorderLayout.WEST);
框架。添加(显示,边框布局。东);
frame.setVisible(true);
initBall();
beep();
time.start();
}
公共静态void initBall()
{
bx=150+(int)(Math.random()*(100));
by=50+(int)(Math.random()*(200));
角度=(int)(Math.random()*(2));
}
公共静态无效哔哔声()
{
//此程序需要目录中的声音文件
//用于外部文件的。
//您可以使用您在下面给出的任何声音,
//但我使用的是:
//http://www.soundjay.com/button/beep-2.wav
试试{
AudioPlayer p=AudioPlayer.player;
AudioStream snd=新的AudioStream(新文件输入流(“beep-2a.wav”));
p、 start(snd);//此序列用于在JAVA中播放声音
}
捕获(IOE){}
}
公共静态类DisplayPanel扩展了JPanel
{
公共组件(图形g)
{   
int num_rows=2;
int num_cols=5;
int y_偏移=10;
int x_偏移=20;
内砖高度=30;
整块砖的宽度=75;
int brick_sep=20;
超级组件(g);
g、 setColor(Color.green);
对于(int i=0;i0)
g、 setColor(Color.red);
其他的
g、 setColor(Color.white);//如果游戏结束,球“不可见”
g、 圆角(bx,by,14,14);
}
}
公共静态类MouseSensor扩展MouseInputAdapter{
公共鼠标传感器(){}
公共无效鼠标按下(MouseEvent e)
{
cx=e.getX();
cy=e.getY();
    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;
            }