0

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

Thief3
  • 18
  • 3
  • Do you have anything you have tried so far that you could add to this? – Farmer Joe Oct 29 '13 at 22:17
  • @farmerjoe No sorry. This is my first question on stackoverflow so I didn't keep track of them but I'll keep it in mind for my next question. – Thief3 Oct 29 '13 at 22:51

2 Answers2

1

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;
    }
}
Community
  • 1
  • 1
D. Visser
  • 875
  • 2
  • 12
  • 19
  • I added a little overall part, and by timer i mean the time in between each image generated. Thank you for the answer and I apologize if this doesn't help you understand my other problems. – Thief3 Oct 29 '13 at 23:35
  • Thanks, All the code works, and the explanations are really helpful(really thank you). I'm just wondering if it's possible to change the amount of time the image, because the image only there for a second. I know that it's because of the timer's reset. I apologize that i'm asking for more. – Thief3 Oct 30 '13 at 20:16
  • I would recommend keeping track of all images in an ArrayList. Then creating a class that contains the information about the image. That class would go into the ArrayList. When rendering, you iterate through the ArrayList to draw every image that it contains. Again, my Slick2D knowledge is very limited, so this is probably not the best thing to do performance-wise. As for the implementation, check my second update of the answer. – D. Visser Oct 30 '13 at 20:40
  • Could you paste a section of code? Perhaps a screenshot. I'm not quite sure what you're doing here. Normally, a local initialization would look like this: `ArrayList images = new ArrayList();` You say you initialized it as `images = new ArrayList();` which should be `images = new ArrayList();` – D. Visser Oct 30 '13 at 23:56
  • You're welcome. Don't mind coming back for more. Glad to have been of service! – D. Visser Oct 31 '13 at 00:28
0

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]
}
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • Thank you for the quick answer. The only thing is that when I replaced array with the array i'm using which is 'blocky' it gave me a error saying Type mismatch cannot convert from double to int, so is there something wrong with the way I made my array?: 'int[] blocky = {0, 29, 50, 79, 109, 138, 168, 204, 222, 248, 276, 304, 334, 363, 393, 418, 443};' – Thief3 Oct 29 '13 at 22:44