我怎样才能跟踪这个高分

时间:2015-05-07 18:52:24

标签: java swing pong

我正在进行一场乒乓球比赛,我的最后一步是跟踪高分,但我不知道如何。有人请帮帮我。我不知道从哪里开始。如果我的高分计数器工作,我的乒乓球游戏将是完美的!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JLabel;
public class Pong2 extends JFrame implements Runnable, MouseMotionListener{

  int ball_x, ball_y, ball_dx, ball_dy, paddle_y, ball_r;

  String r;
  int x_left, x_right, y_top, y_bottom;

  /**
   * Constructor
   */
  public Pong2(){
    init();

  }

  /**
   * this is where we set up the UI
   */
  protected void init(){
    this.setSize(400,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

    ball_x = this.getWidth()/2;
    ball_y = this.getHeight()/2;

    ball_dx = ball_dy = 2;

    ball_r = 30;
    this.addMouseMotionListener(this);

    this.setVisible(true);
    getFocus(this);
    x_left = this.getInsets().left;
    x_right = this.getWidth() - this.getInsets().right - ball_r;
    y_top = this.getHeight() - this.getInsets().top + ball_r/3;
    y_bottom = this.getInsets().bottom + ball_r;

  }

  /**
   * helper method which we use to get the focus
   */
  public void getFocus(final JFrame frame)
  {
      EventQueue.invokeLater(new Runnable() {
              public void run() {
                  frame.requestFocus();
              }
          });
  }

  /**
   * implementation of the Runnable interface to be able to move the ball, etc.
   */
  public void run(){

    while(true){

      ball_x += ball_dx;
      if(ball_x <= x_left || ball_x >= x_right){
        ball_dx *=-1;
        ball_x += (2*ball_dx);
      }

      ball_y += ball_dy;
      if(ball_y <= y_bottom || ball_y >= y_top){
        ball_dy *=-1;
        ball_y += (2*ball_dy);
      }
         ball_x += ball_dx; ball_y += ball_dy;
       if (ball_x == 300 &&  ball_y > paddle_y && ball_y < paddle_y+100){
        ball_dx *=-1;
        ball_x += (2*ball_dx);
        ball_dy *=-1;
        ball_y += (2*ball_dy);



       }


      repaint();

      try{
        Thread.sleep(20);
      }catch(InterruptedException ex){
        System.out.println(ex);
      }
    }
  }
  /**
   * all rendering occurs here
   */
  public void paint(Graphics g){

    g.setColor(Color.white);
    g.fillRect(0,0,this.getWidth(),this.getHeight());

    g.setColor(Color.black);
    g.fillOval(ball_x,ball_y, ball_r, ball_r);

    g.setColor(Color.blue); 
    g.fillRect(300, paddle_y, 50, 100);


  }
  public void mouseMoved(MouseEvent e) {
  }
  public void mouseDragged(MouseEvent e) {
    paddle_y = e.getY();
  }
  /**
   * entry point into the program
   */
  public static void main(String[] args){
    // create the class
    Pong2 application = new Pong2();
    new Thread(application).start();

  }

}*

1 个答案:

答案 0 :(得分:1)

将一个整数值添加到变量列表中,并在每次击球时计数。

<ul class="nav nav-tabs" ng-class="'{{newvar}}'" >
<li class='' ng-repeat="tab in tabset.tabs" >
    <a href="" ng-click="tabset.click(tab);resetInput();" ng-class='{"tab-active":tab.active,"tab-inactive":tab.active === false}'> {{tab.heading}}</a>
</li>
</ul>
<ng-transclude>
</ng-transclude>

您必须像这样修改您的运行方法。

添加到您的上一个如果是计数器。

private int score = 0;

在你的paint方法中添加一个带有分数值的sysout。

要重置计数器,请将初始化内容添加到...

if (ball_x == 300 && ball_y > paddle_y && ball_y < paddle_y + 100) {

    score += 10;

    ball_dx *= -1;
    ball_x += (2 * ball_dx);
    ball_dy *= -1;
    ball_y += (2 * ball_dy);

}