乒乓球中的Java球非常奇怪地弹跳

时间:2013-04-17 17:26:15

标签: java class object user-interface pong

当球到达左侧球拍正上方的某个位置时,它会在球拍内部产生弹跳并反弹并继续下降。它必须与下面的功能有关,但我无法弄明白。

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;
                }


}

1 个答案:

答案 0 :(得分: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是你的右桨。

相关问题