2013-02-14 57 views
0

這是一個12年級的面向對象編程項目。需要關於突破遊戲球速度的幫助

我有一個叫Ball的類來構造我的球對象。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 

public class Ball{ 
    private double xPos; 
    private double yPos; 
    private int direction; 
    public Ball(int ixPos, int iyPos, int idirection){ 
     xPos = ixPos; 
     yPos = iyPos; 
     direction = idirection; 
    } 
    public int returnX(){ 
     return (int)xPos; 
    } 
    public int returnY(){ 
     return (int)yPos; 
    } 
    public int returnDirection(){ 
     return direction; 
    } 
    public void move(){ 
     xPos += 1*Math.cos(Math.toRadians(direction)); 
     yPos -= 1*Math.sin(Math.toRadians(direction)); 
    } 
    public void collide(int collideWith){ 
     if(collideWith==1){//collide with left wall 
      if(90<direction && direction<180){ 
       direction = 180-direction; 
      } 
      if(180<direction && direction<270){ 
       direction = 540-direction; 
      } 
     } 
     if(collideWith==2){//collide with right wall 
      if(0<direction && direction<90){ 
       direction = 180-direction; 
      } 
      if(270<direction && direction<360){ 
       direction = 540-direction; 
      } 
     } 
     if(collideWith==3){//collide with up wall 
      if(0<direction && direction<90){ 
       direction = 360-direction; 
      } 
      if(90<direction && direction<180){ 
       direction = 360-direction; 
      } 
     } 
     if(collideWith==4){//collide with down wall 
      direction = 360-direction; 
     } 
    } 
    public void collidePaddle(int collidePos){ 
     if(collidePos!=50 && collidePos!=0){ 
      direction = (50-collidePos)*180/50; 
     } 
    } 
} 

正如您在「移動」功能中看到的那樣,現在球速度非常低。但我需要球更快。如果我將1改爲5,那麼會出現問題。在我的主類中,它檢查球是否撞牆或阻擋塊以改變方向,如果球每次可以移動的像素數量大於1,則球將進入牆或塊。

對我來說,似乎沒有辦法解決這個問題,不知道從哪裏開始思考,有沒有更好的方法來檢查碰撞或什麼?

謝謝。

回答

2

而不是在您的collidePaddle檢查中使用絕對檢查,您應該允許範圍。

例如...

public void collidePaddle(int collidePos){ 
    if (collidePos >= 50) { 
     direction = (50-collidePos)*180/50; 
     // Correct the position of the ball to meet the minimum requirements 
     // of the collision... 
    } else if (collidePos <= 0) { 
     direction = (50-collidePos)*180/50; 
     // Correct the position of the ball to meet the minimum requirements 
     // of the collision... 
    } 
} 

(對不起,我有工作了你的代碼的樂趣;))

這將允許球內的「通行證」超越這些點虛擬情境,但如果你正確的位置componsate,應該沒有什麼區別......當它呈現...

更新

這裏就是我在談論的一個非常簡單的例子...

public class SimpleBouncyBall { 

    public static void main(String[] args) { 
     new SimpleBouncyBall(); 
    } 

    public SimpleBouncyBall() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new CourtPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class CourtPane extends JPanel { 

     private Ball ball; 
     private int speed = 5; 

     public CourtPane() { 
      Timer timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        Rectangle bounds = new Rectangle(new Point(0, 0), getSize()); 
        if (ball == null) { 
         ball = new Ball(bounds); 
        } 
        speed = ball.move(speed, bounds); 
        repaint(); 
       } 
      }); 
      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(100, 100); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (ball != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
       Point p = ball.getPoint(); 
       g2d.translate(p.x, p.y); 
       ball.paint(g2d); 
       g2d.dispose(); 
      } 
     } 

    } 

    public class Ball { 

     private Point p; 
     private int radius = 12; 

     public Ball(Rectangle bounds) {     
      p = new Point(); 
      p.x = 0; 
      p.y = bounds.y + (bounds.height - radius)/2;     
     } 

     public Point getPoint() { 
      return p; 
     } 

     public int move(int speed, Rectangle bounds) {     
      p.x += speed; 
      if (p.x + radius >= (bounds.x + bounds.width)) {      
       speed *= -1; 
       p.x = ((bounds.x + bounds.width) - radius) + speed;      
      } else if (p.x <= bounds.x) { 
       speed *= -1; 
       p.x = bounds.x + speed;      
      } 
      p.y = bounds.y + (bounds.height - radius)/2;     
      return speed;     
     } 

     public void paint(Graphics2D g) { 
      g.setColor(Color.RED); 
      g.fillOval(0, 0, radius, radius); 
     }    
    }   
} 

如果你已經過去的邊界我的招法並不關心,因爲這將重新回球側內坐這些邊界

public int move(int speed, Rectangle bounds) {     
    // Apply the delta 
    p.x += speed; 
    // Have we passed beyond the right side?? 
    if (p.x + radius >= (bounds.x + bounds.width)) {      
     speed *= -1; 
     p.x = ((bounds.x + bounds.width) - radius) + speed;      
    // Have we past beyond the left side?? 
    } else if (p.x <= bounds.x) { 
     speed *= -1; 
     p.x = bounds.x + speed;      
    } 
    p.y = bounds.y + (bounds.height - radius)/2;     
    return speed;     
} 

的速度播放四周,看看你會得到什麼;)

+0

感謝您的回答。 – 2013-02-14 02:17:41

+0

我不知道我是否提出了足夠好的問題,但對我而言,我沒有詢問有關槳的任何事情......你能告訴我你問我的問題....因爲我尋求幫助將球固定在牆上,當速度過高時阻擋,而不是撞擊槳後的方向。 – 2013-02-14 02:19:49

+0

是的,看來你正在檢查一個絕對的位置,而不是允許流浪者。隨着速度的增加,你不能依靠球的邊緣與任何物體的邊緣完全相遇的事實。相反,你需要看看它的任何部分是否已經擊中了世界上任何可能的部分(包括重疊)。 – MadProgrammer 2013-02-14 02:26:03