2013-04-17 63 views
0

当球到达左侧桨正上方的某个点时,它几乎在桨内部发出嘶嘶声和反弹,并继续向下。它必须与下面的功能有关,但我无法弄清楚。爪哇球在弹跳非常奇怪

public static boolean intervallContains(int low,int high, int n) { //determines if something is in a certain range 
      return n >= low && n <= high; 
     } 
     public void detectPaddle(){ //determines if ball is close enough to paddle for detection 
     int withinY = (paddleStart+y) - (ballStartY+randomBally); 
     int withinY2 = (paddleStartTwo+ytwo) - (ballStartY+randomBally); 
     //System.out.println(withinY +" paddle - ball"); 
     // System.out.println(ballStartY+randomBally +" ball"); 

     if (ballStartX+randomBallx <= paddleFace1 && intervallContains(-50,50,withinY)){ 
     dx = -dx; 
     } 
     if(ballStartX+randomBallx >= paddleFace2 && intervallContains(-50,50,withinY2)){ 
     dx = -dx; 

     } 
     } 

public void points(){ 
      if(ballStartX+randomBallx >= jpW-30){ 
        score1++; 
        randomBallx = 30; 
        randomBally = 30; 
      dx = -dx;   
      } 
      else if(ballStartX+randomBallx <= 0){ 
        score2++; 
        randomBallx = 0; 
        randomBally = 0; 
       dx = -dx; 
      } 
      if(score1 == Pong.points){ 

        //JOptionPane.showMessageDialog(null, "Player one wins? maybe?..."); 
        //choose to continue or close to reset game 
        //System.exit(0); 
      } 
     } 

下面是完整的代码

import java.awt.Color; 
import java.awt.Event; 
import java.awt.Graphics; 
import java.util.Random; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 


public class Pong extends JFrame implements ActionListener{ 

     //implement constants 

     PongPanel pongPanel = new PongPanel(); 

     //JFrame pong x and y coordinates 
     static final int jfpX = 150; 
     static final int jfpY = 20; 

     // JFrame pong width and height 
     static final int jfpW = 800; 
     static final int jfpH = 600; 
     String pointStr; 
     static int points; 


     Thread thrd; 

     public static void main(String[] args) { 
       Pong jfp = new Pong(); 
       jfp.setVisible(true); 

     } 

     public Pong(){ 
       setBounds(jfpX,jfpY,jfpW,jfpH); 
       setTitle("Pong"); 
       setResizable(false); 
       setDefaultCloseOperation(EXIT_ON_CLOSE); 
       setBackground(Color.black); 


       pointStr = JOptionPane.showInputDialog("Enter how many points you want to play to. The limit is 25, And must be at least 5."); 

       try{ 

       points = Integer.parseInt(pointStr); 

       } 

       catch(NumberFormatException e){ 
         pointStr = JOptionPane.showInputDialog("Please enter digits only."); 
       } 




       add(pongPanel); 
       addKeyListener(pongPanel); 

       thrd = new Thread (pongPanel); 
       thrd.start(); 
       } 


     public void actionPerformed(ActionEvent e) { 

     } 

} 

class PongPanel extends JPanel implements Runnable, KeyListener{ 
     Random random = new Random(); 
     static final int jpW = 800; 
     static final int jpH = 600; 
     int paddleStart = (jpH/2)-60; 
     int paddleStartTwo = (jpH/2)-60; 
     int ballStartX = (jpW/2)-30; 
     int ballStartY = (jpH/2)-30; 
     int ytwo,x,y; 
     int paddleFace1 = 40; 
     int paddleFace2 = 730; 
     int ballD = 30; 
     int paddleW1 = 20; 
     int paddleH1 = 100; 
     int paddleW2 = 20; 
     int paddleH2 = 100; 
     boolean play = true; 
     boolean leftWall1 = true; 
     boolean leftWall2 = true; 
     boolean rightWall1 = true; 
     boolean rightWall2 = true; 
     int min = -2; 
     int max = 2; 
     int score1, score2;  
     int randomBallx = random.nextInt(max-min+1)+min; 
     int randomBally = random.nextInt(max-min+1)+min; 



     int dx = 7; 
     int dy = 7; //direction of y 

     public void ballNotZero(){// makes sure the ball doesnt go straight up and down 
     if (randomBallx == 0){ 
       randomBallx = random.nextInt(max-min)+min; 
      } 
      if(randomBally == 0){ 
       randomBally=random.nextInt(max-min)+min; 
      } 


     } 


     public PongPanel(){ 





     } 

     protected void paintComponent(Graphics g) 
     { 
     super.paintComponent(g); 

     Color ball,score,paddleOne,paddleTwo; 
     score = new Color(0,255,0); 
     ball = new Color(255,0,255); 
     paddleOne = new Color(255,0,0); 
     paddleTwo = new Color(0,0,255); 


     g.setColor(ball); 
     g.fillOval(ballStartX+randomBallx,ballStartY+randomBally,ballD,ballD); 

    // g.setFont(font); 
     g.setColor(score); 
     g.drawString(Integer.toString(score1),200 ,100); 
     g.drawString(Integer.toString(score2),600,100); 

     g.setColor(paddleOne); 
     g.fillRect(20,paddleStart+y,paddleW1,paddleH1); 

     g.setColor(paddleTwo); 
     g.fillRect(760,paddleStartTwo+ytwo,paddleW2,paddleH2); 

     g.setColor(Color.white); 
     g.drawLine(400,0,400,600); 

     } 
     public void run() { 
       while(play){ 
       paddleWalls(); 
       detectPaddle(); 
       ballBounce(); 
       points(); 
       moveBall(); 
       ballNotZero(); 
       repaint(); 
     try {Thread.sleep(75); } catch(Exception e){ 

     }; 

       } 
     } 
     public static boolean intervallContains(int low,int high, int n) { //determines if something is in a certain range 
      return n >= low && n <= high; 
     } 
     public void detectPaddle(){ //determines if ball is close enough to paddle for detection 
     int withinY = (paddleStart+y) - (ballStartY+randomBally); 
     int withinY2 = (paddleStartTwo+ytwo) - (ballStartY+randomBally); 
     //System.out.println(withinY +" paddle - ball"); 
     // System.out.println(ballStartY+randomBally +" ball"); 

     if (ballStartX+randomBallx <= paddleFace1 && intervallContains(-50,50,withinY)){ 
     dx = -dx; 
     } 
     if(ballStartX+randomBallx >= paddleFace2 && intervallContains(-50,50,withinY2)){ 
     dx = -dx; 

     } 
     } 

     public void moveBall(){ 

       randomBallx+=dx; 
       randomBally+=dy; 
     } 
     public void ballBounce(){ 
     if(ballStartY+randomBally >= jpH-50){ 
     dy = -dy; 

     } 
     else if(ballStartY+randomBally <= 0){ 
     dy = -dy; 
     } 
     } 

     public void paddleWalls(){ 
     if((paddleStart+y) == 0){ 
       leftWall1 = false; 
     } 
     else{ 
       leftWall1 = true; 
       } 
     if((paddleStart+y) == 480){ 
       rightWall1 = false; 
     } 
     else{ 
       rightWall1 = true; 
       } 
     if((paddleStartTwo+ytwo) == 0){ 
       leftWall2 = false; 
     } 
     else{ 
       leftWall2 = true; 
       } 
     if((paddleStartTwo+ytwo) == 480){ 
       rightWall2 = false; 
     } 
     else{ 
       rightWall2 = true; 
       } 

     } 

     public void points(){ 
      if(ballStartX+randomBallx >= jpW-30){ 
        score1++; 
        randomBallx = 30; 
        randomBally = 30; 
      dx = -dx;   
      } 
      else if(ballStartX+randomBallx <= 0){ 
        score2++; 
        randomBallx = 0; 
        randomBally = 0; 
       dx = -dx; 
      } 
      if(score1 == Pong.points){ 

        //JOptionPane.showMessageDialog(null, "Player one wins? maybe?..."); 
        //choose to continue or close to reset game 
        //System.exit(0); 
      } 
     } 


     public void keyPressed(KeyEvent e) { 

     //player one controls 
     if(e.getKeyCode() == KeyEvent.VK_A && leftWall1 == true){ 
       y-=10; 
     } 
     else if(e.getKeyCode() == KeyEvent.VK_S && rightWall1 == true){ 
         y+=10; 
       } 

     //player two controls 
     if(e.getKeyCode() == KeyEvent.VK_QUOTE && leftWall2 == true){ 
       ytwo-=10; 
     } 
     else if(e.getKeyCode() == KeyEvent.VK_SEMICOLON && rightWall2 == true){ 
         ytwo+=10; 
       } 
     if(e.getKeyCode() == KeyEvent.VK_ESCAPE){ 
     System.exit(0); 
     } 


     } 



     public void keyTyped(KeyEvent e) { 

     } 


     public void keyReleased(KeyEvent e) { 

     } 
     public void startPong() { 
         play = true; 
       } 

       public void stopPong() { 
         play = false; 
       } 


} 
+2

用调试程序遍历代码。 –

回答

2

没有能够亲眼看到这个问题,我猜你的问题是在你的detectPaddle()方法。其中,你会看到球是否足够接近你的桨。但是,您不检查它是否朝着正确的方向移动。

更改这些行:

if (ballStartX+randomBallx <= paddleFace1 && intervallContains(-50,50,withinY)){ 
    dx = -dx; 
} 
if(ballStartX+randomBallx >= paddleFace2 && intervallContains(-50,50,withinY2)){ 
    dx = -dx; 
} 

以包括球方向的支票。也许是这样的:

if (dx < 0 && ballStartX+randomBallx <= paddleFace1 && intervallContains(-50,50,withinY)){ 
    dx = -dx; 
} 
if(dx > 0 && ballStartX+randomBallx >= paddleFace2 && intervallContains(-50,50,withinY2)){ 
    dx = -dx; 
} 

这样的球会不会反弹,桨内 - 它只能在一个方向反弹。

此外,我假设paddleFace1是您的左桨,paddleFace2是您的右桨。

+1

这样可以防止出杆,但这也意味着球不能从桨的“背部”反弹。如果你想支持这一点,当球弹跳时(而不是上面的检查)设置一个标志,并在球离开桨时松开该标志。标志设置后不要反弹。 –