我正在研究一個小遊戲,牆上有你,你必須通過差距閃避它們我無法在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;
}
}
我認爲你將需要提供多一點的代碼。 – Carcigenicate