2016-06-12 6 views
0

나는 브레이크 아웃 게임을하고 있으며 벽돌 충돌을 제외하고는 거의 모든 작업을 수행했습니다. 공이 벽, 패들 및 벽돌로 튀어 오릅니다. 그러나 공이 벽돌을 튀길 때 벽돌은 사라지지 않습니다. 나는 정말로 이것에 붙어있다. 어떤 아이디어라도 대단히 감사하겠습니다. 나는 벽돌, 볼과 주요 클래스가 무엇 :Java에서 브레이크 아웃 벽돌 충돌

벽돌 등급 :

import java.awt.*; 
import java.applet.*; 
public class Brick2{ 
    private int x; 
    private int y; 
    public Brick2(int X, int Y){ 
     x =X; 
     y=Y; 
    } 
    public void update(Ball ba){ 
     collision(ba); 
    } 
    public void collision(Ball ba){ 
     int bX = ba.getX(); 
     int bY = ba.getY(); 
     int bHeight = ba.getImageHeight(); 
     int bWidth = ba.getImageWidth(); 
     for(int x=0; x <= 600; x+=55){ 
      for (int y=0; y<= 100; y+=25){ 
       if (bX-bWidth<=x && bX+bWidth>=x && bY-bHeight<=y && bY+bHeight>=y){ 
        ba.setXVel(6); 

       } 
      } 
     } 
     } 
     public int getX(){ 
     return x; 
    } 
    public int getY(){ 
     return y; 
    } 
    public void paint(Graphics g){ 
     for(int x=0; x <= 800; x+=55){ 
      for (int y=0; y<= 100; y+=25){ 
       g.setColor(Color.RED); 
       g.fillRect(x,y,50,20); 
      } 
     } 
    } 
} 

볼 등급 :

import java.awt.*; 
import java.applet.*; 
public class Ball { 
    private int x=355 ; 
    private int y=200; 
    private int speed = 6; 
    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 int getImageWidth(){ 
     return ball.getWidth(null); 
    } 
    public int getImageHeight(){ 
     return ball.getHeight(null); 
    } 
    public void setXVel(int xv){ 
     yVel = xv; 
    } 
    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); 
     } 
    } 
} 

메인 클래스 : 당신의 도움에 대한

import java.applet.*; 
import java.awt.*; 

public class Breakout extends Applet implements Runnable{ 
    Thread thread = new Thread(this); 
    boolean running = true; 
    //Brick b; 
    Brick2 b2; 
    Paddle p; 
    Ball ba; 
    Image dbImage; 
    Graphics dbg; 
    public void init(){ 
     setSize(800,600); 
     //b = new Brick(this); 
     b2 = new Brick2(0,0); 
     p = new Paddle(this); 
     ba = new Ball(this); 

    } 
    public void start(){ 
     thread.start(); 
    } 
    public void destroy(){ 
     running = false; 
    } 
    public void stop(){ 
     running = false; 
    } 
    public void run(){ 
     while(running){ 
      //b.update(this,ba); 

      b2.update(ba); 
      p.update(this); 
      ba.update(this,p); 
      repaint(); 
      try{ 
       thread.sleep(20); 
      } 
      catch (InterruptedException e){ 
       System.out.println("AN ERROR HAS OCCURED"); 
      } 
     } 
    } 
    public void update(Graphics g, Brick2 b){ 
     dbImage = createImage(getWidth(),getHeight()); 
     dbg = dbImage.getGraphics(); 
     paint(dbg); 
     g.drawImage(dbImage,0,0,this); 

       } 
    public void paint(Graphics g){ 
     g.fillRect(0,0,800,600); 
     //b.paint(g,this); 
     b2.paint(g); 
     p.paint(g,this); 
     ba.paint(g,this); 

    } 
} 

감사합니다 .

답변

1

Brick Classboolean isDestroyed = false을 추가 할 수 있습니다. Ball이 (당신의 collision() 방법)을 Brick을 터치하면 brick = null 그래서 나중에 메모리를 해제 있는지 확인하는 것을 잊지 마세요 :) 그 Brick와 상호 작용/그리기 isDestroyed = true 및 중지를 설정 :

가장 좋은 방법은해야 할 일 수익을 b2.isDestroyed 있는지 확인, b2.paint()를 호출하기 전에, 자신의 메인 클래스에서,

public class Brick2 { 
    private boolean isDestroyed = false; 

    public boolean isDestroyed() { 
     return isDestroyed; 
    } 

    public void setIsDestroyed(boolean newValue) { 
     isDestroyed = newValue; 
    } 
} 

그 다음이 개인 변수로 isDestroyed 부울을 만들고 isDestroyed 방법으로 그 가치를 얻을 수있을 것입니다 true :

public class Breakout...{ 
    public void paint(...) { 
     if(b2 != null && !b2.isDestroyed()) 
      b2.paint(g); 
     else 
      b2 = null; 
} 

하지만 내가 너라면 ArrayListBricks으로 만들고 ArrayList를 통해 추가/제거 할 것입니다. 그런 식으로 관리하기가 더 쉬워 질 것입니다. 할아, 다른 것이 필요한 경우.

+0

어떻게 그 벽돌 그리기를 중단하나요? – John

+0

대답을 편집하겠습니다 –

+0

특정 벽돌과의 상호 작용을 중단하고 brick = null을 만드는 방법은 무엇입니까? 나는 어떤 배열도 갖고 있지 않다. – John

0

벽돌의 배열을 처리하면 다른 개체 모음과 동일한 방식으로 처리됩니다. List<brick> = new ArrayList<brick>();

벽돌에는 4 개의 좌표와 모양이 있어야하며,이 경우에는 int brickLife = 1이 공이 깨지면 0으로 변경됩니다. 튼튼한 벽돌을 추가하려면 값을 높이는 것이 좋습니다.