当增加速度时,球通过桨

时间:2015-06-01 21:23:54

标签: java

我正在尝试创建一个Pong游戏,到目前为止,除了球之外,我还有一切为我工作。起初我让球的x和y轴每次向上移动一个空格。它工作正常,直到我决定将它增加到2.我无法弄清楚我的代码有什么问题,我需要帮助。

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Pong extends JPanel implements KeyListener{

int x = 90;
int y = 90;
int rectytop = 30;
int rectytop2 = 30;
int rectybottom = rectytop + 100; 
int rectybottom2 = rectytop2 + 100;

int border = 30;
boolean balldown = true;
boolean ballright = true;
boolean playerborderup = false;
boolean playerborderdown = false;
boolean balltouch1 = false;
boolean balltouch2 = false;


private void moveball() {

    if (balldown == true){
        y = y + 2;
    }

    if (y == getHeight()-border){
        balldown = false;
    }
   if (balldown == false){
        y = y - 2;
    }






   if (ballright == true){
       x = x + 2;
   }
   if (x == getWidth()-border){
       ballright = false;
   }
   if (ballright == false){
       x = x - 2;
   }



   if (y == 0){
       balldown = true;
   }

   if (x == 0){
       ballright = true;
   }



   if (balltouch1 == false){
       if (x == 75){
           if(y < rectybottom && y > rectytop){
               ballright = true;
           }
       }
   }


   if (balltouch2 == false){
       if (x == 390 && y < rectybottom2 && y > rectytop2){
               ballright = false;
       }
   }


}    

@Override
public void paint(Graphics g){
    super.paint(g);

    //drawing ball and paddles 
    g.fillOval(x, y, 30, 30);
    g.fillRect(45 , rectytop, 30, 100);
    g.fillRect(425, rectytop2, 30, 100);

}


public static void main(String[] args) throws InterruptedException {

    //making the window
    JFrame f = new JFrame("Pong Game");
    Pong game = new Pong();
    f.setVisible(true);
    f.setSize(500, 500);//1024, 724
    f.setResizable(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.addKeyListener(game);

    //game code

    f.add(game);        
    while (true){
        game.repaint();
        game.moveball();

        Thread.sleep(10);


    }
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

    //player one
    if (e.getKeyCode() == KeyEvent.VK_W){
        if (rectytop == 0){
            playerborderup = true;
        }
        if (rectytop != 0){
            playerborderup = false;
        }
        if (playerborderup == true){
            rectytop = rectytop + 0;
            rectybottom = rectytop + 100;
            repaint();
        }
        if (playerborderup == false){
            rectytop = rectytop - 5;
            rectybottom = rectytop + 100;
            repaint();
        }

    }

    if (e.getKeyCode() == KeyEvent.VK_S){
        if (rectytop == 585){
            playerborderdown = true;
        }
        if (rectytop != 585){
            playerborderdown = false;
        }
        if (playerborderdown == true){
            rectytop = rectytop - 0;
            rectybottom = rectytop + 100;
            repaint();
        }
        if (playerborderdown == false){
            rectytop = rectytop + 5;
            rectybottom = rectytop + 100;
            repaint();
        }
    }


    //player two
    if (e.getKeyCode() == KeyEvent.VK_UP){
        if (rectytop2 == 0){
            playerborderup = true;
        }
        if (rectytop2 != 0){
            playerborderup = false;
        }
        if (playerborderup == true){
            rectytop2 = rectytop2 + 0;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
        if (playerborderup == false){
            rectytop2 = rectytop2 - 5;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }

    }

    if (e.getKeyCode() == KeyEvent.VK_DOWN){
        if (rectytop2 == 585){
            playerborderdown = true;
        }
        if (rectytop2 != 585){
            playerborderdown = false;
        }
        if (playerborderdown == true){
            rectytop2 = rectytop2 - 0;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
        if (playerborderdown == false){
            rectytop2 = rectytop2 + 5;
            rectybottom2 = rectytop2 + 100;
            repaint();
        }
    }







}

@Override
public void keyReleased(KeyEvent e) {
    }





}

2 个答案:

答案 0 :(得分:1)

由于每次移动2次,因此可能会“跳过”某些坐标。因此当你说

if (y == getHeight()-border){
    balldown = false;
}

y可能从getHeight() - border + 1到getHeight() - border - 1并且从不满足这个条件。因此,将其更改为范围或小于。您的新代码应为

if (y <= getHeight()-border +1){
    balldown = false;     //change the +1 and -1 to have difference at least speed number
}

请注意,对于带有==,更改

的其他ifs,您必须执行相同的操作
if (x == getWidth()-border){
    ballright = false;
}

if (y == 0){
    balldown = true;
}

if (x == 0){
    ballright = true;
}

if (x <= getWidth()-border +1){
    ballright = false;
}

if (y <= 1){
    balldown = true;
}

if (x <= 1){
    ballright = true;
}

请注意,您也可以通过说明位置是否恰好距离边界一个,暂时将位置减1而不是2来解决问题。

答案 1 :(得分:0)

您正在使用if (x == 75)来检测与左侧拨片的碰撞。但如果速度为2,则球x总是均匀且永远不等于75.要快速修复,请在范围内检查x:if (x > 60 && x < 75)