I'm developing a 2d game for Android.It will be an endless game.I have some items like coins ,plants and other special items.How can i place them effectively?Are there any algorithm or any suggestion for this?

I'm already know that i have to use random numbers for placing them along x and y axis.It's important to not placing two different item at the same x and y position.

2 Answers
2

I've used the following approach to generate apples on a snake clone. It's very simple, although it has some potential problems depending on your game requirements. But if you know will always be a lot more free locations that occupied locations, it should be enough in practice.

Generate a random location for the X and Y axis separately based on the size of the map, but wrap it in a do...while loop that automatically retries whenever the location is already occupied. Something like:

A safer alternative is to somehow keep a list of all the free locations in the screen, and remove one element at random from the list when you need to spawn a new object. This is great in a tile-based game, but even in a continuous environment you can still divide it into an imaginary grid to limit the number of locations.

Since the game world is endless, I would chunk in up into regions. When the player moves close to the edge of the region in one direction, you can generate the objects in the next region in that direction. As the games runs, you can adjust the level play by controlling how many "good" and "bad" objects you place in the upcoming region to keep the player engaged. For example, if the player's "health" is slipping, you may want to adjust the number of "bad" object down or increase the number of "good" objects in the next region he encounters.
David's answer is great for spawning objects. You may want to adjust it slightly so that you don't put your game objects too close together or too far apart. Instead of testing for intersection, just test a bigger area around the potential location.
Consider having a few dozen or so predefined regions that are "fun" to play.