【发布时间】:2020-07-25 14:33:15
【问题描述】:
我是 Java 游戏的新手,尤其是 Java 图形的新手。 我正在尝试制作一个游戏,其中棋盘的每个瓷砖都有 40% 的机会成为填充障碍。 我已经对一些部分进行了编码,但它并没有按照我想要的方式工作。它填满了整个电路板,但我只想让一些部分成为障碍。 这是我的机会和绘图方法代码:
/////////////////////////////////////////
//Draws the graphics needed for the game.
////////////////////////////////////////
public void paint(Graphics g) {
g.setColor(Color.BLUE);
for(int i=1;i<boardWidth/10;i++) {
g.drawLine(i*100, 100, i*100, HEIGHT-100);
}
for(int j=1;j<boardHeight/10;j++)
g.drawLine(100, j*100, WIDTH-100, j*100);
for(int i=100;i<WIDTH-200;i++)
for(int j=100;j<HEIGHT-200;j++)
if(randomBarrier())
g.fillRect(i, j, 100, 100);
}
///////////////////////////////////////////////////////////////////////////
//finds a random place for the barrier objects in the beginning of the game.
///////////////////////////////////////////////////////////////////////////
public static boolean randomBarrier() { //Should put all the parts to this method to see if the are barrier material or not.
int row = WIDTH/100;
int column = HEIGHT/100;
int min = 0;
int max = row*column;
double random = Math.random();
if(random<0.4)
return true;
else if(random>=0.4)
return false;
return true;
}
有人可以帮我解决这个问题吗?
【问题讨论】:
-
您在文本中声明您希望有 0.4% 的机会出现障碍,但您的代码使用了 40% 的机会出现障碍。是哪个?
-
哦,对不起。实际上是 40%。
-
就是说,你的方法
randomBarrier可以写成一行:return Math.random() < 0.4;
标签: java swing graphics game-development