如何解决乒乓球比赛中的得分问题

时间:2015-06-15 21:37:39

标签: java

我正在编写我的第一个Java游戏之一,我需要帮助它的得分部分。每当球接触对方时,每个球员的得分应该增加。一方面它工作得很好,但另一方面它不起作用。另外,在代码中使用尽可能多的if语句是否健康/正常?

P.S。我知道我的代码非常混乱,但这是我第一次创建没有完整教程解释所有内容的东西。

import java.awt.Font;
import java.awt.Graphics;
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.JPanel;
import javax.swing.Timer;

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 scoreplayer1 = 0;
int scoreplayer2 = 0;
int xedge2= 448;
int border = 30;
int scoreplayer1a = (int) scoreplayer1;
boolean balldown = true;
boolean ballright = true;
boolean playerborderup = false;
boolean playerborderdown = false;
boolean balltouch1 = false;
boolean balltouch2 = false;
boolean w = false;
boolean s = false;
boolean up = false;
boolean down = false;
boolean goalon1 = false;
boolean goalon2 = 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 == 76){
           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
    g.fillOval(x, y, 30, 30);
    g.fillRect(48 , rectytop, 30, 100);
    g.fillRect(420, rectytop2, 30, 100);

    g.setFont(new Font("Arial", Font.PLAIN, 100));
    g.drawString(Integer.toString(scoreplayer1a), 175, 100);
    g.drawString(Integer.toString(scoreplayer2), 300, 100);
}

public Timer timer = new Timer(30, new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

    if (w == true){

           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 (s == true){
                        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();
        }
    }

    if (up == true){
        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 (down == true){
        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();
        }
    }

  }

    });

  public void start(){
   timer.start();
   }

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

    //making the window
    JFrame f = new JFrame("Pong Game");
    JPanel p = new JPanel();
    Pong game = new Pong();
    Font font = new Font("Courier", Font.BOLD, 30);

    f.setVisible(true);
    f.setSize(502, 502);//1024, 724
    f.setResizable(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.addKeyListener(game);
    f.add(game);
    f.add(p);
    game.start();


    //game code

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

        game.setscore();
        Thread.sleep(10); 
    }
}

public void setscore(){
    if (x == 0){
        goalon1 = true;
    }

    if (x != 0){
        goalon1 = false;
    }

    if (x == getWidth()-30){
        goalon2 = true;
    }

    if (x != getWidth()-30){
        goalon2 = false;
    }

    if (goalon1 == true){
        scoreplayer2 ++;
    }
    if (goalon2 == true){
        scoreplayer1 = scoreplayer1 + 1;

    }


}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

    //player one
    if (e.getKeyCode() == KeyEvent.VK_W){
       w = true;     
    }

    if (e.getKeyCode() == KeyEvent.VK_S){
       s = true; 
    }


    //player two
    if (e.getKeyCode() == KeyEvent.VK_UP){
        up = true;

    }

    if (e.getKeyCode() == KeyEvent.VK_DOWN){
        down = true;
    }







}

@Override
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W){
        w = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_S){
        s = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_UP){
        up = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN){
        down = false;
    }
}
}

1 个答案:

答案 0 :(得分:0)

你无缘无故地为每个玩家做了很多不同的事情。如果你缩小错误来自哪个代码部分或者至少告诉我们哪个播放器不起作用以及哪个播放器不起作用,那将会更有帮助。

我不打算阅读你发布的所有代码,但要看看这样的差异。该错误可能不仅来自setScore()。没有经过你发布的所有内容,就没有办法告诉你。

if (goalon1 == true){
    scoreplayer2 ++;
}
if (goalon2 == true){
    scoreplayer1 = scoreplayer1 + 1;

}

尝试匹配两个玩家的代码,我相信你会发现你的错误