如何在java中使用随机数组数和计时器?

如何在java中使用随机数组数和计时器?,java,arrays,random,timer,slick2d,Java,Arrays,Random,Timer,Slick2d,我正在制作一个2D Java游戏,我试图找出如何每隔几秒钟(2或3)生成一个图像,其中y轴是从一个数字数组中随机选取的。我不知道如何使同一图像中的多个同时出现在屏幕上,制作计时器,或从数组中获取随机数 有人能帮我吗?我已经被困在这个约3个星期,也有一些关于碰撞的提示将不胜感激 *总的来说,我试图让图像在y轴上生成,y轴是从数组中随机选取的 “int[]blocky={0,29,50,79,109,138,168,204,222,248,276,304,334,363,393,418,443};”

我正在制作一个2D Java游戏,我试图找出如何每隔几秒钟(2或3)生成一个图像,其中y轴是从一个数字数组中随机选取的。我不知道如何使同一图像中的多个同时出现在屏幕上,制作计时器,或从数组中获取随机数

有人能帮我吗?我已经被困在这个约3个星期,也有一些关于碰撞的提示将不胜感激

*总的来说,我试图让图像在y轴上生成,y轴是从数组中随机选取的
“int[]blocky={0,29,50,79,109,138,168,204,222,248,276,304,334,363,393,418,443};”

你根本不需要计时器

while (true) {
    Thread.sleep(x); //x is the number of milliseconds you want the computer to wait for
    int y = Math.random() * array.length;  //array is the name of the array
    //Create your new image, with y-value as array[y]
}

假设您的数字数组如下所示,在类中的某个位置初始化:

int[]number=新的int[]{123321456765923931}

您可以创建如下所示的方法:

private int getRandomNumberFromArray(){
     int min = 0;
     int max = numbers.length;

     return numbers[min + (int)(Math.random() * ((max - min) + 1))];
}
int randomNumberFromArray = getRandomNumberFromArray();
然后您可以这样调用该方法:

private int getRandomNumberFromArray(){
     int min = 0;
     int max = numbers.length;

     return numbers[min + (int)(Math.random() * ((max - min) + 1))];
}
int randomNumberFromArray = getRandomNumberFromArray();
查看Java随机实现的详细说明

至于你的其他问题,我不太清楚你想完成什么。请提供更多细节


更新 更新了你的OP后,我想我对你想做的事情有了更好的了解。请允许我修改一下

首先,对于使用Slick2D显示图像,这是一个很好的简短视频教程。我建议你检查一下他关于这个主题的系列文章,因为它们非常清晰和简洁

至于你的计时器,我找到了这段代码

这将在屏幕上画一个简单的计时器,在x=100,y=100时自动更新。这是可以用于测试目的的东西。至于您的随机间隔,您可以像这样展开上面的代码

int time;
int deadline; // initialize this somewhere in the start of your game

public void update(GameContainer container, int delta) {
    time += delta;
}

public void render(GameContainer container, Graphics g) {
    g.drawString("Time : " + time/1000, 100, 100);

    // if the current time has passed the deadline, do something
    if(time > deadline){
        int min = 2;
        int max = 3;
        deadline = time + ((min + (int)(Math.random() * ((max - min) + 1))) * 1000); // reset the interval timer

        // draw the image
        int x = 100;
        int y = getRandomNumberFromArray(); // this is the method I provided in my first part of the answer
        Image img = new Image("path/file.png");
        g.drawImage(img, x, y) // image file, x, y
    }
}
这段代码创建了一个作为随机间隔计时器的截止日期。截止日期是当前时间的总和,其中将添加一个介于2和3之间的数字(乘以1000,因为时间以毫秒为单位)。当当前时间超过此截止日期时,将创建一个随机图像

至于显示多个图像,我将参考前面链接的教程。我认为这可以通过在ArrayList中跟踪图像来实现。然后在render()方法中渲染所有这些图像。但我不太确定,因为我只知道一些关于Slick2D的基本知识

我还没有测试过这些,但我希望能为您指出正确的方向

更新2 至于评论中的建议,这将是实现这一点的一种方式

ArrayList<DrawnImage> images; // make sure you initialize this at the beginning as: images = new ArrayList<Image>();
int time;
int deadline; // initialize this somewhere in the start of your game

public void update(GameContainer container, int delta) {
    time += delta;
}

public void render(GameContainer container, Graphics g) {
    g.drawString("Time : " + time/1000, 100, 100);

    // if the current time has passed the deadline, do something
    if(time > deadline){
        int min = 2;
        int max = 3;
        deadline = time + ((min + (int)(Math.random() * ((max - min) + 1))) * 1000); // reset the interval timer

        // draw the image
        int x = 100;
        int y = getRandomNumberFromArray(); // this is the method I provided in my first part of the answer
        images.add(new DrawnImage(new Image("path/file.png"), x, y));

        for(DrawnImage di : images){
            g.drawImage(di.image, di.x, di.y); // image file, x, y
        }
    }
}

到目前为止,你有什么可以补充的吗?@farmerjoe不,对不起。这是我关于stackoverflow的第一个问题,所以我没有跟踪他们,但我会在下一个问题中记住。谢谢你的快速回答。唯一的问题是,当我用我正在使用的数组“blocky”替换数组时,它给了我一个错误,说类型不匹配不能从double转换为int,那么我创建数组的方式有什么问题吗?:“int[]blocky={0,29,50,79,109,138,168,204,222,248,276,304,334,363,393,418,443};'我添加了一个小的整体部分,我所说的计时器是指生成每个图像之间的时间。谢谢你的回答,如果这不能帮助你理解我的其他问题,我向你道歉。谢谢,所有的代码都很有效,解释也很有帮助(真的谢谢你)。我只是想知道是否有可能改变图像的时间量,因为图像只存在一秒钟。我知道这是因为计时器重置了。很抱歉,我要求更多。我建议在ArrayList中跟踪所有图像。然后创建一个包含图像信息的类。该类将进入ArrayList。渲染时,可以迭代ArrayList以绘制其中包含的每个图像。再说一次,我的Slick2D知识非常有限,因此这可能不是性能方面最好的做法。至于实现,请检查我对答案的第二次更新。你能粘贴一段代码吗?也许是截图。我不太清楚你在这里干什么。通常,本地初始化如下所示:
arraylistimages=newarraylist()
您说您将其初始化为
images=newarraylist()应该是
images=newarraylist()不客气。不介意再来多吃点。很高兴为您服务!