Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Random image placement? [Array]  (Read 2208 times)
0 Members and 1 Guest are viewing this topic.
Offline Shazer2

Junior Devvie


Medals: 3


Aspiring developer.


« Posted 2011-05-15 11:16:15 »

I am trying to achieve random image placement with just for loops. The code I was using is:
1  
2  
3  
4  
5  
6  
7  
for (int tx = 0; tx <= roomWidth; tx += 80) {
   for (int ty = 0; ty <= roomHeight; ty += 80) {
      if (tx < roomWidth && ty < roomHeight) {
         g.drawImage(tree, (int) Math.round(Math.random() * tx), (int) Math.round(Math.random() * ty), null);
      }
   }
}


That obviously has a few complications,
a) when I use more then 1 random value create an instance of Random and use that instead of Math.random()
b) might result in drawing trees on top of each other
c) it will be different on every redraw (bad)

I've thought of doing it a different way using arrays but I'm not sure how to approach it. I think I'll use a random object and place a different position on each element (maybe), but I don't know. So if anyone can explain this to me it would be much appreciated.

Shazer2

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-15 12:24:36 »

Just create an array and generate some random X and Y values into it. If you want them to be different, check them against each earlier X and Y.

Offline zoto

Senior Devvie


Medals: 4



« Reply #2 - Posted 2011-05-15 16:49:32 »

Create an object that has the image and location, only set it when you initialize.

Also your first tree will currently always be at 0, 0.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #3 - Posted 2011-05-17 19:49:16 »

I personally wouldnt draw image to random locations, but make an nD-array and have the numbers be randomly generated into those.  Then draw the images from that.  Just have it populate the array once, so that way it wont keep regenerating and recalculating new random variables every loop/redraw.    You can still manually choose to randomize it later by recalling the randomizer/repopulate/regenerate function

I personally feel that this gives you more control. 


Since you said trees, I'd assume that each image is a tree, and this is a forest? 

Do you want it 100% random location?  Do you want minimum distance from each other or grouping/clustering (or avoidance of clustering) 

"Experience is what you get when you did not get what you wanted"
Offline DzzD
« Reply #4 - Posted 2011-05-17 21:51:18 »

you may use a grid placement to avoid two object to overlap and a stack of pos and maybe a PRNG to avoid different generation, not very optimised but should work well until you use very small object on very big area.

something like :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
Vector freePos=new Vector();
int nbPosX = screenWidth / objectWidth;
int nbPosY = screenHeight / objectHeight;
 
//fill free pos
for(int cx=0;cx<nbPosX :cx++)
 for(int cy=0;cy<nbPosY :cy++)
  freePos.addElement(new Point(cx,cy));

for(int n=0;n<nbObject;n++)
{
 int pos=(int)(pseudoRandom()*freePos.size());
 Point gridPos=(Point)freePos.elementAt(n);
 object[n].pos=gridPos;
 freePos.removeElementAt(n);
}

Offline ReBirth
« Reply #5 - Posted 2011-05-18 13:44:30 »

I personally wouldnt draw image to random locations, but make an nD-array and have the numbers be randomly generated into those.  Then draw the images from that.  Just have it populate the array once, so that way it wont keep regenerating and recalculating new random variables every loop/redraw.    You can still manually choose to randomize it later by recalling the randomizer/repopulate/regenerate function
It sounds good. sample code maybe?

Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #6 - Posted 2011-05-18 19:11:32 »

I'd do something like this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
//Screen size 800x600
    public int treeCount = 30;
    public int[] treeLoc = new int[2*treeCount];
    Random r = new Random();
   
    public void RandomizeTreeLocs(){ //needs to be called once, during init
        for(int i=0; i<(2*treeCount);i+=2){
            treeLoc[i] = r.nextInt(800); // X location
            treeLoc[i+1] = r.nextInt(600); // Y Location
        }
    }
   
    public void drawTrees(){
        //blah blah portion
        for(int i=0; i<(2*treeCount);i+=2){
        g.drawImage( treePicture, treeLoc[i], treeLoc[i+1],null);
        }
    }
   
    //Main Loop
    if(keyPressed == R){
        RandomizeTreeLocs();
    }





another method, although in my opinion, less efficient.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
    //Screen size 800x600
    public int tree = 30;
    public int[][] treeLoc = new int[800][600];
    Random r = new Random();
    //treePicture array contains an array of different image types for different trees/rocks/grass
   
    public void RandomizeTreeLocs(){ //needs to be called once, during init
        for(int i=0; i<tree;i++){
            treeLoc[r.nextInt(800)][r.nextInt(600)] = 1;
            // use different numbers for different "types of trees"
        }
    }
   
    public void drawTrees(){
        //blah blah portion
        for(int i=0; i<800;i++){
            for(int j=0; j<600;j++){
                if( treeLoc[i][j] > 0){
                    //g.drawImage( treePicture[ treeLoc[i][j] ], i, j,null);
                }
            }
        }
    }
   
   
    //Main Loop
    /*if(keyPressed == R){
        RandomizeTreeLocs();
    }



There are better ways to write the above, these are just some quickly thrown together ideas.
Depending on the type of game and what all you need/want from it.

I think making the tree an entity, with x/y coordinates inside of it,  perhaps with other variables and functions.

Then just maintaining a EntityList of all the trees,  You could also with entities, have them check for distance to other trees and remove themselves if they are too close, or perhaps change the type of tree,(i.e. if 1 tree has 5 trees close to it, make itself a baby tiny tree) if the tree has nothing close to it, make it a bigger/older tree?


"Experience is what you get when you did not get what you wanted"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (22 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!