2016-06-07 80 views
-2

我試圖做一個乒乓球比賽,但我的球不動,所以如何讓球移動?如何讓代碼移動球?

這是我的代碼

package test; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class pingpong1 extends JFrame implements Runnable { 

public static void main(String[] args) { 
     pingpong1 ping = new pingpong1("PingPong Hard Game"); 

     new Thread(ping).start(); 

     ping.setSize(600, 300); 

     ping.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     ping.setVisible(true); 

    } private int width, height; 

    private User user;   
    private Computer computer;   
    private Ball ball; 
    static int UserScore = 0; 
    int ComputerScore=0; 

    public pingpong1(String title){ 

    } 
    @Override 
    public void paint(Graphics g){ 
     super.paint(g); 
     Image img2; 
     ImageIcon img = new ImageIcon("pingpong.png"); 
     img2 = img.getImage(); 
     g.drawImage(img2,0,0, this); 
     ball.paint(g); 
    } 
    @Override 
    public void run(){ 
     while(true){ 
      ball.moveBall(); 
      repaint(); 

       try { 
        Thread.sleep(20); 
       } catch (InterruptedException e) { 
        Logger.getLogger(getName()).log(Level.SEVERE,null,e); 
       } 
    } 
    } 

    public void paintComponent(Graphics g) { 
      if (user == null) { 
       width = getWidth(); 
       height = getHeight(); 
       user = new User(); 
       computer = new Computer(); 
       ball = new Ball(); 
      } 
      ball.draw(g); 

     } 
    public class User{ 

    } 
    public class Computer{ 

    } 
    public class Ball{ 
     private int x,y; 
     private int centerX , centerY; 
     private Color color; 
     boolean go; 
     Ball(){ 
      go=false; 
     } 
     public void paint(Graphics g) { 
      // TODO Auto-generated method stub 

     } 
     public Ball(int x,int y,Color color){ 
      this.x=x; 
      this.y=y; 
      this.color=color; 
      this.centerX=5; 
      this.centerY=5; 
     } 
     public int getX(){ 
      return x; 
     } 
     public int getY(){ 
      return y; 
     } 
     public void moveBall(){ 
      centerX=5; 
      x+=centerX; 
      y+=centerY; 

     } void draw(Graphics g){ 
      Image img2; 
      ImageIcon img = new ImageIcon("pingpong.png"); 
      img2 = img.getImage(); 
      g.drawImage(img2, centerX - -35, centerY -10 , null); 
     } 
    } 
    } 
+0

爲什麼_android_標籤? –

+0

你預計什麼時候在不同的地方抽球?我會猜畫球類的畫。但是,當我期望電話 – Gildraths

回答

1

首先你的移動它的代碼,需要有某種形式的輸入的實際移動球。現在它不會做任何事情,但添加x,y並不重繪它,所以你基本上告訴它什麼都不做。 如果你正在尋找用戶控制這樣的事情會工作嗎?

public void moveIt(KeyEvent evt) { 
switch (evt.getKeyCode()) { 
     case KeyEvent.VK_DOWN: 
      myY += 5; 
      break; 
     case KeyEvent.VK_UP: 
      myY -= 5; 
      break; 
     case KeyEvent.VK_LEFT: 
      myX -= 5; 
      break; 
     case KeyEvent.VK_RIGHT: 
      myX += 5; 
      break; 
    } 
} 

如果您正在尋找一種自動移動球的方法,那麼您需要查看代碼中的一些內容。當你不採取考慮速度/方向等...

這是一個感人球一個基本的例子http://introcs.cs.princeton.edu/java/34nbody/Ball.java.html

我會作出評論,但我的代表處是50

+0

我需要自動移動時,您正在調用paint方法(未實現)。 –

+0

如果是這種情況,請檢查基本鏈接,只要它能讓您獲得比您需要移動更多的基本鏈接。 –

+0

感謝您的幫助 –

0

在你的代碼是正確更新球的位置在你的遊戲循環:

類pingpong1

while(true){ 
     ball.moveBall(); // You update movement here 
     repaint(); 
     try { 
      Thread.sleep(20); 
     } catch (InterruptedException e) { 
      System.err.println("Interrupted."); 
     } 
    } 

但你從來沒有真正在更新的位置重繪球:

public void paint(Graphics g){ 
    super.paint(g); 
    Image img2; 
    ImageIcon img = new ImageIcon("pingpong.png"); 
    img2 = img.getImage(); 
    g.drawImage(img2,0,0, this); 
    ball.paint(g); // this method is empty 
} 

因爲ball方法paint是空的:

類球

public void paint(Graphics g) { 
    // TODO Auto-generated method stub 
} 

要更正:

public void paint(Graphics g) { 
    ImageIcon icon = new ImageIcon("ball.png"); 
    Image image = icon.getImage(); 
    g.drawImage(image, x, y, null); 
} 

或者只是打電話ball.draw()代替,但你還是要糾正xy,因爲他們是目前不變,變化:

g.drawImage(img2, centerX + 35, centerY - 10, null); 

要:

g.drawImage(img2, x, y, null);