Game Guides > Game FAQ >  

In c plus plus programming how would you do the programming to make random objects drop when an object is destroyed can anyone write the basic code for this for me

In c plus plus programming how would you do the programming to make random objects drop when an object is destroyed can anyone write the basic code for this for me
We can't help you with the graphics programming in C++ since that is platform-specific; there is no generic graphics capability in C++. However, the basic code will require you to design several specific classes of objects, all of which are derived from an abstract base class. To use an example, if the objects are shapes, then you would design an abstract base class named shape, and derive your specific shapes (such as circles, squares, triangles, etc) from that class. The shape class won't have enough information to draw itself on screen, therefore the shape::draw() method must be declared pure-virtual which, in turn, renders the class an abstract base class. Every specific shape must then provide an implementation for the draw method.
To keep track of your objects, use an STL container such as a vector or list, using a pointer (not a reference) to your abstract shape class as the template.
The object that owns this container will ultimately be responsible for creating new objects and adding them to it, as well as destroying objects and removing them from it. To create a new object at random, you will need to generate a pseudo-random number in the range 0 to n-1 (where n is the number of unique objects, excluding the abstract class), and use a switch statement to determine which type of object to instantiate. The newly instantiated object can then be pushed onto your container.