2013-04-17 4 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

이것은 spaz-out을 막을 것이나, 공이 패들의 "뒤"에서 튀어 나오지 못하게 할 수도 있습니다. 이를 지원하려면 볼이 바운스 될 때 플래그를 설정하고 (위 체크 대신), 볼이 패들과 떨어져있을 때 플래그를 설정 해제하십시오. 국기가 설정되면 튀지 마라. –

관련 문제