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

标签 java arrays random timer slick2d

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

有人可以帮我解决这个问题吗?我已经在这个问题上坚持了大约 3 周,如果能提供一些关于碰撞的提示,我将不胜感激。

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

最佳答案

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

int[] 数字 = new int[]{ 123, 321, 456, 765, 923, 931 };

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

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

     return numbers[min + (int)(Math.random() * ((max - min) + 1))];
}

然后你可以像这样调用该方法:

int randomNumberFromArray = getRandomNumberFromArray();

查看 this thread 了解 Java Random 实现的详细说明。

至于你的其他问题,我不太确定你想要实现什么目标。请提供更多详细信息。

<小时/>

更新

随着你的操作更新,我想我对你想要做什么有了更好的了解。请允许我修改一下。

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

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

int time;

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

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

这将在屏幕上 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
        }
    }
}

DrawnImage类

public class DrawnImage {
    public Image image;
    public float x;
    public float y;

    public DrawnImage(Image image, float x, float y){
        this.image = image;
        this.x = x;
        this.y = y;
    }
}

关于java - 如何在java中使用随机数组和计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670206/

相关文章:

java - 忽略句点但在单词末尾打印句点

java - 将 Firefox 配置文件设置为使用 Selenium 和 Java 自动下载文件

java - 由大元素和小元素组成的 float 组的总和

Java:查找数组中 double 的平均值

arrays - 在 Google 表格中将 2D 范围转换为带有空格的 1D 数组

java - GlassFish 3.1 问题与/faces/*

java - 隐藏 Storm UI 的配置值

php - 获取 mysql 结果,然后随机化结果顺序,然后显示一个与其他结果不同的结果

c++ - 用不重复的随机数填充二维数组

python - 如何随机选择具有嵌套列表的列表