碰撞后删除现有的矩形

时间:2018-04-11 07:23:14

标签: java swing graphics

我正在创建Brick Breakers,我已经为多行(xCord-etc)创建了数组,yCords用于绘制和跟踪每个矩形的点。正如你所看到的,我做了一个for循环来遍历每个x坐标并检测到球与矩形相交。我的问题是我无法弄清楚如何在与球相交时移除一个矩形。我一直在使用clearRect并尝试将其设置为零以获得宽度和高度,但它所做的只是在碰撞时将其颜色更改为白色。

有什么建议吗?

    import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Timer;

import javax.swing.JPanel;
import java.util.*;


    public class game extends JPanel implements KeyListener, ActionListener {
        private static final Component Rectangle = null;
        //private static final String  = null;
        private Timer timer;
        private boolean play = false;
        private static int playerx = 650,playerY=930,panW=250,panH=15;
        private static int ballx=900, bally=600, ballxdir=-1,ballydir=-2,ballW=30,ballH=30;

    int []yCord = {0,80,160,240,320,400,480,40,120,200,280,360,440,530};// Rows of Bricks^
    int[] xCord= {1,210,420,630,840,1050,1260}; // Horizontal xPos bricks <-->
    int[] secRow ={100,310,520,730,940,1150};
    int[] xRow = {200,410,620,830,1040};
    int[] fRow = {300,510,720,930};
    int[]sixRow= {400,610,820};
    int[]sevRow = {500,710};
    int[]finRow = {600};




        for(int loc=0;loc<7; loc++) {  //top Row
                g.setColor(Color.lightGray);
            g.fillRect(xCord[loc], yCord[0], 200, 30);
            g.fillRect(xCord[loc], yCord[7], 200, 30);
            g.setColor(Color.yellow);
            g.fillRect(xCord[loc],yCord[13], 200, 30);  //second row
            if (new Rectangle(ballx, bally,ballW,ballH).intersects  //detects intersection
                    (new Rectangle(xCord[loc],yCord[0],200,30))){ 
                System.out.print(xCord[loc]+" Brick Hit\n");            
                g.clearRect(xCord[loc], yCord[0], 200, 30);
            }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(xCord[loc],yCord[7],200,30))){
                g.clearRect(xCord[loc], yCord[7], 200, 30);
                repaint();
                }else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(xCord[loc],yCord[13],200,30))){
                g.clearRect(xCord[loc], yCord[13], 200, 30);

                }
        //      ballydir = -ballydir;
            }
            for (int loc=0; loc<6; loc++) {
                g.setColor(Color.GREEN);
                g.fillRect(secRow[loc], yCord[1], 200, 30);
                g.fillRect(secRow[loc], yCord[8], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                    (new Rectangle(secRow[loc],yCord[1],200,30))) { 
                    System.out.print(secRow[loc]+" Brick Hit\n");
                    g.clearRect(secRow[loc], yCord[1], 200, 30);
            //      ballydir = -ballydir;
                }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(secRow[loc],yCord[8],200,30))){
                        g.clearRect(secRow[loc], yCord[8], 200, 30);
                        repaint();
                        }
                        }

            for (int loc=0; loc<5; loc++) {
                g.setColor(Color.MAGENTA);
                g.fillRect(xRow[loc],yCord[2], 200, 30);
                g.fillRect(xRow[loc], yCord[9], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(xRow[loc],yCord[2],200,30))) { 
                    System.out.print(xRow[loc]+" Brick Hit\n");
                    g.clearRect(xRow[loc], yCord[2], 200, 30);
            //      ballydir = -ballydir;
                }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(xRow[loc],yCord[9],200,30))){
                        g.clearRect(xRow[loc], yCord[9], 200, 30);
                        repaint();
                        }
            }
            for (int loc=0; loc<4; loc++) {
                g.setColor(Color.BLUE);
                g.fillRect(fRow[loc], yCord[3], 200, 30);
                g.fillRect(fRow[loc],yCord[10], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(fRow[loc],yCord[3],200,30))) { 
                    System.out.print(fRow[loc]+" Brick Hit\n");
                    g.clearRect(fRow[loc], yCord[3], 200, 30);
                }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(fRow[loc],yCord[10],200,30))){
                        g.clearRect(fRow[loc], yCord[10], 200, 30);
                        repaint();
                        }
            }
            for (int loc=0; loc<3; loc++) {
                g.setColor(Color.PINK);
                g.fillRect(sixRow[loc], yCord[4], 200, 30);
                g.fillRect(sixRow[loc],yCord[11], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(sixRow[loc],yCord[4],200,30))) { 
                    System.out.print(sixRow[loc]+" Brick Hit\n");
                    g.clearRect(sixRow[loc], yCord[4], 200, 30);
            //      ballydir = -ballydir;
                }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(sixRow[loc],yCord[11],200,30))){
                        g.clearRect(sixRow[loc], yCord[11], 200, 30);
                        repaint();
                        }
            }
            for (int loc=0; loc<2; loc++) {
                g.setColor(Color.RED);
                g.fillRect(sevRow[loc], yCord[5], 200, 30);
                g.fillRect(sevRow[loc],yCord[12], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(sevRow[loc],yCord[5],200,30))) { 
                    System.out.print(sevRow[loc]+" Brick Hit\n");
                    g.clearRect(sevRow[loc], yCord[5], 200, 30);
            //      ballydir = -ballydir;
                }
                else if(new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(sevRow[loc],yCord[12],200,30))){
                        g.clearRect(sevRow[loc], yCord[12], 200, 30);
                        repaint();
                        }
            }
            for (int loc=0; loc<1; loc++) {
                g.setColor(Color.RED);
                g.fillRect(finRow[loc], yCord[6], 200, 30);
                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                        (new Rectangle(finRow[loc],yCord[6],200,30))) { 
                    System.out.print(finRow[loc]+" Brick Hit\n");
                    g.clearRect(finRow[loc], yCord[6], 200, 30);
                    ballydir = -ballydir;
                }

            }


        g.dispose();
        //repaint();

    }


     public void right() {  //left and right methods for keyboard inputs
            play = true;
            playerx += 40;
        }
        public void left() {
            play = true;
            playerx -=40;
        }



        @Override
        public void keyPressed(KeyEvent e) {  //user Keyboard INPUTS L,R
            int key = e.getKeyCode();
            if(key ==KeyEvent.VK_LEFT) {
                //System.out.print("Left\n");
                left();
            }if (key == KeyEvent.VK_RIGHT) {
                //System.out.print("Right\n");
                right();
            }



        }
        @Override
        public void actionPerformed(ActionEvent e) { //basic GAMEPLAY BOUNDS collision
    // implement code for collision against panel and right wall 
    // IF ball goes out of bounds (ground level) display GAME OVER
            timer.start();
            if (play) {


                ballx +=ballxdir;
                bally -=ballydir;

                if (new Rectangle(ballx, bally,ballW,ballH).intersects
                    (new Rectangle(playerx,playerY,panW,panH))) { 

                    System.out.print("HIT");
                    ballydir = -ballydir;
                }


                //ballydir = +ballydir;
                //left wall collision
                if (ballx <1) {
                    ballxdir =-ballxdir+1;
                    System.out.print("SPEED INCREASE: "+ballxdir+" LEFT HIT\n");
                }
                //RIGHT wall collision
                if (ballx>1455) {
                    ballxdir= -ballxdir+1;
                    System.out.print("SPEED INCREASE: "+ballxdir+ " RIGHT wall HIT\n");
                }
                //GROUND collision(collision not needed) Change to display JFRAME of "GAME OVER"
                if (bally>925) {
    //          bally=500;//start position
    //          ballx=900;  
                System.exit(0);
                }

                //celling collision
                if (bally <-1) {
                    ballydir =-ballydir-1;
                    System.out.print("SPEED INCREASE: "+ballydir+" Celling HIT\n");
                }
                //PANEL OFF SCREEN PREVENTION BOUNDS
                if(playerx>1223) {
                    playerx=1223;
                    System.out.print("Out Of Bounds\n");
                }if (playerx<1) {
                    playerx =1;
                    System.out.print("Out Of Bounds\n");



                }

            }
            repaint();

        }

0 个答案:

没有答案