i am trying to code my sprites to collect items which then display a score in the top left of the screen depending on which items they collect, each sprite has their own item and there is an item which deducts score. However i am at a loss as to how to do this. Help please, i am using Visual C++ with SDL

Dani AI

Generated

asked about per-sprite item pickup and score display; 's idea of using an item class hierarchy is a good start. The practical pieces to add are: make each Item carry a point value (positive or negative), an owner/filter (which sprite(s) may collect it), a bounding rect/position, and an active flag or respawn timer. Give each Sprite its own score field so items simply modify that value when collected.

Run a collect pass each frame: test bounding-box overlap between sprite and item, then check the item is active and allowed for that sprite. On a successful collect, add the item value to the sprite score, set the item inactive (or mark it for deletion), and spawn any effects. Avoid erasing items from the container while iterating; instead mark-for-deletion and remove them after the loop (erase-remove idiom works well).

For drawing the score use a text layer (SDL_ttf or a bitmap font). Re-create the text surface/texture only when the score changes to avoid wasted work. If using SDL2 render to a texture; with SDL 1.2 use surfaces+blit. See the SDL_ttf docs for details on rendering text: SDL_ttf documentation. For safe removal patterns see the erase/remove idiom: .

Debugging notes: render item and sprite bounds to verify collisions, log collect events and resulting scores, clamp scores if you want no negatives, and ensure the item is deactivated immediately to prevent double-collection in the same frame.

Recommended Answers

All 2 Replies

Create a hierarchy :

class Item{
 //...
 virtual int points()const; //returns how much points this item is worth
};
class Book{
 //...
 int points()const{ return 2; }
};
class Food{
 //...
 int points()const{ return 5; }
} ;
//...

can anyone be any more specific?

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.