我不能让球反弹我的球棒

时间:2016-09-28 18:29:12

标签: java intersection

这是我的蝙蝠和球X和Y -

    private int moveBallX = 1;
    private int moveBallY = -1;
    private int ballX = 320;
    private int ballY = 220;
    private double batX = 240;
    private int batY = 450;

以下是绘制球的方法 -

矩形球=新的矩形(ballX,ballY,15,15);

    public void drawBall(Graphics g){

    g.setColor(Color.BLACK);
    g.fillOval(Ball.x , Ball.y, 15, 15);

}

这是我的碰撞代码,如果碰到蝙蝠内部的移动方法,它将不会击退球。 (Fwi I'我从这个方法中删除了Ball和Bat移动代码,因为我只是遇到了这个部分的问题)

 public void move(){

    if(Bat.intersects(Ball)){

        moveBallY = -moveBallY;

    }
 }

我已使用此代码将Ball.x和Ball.y分配给moveBallX和moveBallY -

    Ball.x += moveBallX;
    Ball.y += moveBallY;

以下是调用move和draw方法的代码 -

public void run() {
      init();
      while(true){

          thread.currentThread();
            try {
               Thread.sleep(10);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }

        draw();
        move();

    }
}

绘制方法 -

public void draw(){
    buffer = window.canvas.getBufferStrategy();

    if(buffer == null){
        window.canvas.createBufferStrategy(3);
        return;
    }

    g = buffer.getDrawGraphics();
    g.clearRect(0, 0, WIDTH, HEIGHT);

    //draw below

    drawBall(g);
    drawBat(g);
    drawBricks(g);

    //end here

    buffer.show();
    g.dispose();

}

0 个答案:

没有答案