2015-06-01 19 views
1

我正在嘗試創建一個乒乓球遊戲,到目前爲止,除了球之外,我還有一切爲我工作。起初,我把球的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) { 
    } 





} 
+0

Wh在你的問題是否具體?也許有''balltouch'代碼不工作的錯誤? –

+1

我沒有時間閱讀所有的代碼,但是當這樣的事情發生在我身上時,這是因爲增加的速度導致球每次跳動移動太多以至於球從未實際接觸到槳,所以它從未交互。確保障礙物實際上與槳板相互作用。 – Carcigenicate

+0

所以,如果我添加一個搖擺計時器,問題會解決嗎?如果有的話,誰能告訴我怎麼做? – yj2000

回答

0

您正在使用if (x == 75)來檢測與左側槳葉的碰撞。但是如果速度是2,那麼球的速度是2,但是總是平穩的,永遠不會等於75.爲了快速解決問題,請對照範圍檢查x:if (x > 60 && x < 75)

1

由於每次移動2次,它可能會跳過某些座標。因此,當你說

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

Ÿ可以從走的getHeight() - 邊界+ 1的getHeight() - 邊界 - 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.