2011-11-30 32 views
1

我正在創建pong的另一個版本。這個人爲每個人使用3個槳。我花了將近兩個小時的時間試圖弄清楚爲什麼當球撞擊槳時,其他兩個槳沒有檢測到。原始(頂部)槳不會檢測到撞擊並正確更新撞擊計數器。我已嘗試單獨使用if報表,else if報表等,但沒有成功。我創建了三個Y變量來檢測所有三個槳的位置,但仍然沒有運氣。三槳砰擊檢測

有什麼建議嗎?

import java.awt.Point; 


public class box { 
    private int xTopLeft, yTopLeft, width, height; 
    int xBall, yBall; 
    private int radius = 5; 
    int xBallVel, yBallVel; 
    int VELOCITY_MAG =5; 
    public Point ballLoc = new Point(); 

    private int paddleY; 
    private int paddleYP2; 
    private int paddleYP3; 
    private int paddleWidth = 20; 
    private int paddleWidth2 = 20; 
    private int paddleWidth3 = 20; 
    int hitCount = 0; 
    int missCount = 0; 

    private boolean updating=true; 

    public int getBallRadius() 
    { 
     return radius; 
    } 
    public Point getBallLoc() 
    { 
     ballLoc.x = xBall; 
     ballLoc.y = yBall; 
     return ballLoc; 
    } 
    public box(int x, int y, int w, int h) 
    { 
     xTopLeft =x; 
     yTopLeft = y; 
     width =w; 
     height = h; 
     createRandomBallLocation(); 
    } 
    public void updatePaddle(int y) 
    { 

     paddleY = y; 
    } 
    public void updatePaddleP2(int yp2) 
    { 

     paddleYP2 = yp2; 
    } 
    public void updatePaddleP3(int yp3) 
    { 

     paddleYP3 = yp3; 
    } 
    public int getPaddleWidth() 
    { 
     return paddleWidth ; 
    } 
    public int getPaddleWidth2() 
    { 
     return paddleWidth2 ; 
    } 
    public int getPaddleWidth3() 
    { 
     return paddleWidth3 ; 
    } 
    public int getPaddleY() 
    { 
     System.out.println(paddleY); 
     return paddleY; 

    } 
    public int getPaddleP2() 
    { 
     System.out.println(paddleYP2); 
     return paddleYP2; 
    } 
    public int getPaddleP3() 
    { 
     return paddleYP3; 
    } 
    public int getHitCount() 
    { 
     return hitCount; 
    } 
    public int getMissCount() 
    { 
     return missCount; 
    } 
    public int velMag() 
    { 
     return VELOCITY_MAG; 
    } 
     public void update() 
    { 
     if (!updating) return; 

     xBall = xBall + xBallVel; 
     yBall = yBall + yBallVel; 

     if (xBall + radius >= xTopLeft+width && xBallVel>=0) 

      if ((yBall >= paddleY-paddleWidth && yBall <= paddleY + paddleWidth) 
        || (yBall >= paddleYP2-paddleWidth2 && yBall <= paddleYP2 + paddleWidth2) 
        || (yBall >= paddleYP3-paddleWidth3 && yBall <= paddleYP3 + paddleWidth3)) 
      { 
       // hit paddles 

       xBallVel = - xBallVel; 
       hitCount++; 

      } 

     else if (xBall+radius >= xTopLeft + width) 
      { 
       xBallVel = - xBallVel; 

      } 


     if (yBall+radius >= yTopLeft + height && yBallVel >= 0) 
     { 
      yBallVel = - yBallVel; 
     } 
     if (yBall-radius <= yTopLeft && yBallVel <=0) 
     { 
      yBallVel = - yBallVel; 
     } 
     if (xBall-radius <= xTopLeft && xBallVel <= 0) 
     { 
      xBallVel = - xBallVel; 
     } 

    } 
    public void createRandomBallLocation() 
    { 
     xBall = xTopLeft + radius + 
       (int)((width-2*radius)*Math.random()); 

      yBall = yTopLeft + radius + 
       (int)((height-2*radius)*Math.random()); 

      xBallVel = velMag(); 
      yBallVel = velMag(); 

    } 
} 
import java.awt.*; 

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Point; 

import java.awt.event.MouseEvent; 
import java.awt.image.BufferStrategy; 

public class Pong extends JFrame 
implements Runnable{ 

    box box = null; 
    int top, left, width, height; 
    final int LINE_THICKNESS = 5; 
    Graphics bufferGraphics; 
    int sleep=25; 

    public Pong(String name) 
    { 
     super(name); 
     int windowWidth = 1024; 
     int windowHeight =768; 

     this.setSize(windowWidth, windowHeight); 
     this.setResizable(false); 
     this.setLocation(400, 150); 
     this.setVisible(true); 

     this.createBufferStrategy(4); 
     MyMouseClick mmc = new MyMouseClick(); 
     addMouseListener(mmc); 
     MyMouseMotion mmm = new MyMouseMotion(); 
     addMouseMotionListener(mmm); 


     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Thread thread = new Thread(this); 
     thread.start(); 
    } 
    public void run() 
    { 
     while(true) 
     { 
      try 
      { 
       if (box != null) 
       { 
        box.update(); 
        repaint(); 

       } 
       Thread.sleep(sleep); 
      } 
      catch (InterruptedException e) 
      {} 
     } 

    } 

    public void paint(Graphics g) 
    { 
     BufferStrategy bf = this.getBufferStrategy(); 
     g = bf.getDrawGraphics(); 
     Dimension d = getSize(); 
     if (box ==null) 
     { 
      Insets insets = getInsets(); 
      left = insets.left; 
      top = insets.top; 
      width = d.width-left - insets.right; 
      height = d.height - top - insets.bottom; 
      box = new box(left, top, width, height); 

     } 

     g.fillRect(0,0, d.width, d.height); 
     g.setColor(Color.BLUE); 
     g.setFont(new Font("Arial", 20, 30)); 
     g.drawString("Hits: "+box.getHitCount(), 20, 60); 
     g.setColor(Color.red); 
     g.setFont(new Font("Arial", 20, 30)); 
     g.drawString("Misses: "+box.getMissCount(), 20, 380); 


     g.fillRect(left, top, left+width, LINE_THICKNESS); // top of box 
     g.setColor(Color.white); 
     g.fillRect(left, top+height, left+width, LINE_THICKNESS); // bottom of box 
     g.drawLine(left, top, left, top+height); // left of box 
     g.drawLine(left+width, top, left+width, top+height); // right side 

     Point ballLoc = box.getBallLoc(); 
     int radius = box.getBallRadius(); 


     g.fillOval(ballLoc.x - radius, ballLoc.y-radius, 2*radius, 2*radius); 

     // Draw paddles Player 1 
     g.setColor(Color.yellow); 
     int yp = box.getPaddleY(); 
     int yp2 = box.getPaddleP2(); 
     int yp3 = box.getPaddleP3(); 
     int yw = box.getPaddleWidth(); 
     int yw2 = box.getPaddleWidth2(); 
     int yw3 = box.getPaddleWidth3(); 
     g.fillRect(left+width-5, yp-yw, 4, 50); 
     g.fillRect(left+width-5, (yp2-yw2)+280, 4, 50); 
     g.fillRect(left+width-5, (yp3-yw3)+140, 4, 50); 
     bf.show(); 
    }  

    // *********************** Inner classes 
    class MyMouseClick extends MouseAdapter 
    { 
     public void mouseClicked(MouseEvent e) 
     { 
      box = null; 
      repaint(); 
     } 
    } 
    class MyMouseMotion extends MouseMotionAdapter 
    { 
     public void mouseMoved(MouseEvent e) 
     { 
      int y = e.getY(); 
      int y2 = e.getY(); 
      int y3 = e.getY(); 
      if (box != null) 
      { 
       box.updatePaddle(y); 
       box.updatePaddleP2(y2); 
       box.updatePaddleP3(y3); 
       repaint(); 
      } 

     } 


    } 

    // ************************************ 


    public static void main(String[] args) { 
     Pong t = new Pong("Three Paddle Pong"); 
     t.setVisible(true); 
    } // end of main 


} 

Screenshot

+0

update()方法的格式非常難以閱讀。你能描述一下這些槳應該如何定向嗎?他們是三個垂直排列槳排列在不同的x值嗎?也許一個截圖也會有幫助 – I82Much

+0

@ I82Much鏡頭加入 – user973858

回答

1

paint方法你在不同的y位置畫你的3撥片,但其用於碰撞檢測的實際槳位置,paddleYP?,您只需設置3個槳的相同ymouseMoved

+0

謝謝。我剛剛注意到,無論我第一次放入if語句中的哪個槳,它總是隻檢測到第一個撞擊。我會嘗試添加兩個其他的y然後在鼠標移動。謝謝! – user973858

+0

我添加了兩個更多的變量鼠標移動,但仍然只檢測頂部槳。也許我沒有正確添加變量? – user973858

+0

是的,你沒有正確添加它們:你有3個變量,但它們具有相同的值。你的槳應該有不同的y,因爲它們沒有相同的y。 – toto2