LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Placing a bunch of trees

Root / Programming Questions / [.]

Z_E_R_OCreated:
I'm trying to put trees in random places and I can't do it. I'm trying with VAR and Dim like this :
DIM Tree%(5,1) 
Tree%(0,1) = 0 -Age of tree
I want 5 trees using the BG and I dunno how to put them with this code....

VAR MAXX = 400 / 16
VAR MAXY = 240 / 16
VAR TREE = 107 'Dont know the right index, replace with desired tile id

BGSCREEN 0, MAXX, MAXY
FOR I = 0 TO 4
 REPEAT
  X = RND(MAXX)
  Y = RND(MAXY)
 UNTIL BGGET(0, X, Y) != TREE
 BGPUT 0, X, Y, TREE
NEXT I

I typed that without testing in SmileBasic, but is that what you are looking for? The repeat until loop is to make sure each tree goes on a new tile.

you use [] for arrays, not ()

Thanks for the help guys. It's partially working. I have to tweak it.

I would change tree to 100 in the example for a better tree picture but otherwise the code above just puts some tiles on the BG graphics layer. I wasn't using arrays, RND is a function call to the random number generator. BGGET is also a function call that gets a tile id from the BG graphics layer. If you need it in an array I would just create tree_x and tree_y empty arrays then push the values in. Instead of bgget you would have to loop through the array so far looking for a duplicate x and y location. Hope that makes sense. Let me know if you have questions.

I'm trying to put trees in random places and I can't do it. I'm trying with VAR and Dim like this :
DIM Tree%(5,1) 
Tree%(0,1) = 0 -Age of tree
I want 5 trees using the BG and I dunno how to put them with this code....
notice the DIM Tree%(5,1)? That is what I'm talking about.