簡體   English   中英

我該如何做才能使此框中的圓真正自動移動?

[英]How do I make it so the circle in this frame actually moves automatically?

我無法解決這個問題。 我用所需的輸出創建了該框架和面板。 唯一的事情是我無法弄清楚如何使球“自動”移動。 我理想的游戲可能是在游戲開始時就放下了球/圓,可能是單擊了按鈕或類似的按鈕。 我將如何去做呢?

我嘗試用E鍵移動球,但是這對於用戶來說太不方便了,因此我認為沒有事件處理程序的情況下移動球是更好的選擇。

    private int ballX, ballY, ballR;
private int score1, score2;
private JPanel panel;
private JFrame frame;
private DrawingArea canvas;
private int xpos, ypos,width,height;
private int xpos2, ypos2, width2, height2;
public Pong(){
    xpos = 300;
    ypos = 550;
    width = 100;
    height = 50;

    xpos2 = 300;
    ypos2 = 100;
    width2 = 100;
    height2 = 50;

    ballR = 50;
    ballX = 325; 
    ballY = 330;


}
public static void main(String[]args){
    Pong p = new Pong();
    p.run();
}
public void run(){
    frame = new JFrame("Pong");
    frame.setSize(700,700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas = new DrawingArea();     // create a panel to draw on
    canvas.setBackground(Color.WHITE);
    canvas.addFocusListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);


    frame.getContentPane().add(canvas);
    frame.setVisible(true);
}
class DrawingArea extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor ( Color.blue );
        g.fillRect ( xpos, ypos, width, height );
        g.setColor(Color.red);
        g.fillRect(xpos2, ypos2, width2, height2);


        g.setColor(Color.black);
        g.fillRect(0,0,700,50);
        g.fillRect(0,630,700,50);

        g.fillOval(ballX,ballY,ballR, ballR);

    }
}
public void keyPressed ( KeyEvent e )    {
    int value = e.getKeyCode();

    switch ( value )    {
        case KeyEvent.VK_RIGHT:     xpos += 50; break;
        case KeyEvent.VK_LEFT:      xpos -= 50; break;
        case KeyEvent.VK_A:         xpos2 -= 50; break;
        case KeyEvent.VK_D:         xpos2 += 50; break;
        /*try to drop the ball with the space button
         * case KeyEvent.VK_SPACE:
            ballX+=25;
            ballY+=25;
            break;
        case KeyEvent.VK_ENTER:
            xpos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
            ypos = (int)( Math.random ( ) * ( 500 - (2 * radius) ) );
            break; 
        */
    }
    if( (xpos < 0 || xpos >= 700) || (xpos2 < 0 || xpos2 >= 700)){
         if(xpos < 0 || xpos2 < 0){
            if(xpos < 0) xpos = 0;
            else if(xpos2 < 0) xpos2 = 0;
            return;
        }
        else if(xpos >= 700 || xpos2 >= 700){
            if(xpos >= 700)xpos = 550;
            else if(xpos2 >= 700) xpos2=550;
            return;
        }
    }

    canvas.repaint ( );
}

}

我希望輸出會隨着游戲開始/按下按鈕而使球下降到藍色矩形上,但是我無法使其正常工作。

當您要自動移動球時,請在run()方法中使用一些增量值(如您在keyPressed()接口方法中所做的那樣)更改dx,dy等,以改變球的位置(您的xpos,ypos,xpos1和ypos2)。並調用JFrame的repaint()方法。

public class BouncingBall extends JPanel {

  // Box height and width
  int width;
  int height;

  // Ball Size
  float radius = 40; 
  float diameter = radius * 2;

  // Center of Call
  float X = radius + 50;
  float Y = radius + 20;

  // Direction
  float dx = 3;
  float dy = 3;

  public BouncingBall() {

    Thread thread = new Thread() {
      public void run() {
        while (true) {

          width = getWidth();
          height = getHeight();

          X = X + dx ;
          Y = Y + dy;

          if (X - radius < 0) {
            dx = -dx; 
            X = radius; 
          } else if (X + radius > width) {
            dx = -dx;
            X = width - radius;
          }

          if (Y - radius < 0) {
            dy = -dy;
            Y = radius;
          } else if (Y + radius > height) {
            dy = -dy;
            Y = height - radius;
          }
          repaint();

          try {
            Thread.sleep(50);
          } catch (InterruptedException ex) {
          }

        }
      }
    };
    thread.start();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
    g.fillOval((int)(X-radius), (int)(Y-radius), (int)diameter, (int)diameter);
  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Bouncing Ball");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setContentPane(new BouncingBall());
    frame.setVisible(true);
  }
}

由於我在Thread.sleep()中使用了50毫秒的渲染時間,因此您可以指定自己的時間。 因此,對於我來說,每50毫秒您的JFrame都會更新一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM