Question

I'm making a 2D Java game and I'm trying to figure out how to generate an image every few seconds(2 or 3) with the y axis picked randomly from an array of numbers. I don't know how to make the more than 1 of the same image appear on the screen at once, making a timer, or getting a random number out of an array.

Can someone please help me with this? I've been stuck on this for about 3 weeks, also some tips on collision would be appreciated.

*Overall I'm trying to have images generate at a y that is randomly picked from a array "int[] blocky = {0, 29, 50, 79, 109, 138, 168, 204, 222, 248, 276, 304, 334, 363, 393, 418, 443}; "

Was it helpful?

Solution

Assuming your array of numbers looks like this, initialized somewhere in your class:

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

You can create a method looking like this:

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

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

Then you could call that method like this:

int randomNumberFromArray = getRandomNumberFromArray();

Look into this thread for a detailed explanation of Java's Random implementation.

As for your other problems, I'm not quite sure what you are trying to accomplish. Please provide some more details.


Update

With your OP updated, I think I have a better idea of what you're trying to do. Allow me to revise.

First, for getting images displayed with Slick2D, here is a good, short video tutorial. I recommend you check his series on the subject, as they are very clear and concise.

As for your timer, I found this piece of code 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);
}

This will draw a simply timer that on the screen at x=100, y=100 that updates itself. This is something you could use for test purposes. As for your random interval, you can expand on the above code like this.

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
    }
}

This piece of code creates a deadline that functions as a random interval timer. The deadline is a summation of the current time, to which a number between 2 and 3 will be added (multiplied with 1000, because time is kept in milliseconds). When the current time surpasses this deadline, a random image will be created.

As for displaying multiple images, I would refer to the previously linked tutorial. I think this could be accomplished by keeping track of the images in an ArrayList. Then rendering all of those images in the render() method. But I'm not quite sure since I only know a few basics of Slick2D.

I haven't tested any of this, but I hope to have pointed you into the right direction.

Update 2

As for the recommendation in the comment, this would be a way of implementing that.

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 class

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;
    }
}

OTHER TIPS

You don't need a timer at all.

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]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top