2015-07-21 66 views
1

我正在研究一個小遊戲,牆上有你,你必須通過差距閃避它們我無法在java中獲得我的算法

我的問題是我無法得到我的大小相等的間隙

int x = random.nextInt(Game.WD - 200) + 100; 
int y; 
int HEIGHT = 10; 
int WIDTH = Game.WD - x; 
int gap = 100; 

public void paint(Graphics g){ 
    g.drawRect(x, y, WIDTH , HEIGHT); //right part 
    g.drawRect(-x, y, (Game.WD - gap) , HEIGHT); //left part 

我知道它與這部分課程有關。當我運行這個時,它給了我不平等的差距。我希望這是足夠的信息。

game.WD等於500

這裏是全班以防萬一:

import java.awt.Graphics; 

進口java.awt.Rectangle中; import java.util.Random;

公共類邊境

Random random = new Random(); 

int x = random.nextInt(Game.WD - 200) + 100; 
int y; 
int speed = - 6;  
int HEIGHT = 10; 
int WIDTH = Game.WD - x; 
int gap = 100; 


@SuppressWarnings("unused") 
private Game game; 

public Border(int i) { 
    this.y = i; 
} 


public void paint(Graphics g){ 
    g.drawRect(x, y, WIDTH , HEIGHT); 
    g.drawRect(-x, y, (Game.WD - gap) , HEIGHT); 

} 

public Rectangle getBounds3(){ 
    return new Rectangle(x, y, WIDTH, HEIGHT); 
} 

public Rectangle getBounds4(){ 
    return new Rectangle(x, y, WIDTH, HEIGHT); 
} 

public void move() { 
    y += speed; 


    if (y <= 0){ 
     y = Game.HE; 
     x = random.nextInt(Game.WD - 200) + 100; 
     WIDTH = Game.WD - x; 
    } 

} 
+0

我認爲你將需要提供多一點的代碼。 – Carcigenicate

回答

0

-x是你的錯誤,這是沒有意義的使用值是x在y軸上的反射鏡。

對於這一點:

0--a<gap>x---b 

定義,像這樣:

//a+gap = x so: 
a = x - gap; 
b = Game.WD - x; 

g.drawRect(0, y, a, HEIGHT); //left part 
g.drawRect(x, y, b, HEIGHT); //right part 
+0

謝謝,它幾乎完美地工作,但右側仍然不平等。也許x有什麼問題? – 2hTu2

+0

你說你想要一個差距。你沒有說一面平坦的牆。 – weston