模拟雨 [英] Simulating rain
问题描述
我正在用java制作游戏,我想创建一个倾盆大雨的模拟。在下雨的时候,云应该向右移动。移动云是没有问题的。这是我正在努力的下雨。
I am making a game in java and I want to create a simulation of a cloud that is pouring rain. The cloud is supposed to move to the right while raining. Moving the cloud is no problem. It's the rain that I am struggling with.
我想做的是使用计时器绘制一个矩形,这应该看起来像是在云中的随机x值下雨。然后每100毫秒将下降的y值加1。但我不想为每次雨滴创建100个不同的矩形,x变量和y变量。
What I was thinking of doing was with a timer to draw a rectangle, thats supposed to look like falling rain at a random x value inside of the cloud. And then add 1 to the y value of the drop each 100 millisecond. But I don't want to create 100 different rectangles, x variables and y variables for each rain drop.
我知道如何才能做到这一点?建议赞赏!
Any idea how I can accomplish this? Suggestions appreciated!
这是一场2D游戏..抱歉。
It is a 2d game.. Sorry.
推荐答案
我建议只将值存储为对象的ArrayList。
I would recommend just storing the values as an ArrayList of objects.
class Raindrop {
private int x;
private int y;
public void fall() {
y--;
}
}
然后创建一个具有泛型类型的ArrayList。
Then make an ArrayList with a generic type.
ArrayList<Raindrop> drops = new ArrayList<Raindrop>();
为了降低每一次跌幅,
for (int i=0; i<drops.length(); i++) {
drops.get(i).fall();
}
这篇关于模拟雨的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!