2016-06-07 4 views
0

나는 브레이크 아웃 게임을 만들고 있습니다. 두 개의 클래스가 있습니다 : 벽돌 이미지의 배열을 표시하는 벽돌 클래스와 창의 주위에 공 이미지를 이동하는 공 클래스입니다. 나는 공이 벽돌 중 하나에 부딪 칠 때 벽돌을 어떻게 사라지게하는지 알아 내려고 노력 중이다. 어떤 도움이라도 대단히 감사하겠습니다.자바에서 충돌 감지

벽돌 클래스 :

public class Brick { 
    private URL url; 
    private Image brick; 
    Image [][] bricks = new Image[50][3]; 

    public Brick (Breakout bR){ 
     url = bR.getDocumentBase(); 
     brick = bR.getImage(bR.getDocumentBase(),"brick.png"); 
     for(int i =0; i < bricks.length; i++) 
      for(int j = 0; j < bricks[0].length; j++) 
       bricks[i][j] = brick; 
    } 

    public void update(Breakout bR){} 

    public void paint(Graphics g, Breakout bR){ 
     brick = bR.getImage(bR.getDocumentBase(),"brick.png"); 
     int imageWidth = imageWidth = bricks[0][0].getWidth(bR); 
     int imageHeight = imageHeight = bricks[0][0].getHeight(bR); 

     for (int i = 0; i < bricks.length; i++) 
      for (int j =0; j < bricks[0].length; j++) 
       g.drawImage(brick, i * imageWidth + 5, j* imageHeight + 5, bR); 
    } 
} 

볼 등급 : 당신의 도움에 대한

public class Ball { 
    private int x=355 ; 
    private int y=200; 
    private int speed = 8; 
    private int xVel = -speed; 
    private int yVel = speed; 
    private boolean gameOver = false; 

    private Image ball; 

    public Ball (Breakout bR){ 

     ball = bR.getImage(bR.getDocumentBase(),"ball.png"); 


    } 
    public void update(Breakout bR, Paddle p){ 
     x += xVel; 
     y += yVel; 
     if (x < 0){ 
      xVel = speed; 
     } 
     else if (x > bR.getWidth()){ 
      xVel = -speed; 
     } 
     if(y > bR.getHeight()){ 
      gameOver = true; 
     } 
     else if (y < 0){ 
      yVel = speed; 
     } 

     collision(p); 
    } 
    public void collision(Paddle p){ 
     int pX = p.getX(); 
     int pY = p.getY(); 
     int pHeight = p.getImageHeight(); 
     int pWidth = p.getImageWidth(); 

     if (pX<=x && pX+pWidth>=x && pY-pHeight<=y && pY+pHeight>=y){ 
      yVel = -speed; 
     } 
    } 
    public int getX(){ 
     return x; 
    } 
    public int getY(){ 
     return y; 
    } 

    public void paint (Graphics g, Breakout bR){ 
     g.drawImage(ball,x,y,bR); 
     if (gameOver){ 
      g.setColor(Color.WHITE); 
      g.drawString("Game Over", 100,300); 
     } 
    } 
} 

감사합니다 :)

+1

1) 애플릿을 코딩하는 이유는 무엇입니까? 교사가 지정했기 때문에 [CS 교사가 ** Java 애플릿 교육 **을 중단해야하는 이유] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should?hl=ko)를 참조하십시오. -stop-teaching-java-applets /)를 사용합니다. 2) 더 나은 도움을 받으려면 [MCVE] 또는 [Short, Self Contained, Correct Example] (http://www.sscce.org/)를 게시하십시오. 3) 작업 예제 (Java-2D 모양 사용)에 대해서는 [복잡한 모양으로 충돌 감지] (http://stackoverflow.com/a/14575043/418556)를 참조하십시오. –

+1

BTW - AWT 또는 Swing 구성 요소를 사용합니까? –

+1

[복잡한 모양으로 충돌 탐지] 가능한 중복 (http://stackoverflow.com/questions/14574045/collision-detection-with-complex-shapes) –

답변

0

당신은 Rectangle.intersects을 사용할 수 있습니다().

볼 및 브릭 클래스 모두에 대해 getBounds() 메서드를 만듭니다. 이렇게하면 엔티티를 둘러싼 가상 직사각형이 생성됩니다. 당신이 그것을 필요로하는 경우에

if(ball.getBounds().intersects(brick.getBounds())){ 
    doSomething(); 
} 

더 많은 정보 here :

public Rectangle getBounds(){ 
    return new Rectangle(x, y, ballSizeX, ballSizeY); 
} 

는 그런 충돌을 검출하는 것처럼 보일 것이다.