I am making a jump and run game- i want my sprites to collect items that are worth points. I want these items to be randomly generated on my tile map.
Does anyone know how i can do this? I am using Visual C++ with SDL

Dani AI

Generated

Short summary and two practical ways to handle this (based on what said and the responses from and ).

If the level editor can export an object layer or custom tile properties, the simplest solution is to place spawn markers in the editor and load them at runtime. That gives designers control, avoids unreachable or unsafe placements, and makes balancing trivial: export the marker positions and create item objects at those tiles (convert tile coords to pixels with x*tileWidth, y*tileHeight).

If you must place items at runtime, follow these steps:

  • Build a list of candidate tiles: a tile is a candidate if it is empty, the tile below is "ground" (so the item has something to sit on), and the tile is not a hazard or occupied.
  • Optionally filter candidates by reachability: run a flood-fill or pathfinding from the player start to remove unreachable tiles.
  • Randomly pick N unique candidates. Seed your RNG once at startup (as suggested using rand, but prefer <random> in modern C++ for better behavior).
  • Place items and ensure you do collision checks so items don’t overlap moving entities.

Example (concise C++-style pseudocode using C++11 features):

std::vector<int> candidates;
for (int y=0;y<h;++y)
  for (int x=0;x<w;++x)
    if (!isSolid(tileAt(x,y)) && isSolid(tileAt(x,y+1)) && !occupied(x,y))
      candidates.push_back(y*w + x);

std::mt19937 rng(seed);
std::shuffle(candidates.begin(), candidates.end(), rng);

for (int i=0; i<desiredCount && i<candidates.size(); ++i) {
  int idx = candidates[i];
  spawnItemAt((idx % w)*tileW, (idx / w)*tileH);
}

Troubleshooting tips: visualize candidate tiles during testing, use a deterministic seed while debugging, avoid seeding every frame, and prefer editor markers if available to fix placement problems quickly. As hinted — learning the mechanics helps, but these practical steps should get a working item-generation system running with SDL.

Recommended Answers

All 7 Replies

How are you creating the tile map? A 2d Array?

the tile map is created in a level editor, no coding is required

It's really impossible to say without knowing the details of the level editor.

What are you trying to do? Copy a code and get more code from someone else and say
you did it? If you're not going to a programmer, then its ok, I guess. But if you
decide to become a programmer, then stop with this nonsense and start learning.

im not wanting to be a programmer, i am taking a course and one of the papers "is not a programming course" but its more about learning the ins and outs of games, and game play. So we haven't been taught any programming, but are expected to make games using C++. If i had the time and the teaching resource i would love to do this properly but for now this is the best i can do.

Look up srand and rand to generate random numbers.
If you have ten different item types, generate a number between 0 and 9 and create the item matching that number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.