点击它们使正方形消失

时间:2014-10-23 12:35:30

标签: java actionlistener

我是Java新手,我正在做一个简单的游戏。

游戏涉及很多方块四处移动,你必须点击它们才能消失,但我不知道该怎么做。我制作了所有的正方形,但是当它们(用鼠标)单击时,它们就会消失,这是我唯一需要的东西。怎么做?

以下是整个游戏的代码:

public static void main(String[] args) {
    SimpleWindow w = new SimpleWindow (800, 800, "Square Killing");

        Square sq1 =  new Square (50, 50, 20);    // Fyrkant nr 1
        Square sq2 =  new Square (100, 100, 20);  // Fyrkant nr 2
        Square sq3 =  new Square (350, 350, 20);  // Fyrkant nr 3
        Square sq4 =  new Square (600, 600, 20);  // Fyrkant nr 4
        Square sq5 =  new Square (20, 20, 20);    // Fyrkant nr 5
        Square sq6 =  new Square (250, 250, 20);  // Fyrkant nr 6
        Square sq7 =  new Square (405, 405, 20);  // Fyrkant nr 7
        Square sq8 =  new Square (591, 591, 20);  // Fyrkant nr 8
        Square sq9 =  new Square (110, 110, 20);  // Fyrkant nr 9
        Square sq10 = new Square (230, 203, 20);  // Fyrkant nr 10
        Square sq11 = new Square (340, 340, 20);  // Fyrkant nr 11
        Square sq12 = new Square (320, 320, 20);  // Fyrkant nr 12
             int dx = 20; //F nr 1 hastighet x-led
             int dy = 4;  //F nr 1 hastighet y-led

             int ex = 10; //F nr 2 hastighet x-led
             int ey = 6;  //F nr 2 hastighet y-led

             int fx = 15; //F nr 3 hastighet x-led
             int fy = 3;  //F nr 3 hastighet y-led

             int sx = 5;  //F nr 4 hastighet x-led
             int sy = 12; //F nr 4 hastighet y-led

             int xx = 8;  //F nr 5 hastighet x-led
             int xy = 2;  //F nr 5 hastighet y-led

             int tx = 4;  //F nr 6 hastighet x-led
             int ty = 11; //F nr 6 hastighet y-led

             int lx = 6;  //F nr 7 hastighet x-led
             int ly = 16; //F nr 7 hastighet y-led

             int rx = 1;  //F nr 8 hastighet x-led
             int ry = 10; //F nr 8 hastighet y-led

             int gx = 10; //F nr 9 hastighet x-led
             int gy = 13; //F nr 9 hastighet y-led

             int hx = 4;  //F nr 10 hastighet x-led
             int hy = 15; //F nr 10 hastighet y-led

             int jx = 5;  //F nr 11 hastighet x-led
             int jy = 14; //F nr 11 hastighet y-led

             int ux = 3;  //F nr 12 hastighet x-led
             int uy = 13; //F nr 12 hastighet y-led

             while(true){
            if(sq1.getX() < 20 || sq1.getX() + sq1.getSide() > w.getWidth())
                dx = -dx;
            if(sq1.getY() < 20 || sq1.getY() + sq1.getSide() > w.getHeight())
                dy = -dy;
        w.clear();
        sq1.draw(w);
        sq1.move(dx, dy);

            if(sq2.getX() < 20 || sq2.getX() + sq2.getSide() > w.getWidth())
                ex = -ex;
            if(sq2.getY() < 20 || sq2.getY() + sq2.getSide() > w.getHeight())
                ey = -ey;
        sq2.draw(w);
        sq2.move(ex, ey);
        w.delay(2);

            if(sq3.getX() < 20 || sq3.getX() + sq3.getSide() > w.getWidth())
                fx = -fx;
            if(sq3.getY() < 20 || sq3.getY() + sq3.getSide() > w.getHeight())
                fy = -fy;
        sq3.draw(w);
        sq3.move(fx, fy);
        w.delay(2);

            if(sq4.getX() < 20 || sq4.getX() + sq4.getSide() > w.getWidth())
                sx = -sx;
            if(sq4.getY() < 20 || sq4.getY() + sq4.getSide() > w.getHeight())
                sy = -sy;
        sq4.draw(w);
        sq4.move(sx, sy);
        w.delay(1);

            if(sq5.getX() < 20 || sq5.getX() + sq5.getSide() > w.getWidth())
                xx = -xx;
            if(sq5.getY() < 20 || sq5.getY() + sq5.getSide() > w.getHeight())
                xy = -xy;
        sq5.draw(w);
        sq5.move(xx, xy);
        w.delay(2);

            if(sq6.getX() < 20 || sq6.getX() + sq6.getSide() > w.getWidth())
                tx = -tx;
            if(sq6.getY() < 20 || sq6.getY() + sq6.getSide() > w.getHeight())
                ty = -ty;
        sq6.draw(w);
        sq6.move(tx, ty);
        w.delay(2);


            if(sq7.getX() < 20 || sq7.getX() + sq7.getSide() > w.getWidth())
                lx = -lx;
            if(sq7.getY() < 20 || sq7.getY() + sq7.getSide() > w.getHeight())
                ly = -ly;
        sq7.draw(w);
        sq7.move(lx, ly);
        w.delay(1);


            if(sq8.getX() < 20 || sq8.getX() + sq8.getSide() > w.getWidth())
                rx = -rx;
            if(sq8.getY() < 20 || sq8.getY() + sq8.getSide() > w.getHeight())
                ry = -ry;
        sq8.draw(w);
        sq8.move(rx, ry);
        w.delay(2);


            if(sq9.getX() < 20 || sq9.getX() + sq9.getSide() > w.getWidth())
                gx = -gx;
            if(sq9.getY() < 20 || sq9.getY() + sq9.getSide() > w.getHeight())
                gy = -gy;
        sq9.draw(w);
        sq9.move(gx, gy);
        w.delay(2);


            if(sq10.getX() < 20 || sq10.getX() + sq10.getSide() > w.getWidth())
                hx = -hx;
            if(sq10.getY() < 20 || sq10.getY() + sq10.getSide() > w.getHeight())
                hy = -hy;
        sq10.draw(w);
        sq10.move(hx, hy);
        w.delay(2);


            if(sq11.getX() < 20 || sq11.getX() + sq11.getSide() > w.getWidth())
                jx = -jx;
            if(sq11.getY() < 20 || sq11.getY() + sq11.getSide() > w.getHeight())
                jy = -jy;
        sq11.draw(w);
        sq11.move(jx, jy);
        w.delay(2);


            if(sq12.getX() < 20 || sq12.getX() + sq12.getSide() > w.getWidth())
                ux = -ux;
            if(sq12.getY() < 20 || sq12.getY() + sq12.getSide() > w.getHeight())
                uy = -uy;
        sq12.draw(w);
        sq12.move(ux, uy);
        w.delay(2);

        switch ((int) (Math.random() *12)) { //De olika färgerna på rutorna
            case 1:  w.setLineColor(java.awt.Color.RED); break;
            case 2:  w.setLineColor(java.awt.Color.BLUE); break;
            case 3:  w.setLineColor(java.awt.Color.GREEN); break;
            case 4:  w.setLineColor(java.awt.Color.PINK); break;
            case 5:  w.setLineColor(java.awt.Color.YELLOW); break;
            case 6:  w.setLineColor(java.awt.Color.CYAN); break;
            case 7:  w.setLineColor(java.awt.Color.ORANGE); break;
            case 9:  w.setLineColor(java.awt.Color.LIGHT_GRAY); break;


        }
    }
}
}

0 个答案:

没有答案