2013-04-17 59 views
0

所以我介紹到java和我的技能是有限的,我們的老師希望我們做一個屏幕保護程序。我的目標是讓多個熱氣球同時在屏幕上彈跳,當他們撞到牆上時,他們隨機改變方向。我有一個氣球隨機彈跳,除了它有時假髮,仍然熄滅屏幕,我認爲這個問題是在我的數學,但是。
我需要幫助的問題是,當我向小程序添加第二個圖像時,兩個圖像看上去都是相連的,它們移動的方向完全相同,當一個方向改變方向時,另一個不同的是開始座標,我如何讓他們彼此分開? 繼承人我的代碼。如何讓兩個相同的圖像彼此分開移動?

*** 
import acm.program.*; 
import acm.graphics.*; 
import java.awt.Color; 

public class HotAirBalloons extends GraphicsProgram 
{ 

    private static final int APPLET_WIDTH = 800; 
    private static final int APPLET_HEIGHT = 600; 
    private int speedX = 1; 
    private int speedY = 1; 

    public void init() 
    { 
     setSize(APPLET_WIDTH,APPLET_HEIGHT); 
     setBackground(new Color(100,210,255)); 
    } 

    public void moveRandomDirection() 
    { 
     double direction = Math.random() * 2.0 * Math.PI; 
     double speed = 3.0; 
     speedX = (int) (speed * Math.cos(direction)); 
     speedY = (int) (speed * Math.sin(direction)); 
    } 

    public void run() 
    { 

     GImage img1 = new GImage("balloon.jpg"); 
     add(img1, 0, 0); 
     GImage img2 = new GImage("balloon.jpg"); 
     add(img2, 200, 200); 

     while(true) 
     { 
      pause(15); 
      img1.move(speedX, speedY); 
      img2.move(speedX, speedY); 

      if (img1.getX() > APPLET_WIDTH - 50) 
      { 
       moveRandomDirection(); 
      } 

      if (img1.getX() < 1) 
      { 
       moveRandomDirection(); 
      } 

      if (img1.getY() +85 > APPLET_HEIGHT) 
      { 
       moveRandomDirection(); 
      } 
      if (img1.getY() < 1) 
      { 
       moveRandomDirection(); 
      } 
      if (img2.getX() > APPLET_WIDTH - 50) 
      { 
       moveRandomDirection(); 
      } 

      if (img2.getX() < 1) 
      { 
       moveRandomDirection(); 
      } 

      if (img2.getY() +85 > APPLET_HEIGHT) 
      { 
       moveRandomDirection(); 
      } 
      if (img2.getY() < 1) 
      { 
       moveRandomDirection(); 
      } 
     } 
    } 
} 

回答

1

您對兩個氣球都使用相同的speedX和speedY變量。給他們自己的速度變量。

此外,氣球「有時假髮」的原因是,當你選擇一個隨機的方向時,你沒有指定它不能朝着你試圖讓它們消失的方向從。

爲了解決這個問題,我建議你爲每個氣球創建一個moveRandomDirection方法,一個代表傳入的值代表他們已經擊中了哪條邊。然後限制他們可以移動到其他任何方向的方向。

試試這個:

public void moveBalloonOneInRandomDirection(int whichEdge) 
{ 
    double direction = 0; 
    switch(whichEdge) { 
    case(0): // Top edge 
     direction = Math.random() * Math.PI + ONLY_ALLOW_DOWN_VAL; 
    case(1): // Left edge 
     direction = Math.random() * Math.PI + ONLY_ALLOW_RIGHT_VAL; 
    case(2): // Bottom edge 
     direction = Math.random() * Math.PI + ONLY_ALLOW_UP_VAL; 
    case(3): // Right edge 
     direction = Math.random() * Math.PI + ONLY_ALLOW_LEFT_VAL; 
    } 
    double direction = Math.random() * 2.0 * Math.PI; 
    double speed = 3.0; 
    speedX1 = (int) (speed * Math.cos(direction)); 
    speedY1 = (int) (speed * Math.sin(direction)); 
} 

然後在你的if語句,你就會有這樣的事情:

if (img1.getX() > APPLET_WIDTH - 50) // Right edge 
{ 
    moveBalloonOneInRandomDirection(3); 
} 

你應該也可能使用常量,這樣,而不是「幻數」 ,你將有

private static final int RIGHT_EDGE = 3; 
... 
moveBalloonOneInRandomDirection(RIGHT_EDGE); 
+0

非常感謝您對我的其他問題的幫助!我可能會學習這個東西畢竟=) – James

2

這應該工作:

import acm.program.*; 
import acm.graphics.*; 
import java.awt.Color; 
import java.awt.Point; 

public class HotAirBalloons extends GraphicsProgram 
{ 
    private int speed1 = new Point(1, 1); 
    private int speed2 = new Point(1, 1); 

    public Point moveRandomDirection() 
     { 
      double direction = Math.random() * 2.0 * Math.PI; 
      double speed = 3.0; 
      return new Point((int) (speed * Math.cos(direction)), (int) (speed * Math.sin(direction))); 
     } 

    public void run() 
    { 

     GImage img1 = new GImage("balloon.jpg"); 
     add(img1, 0, 0); 
     GImage img2 = new GImage("balloon.jpg"); 
     add(img2, 200, 200); 

     while(true) 
     { 
      pause(15); 
      img1.move(speed1.x, speed1.y); 
      img2.move(speed2.x, speed2.y); 



      if (img1.getX() > APPLET_WIDTH - 50 || img1.getX() < 1) 
      { 
       speed1 = moveRandowmDirection(); 
      } 

      if (img1.getY() +85 > APPLET_HEIGHT || img1.getY() < 1) 
      { 
       speed1 = moveRandomDirection(); 
      } 

      if (img2.getX() > APPLET_WIDTH - 50 || img2.getX() < 1) 
      { 
       speed2 = moveRandomDirection(); 
      } 

      if (img2.getY() +85 > APPLET_HEIGHT || img2.getY() < 1) 
      { 
       speed2 = moveRandomDirection(); 
      } 
     } 
    } 
} 

編輯:這修復了「鏈接」行爲,我投了Rob Watts對圖像與邊緣碰撞時解決問題的回覆。

+0

插入它後幾乎完美的作品,但它仍然看起來像氣球連接在某種程度上,當氣球2撞牆壁氣球一改變方向以及即使它不是靠近牆壁,反之亦然,它們完全按照隨機方向完全相反,但奇怪的是看到一個開關沒有撞牆。 – James

+0

好的,我會看看。我將編輯答案以反映變化。 – Nolo