Question about vectors

I am working on an asteroids program for class and I have it mostly done. The only thing that I'm having problems with is using a vector to assign an id to the asteroids as they are being created. I do not understand how vectors work. Any help would be greatly appreciated.
~I'm using darkgdk and visual C++ 2008 express.~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Dark GDK - The Game Creators - www.thegamecreators.com

// the wizard has created a very simple project that uses Dark GDK
// it contains the basic code for a GDK application

// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include <ctime>
#include <vector>

class Asteroids {
public:

	Asteroids() {
		
		//Set Asteroid ID
		ast_id = numObjects;

		//Random Asteroid Location
		srand ((unsigned int) time(0));

		//Variables
		astx = rand() %700;
		asty = rand() %500;
		astx_cnt = 2;
		asty_cnt = 2;
		asrotate = 0;

		numObjects++;
	}

	~Asteroids() {

		numObjects--;
	}

	static int numObjects;

	void Location () {

		//Asteroid Variables
		asmove = 0;
		
		//Asteroid Movement Variables
		dbSprite(ast_id,astx,asty,2);
		astx = astx + astx_cnt;
		asty = asty + asty_cnt;
		astx_inc = astx+1;
		asty_inc = asty+1;

	
		//Rotate Asteroid 1
		asrotate = asrotate +5;
		dbRotateSprite(ast_id,asrotate);
		
		//Place Asteroid Sprite
		if (dbSpriteExist(ast_id))
		{
			
			dbOffsetSprite(ast_id,28,29);

			if (asty >= 450) 
			{
				asty_cnt = (asty_cnt * (-1));
			}

			if (astx >= 650)
			{
				astx_cnt = (astx_cnt * (-1));
			}

			if (asty <=0)
			{
				asty_cnt = (asty_cnt * (-1));
			}

			if (astx <=0)
			{
				astx_cnt = (astx_cnt * (-1));
			}
		}

		Collision(ast_id);
	}

	void Collision(int ast_id){

		int astx1;
		int asty1;

		//Circular Collision Variables
		radius = 28 + 22;
		shipx = dbSpriteX(1);
		shipy = dbSpriteY(1);
		distx = shipx - astx;
		disty = shipy - asty;

		//Circular Collision
		if (dbSpriteExist(ast_id))
		{
			astx1 = dbSpriteX(ast_id);
			asty1 = dbSpriteY(ast_id);
		}

		//set collision for ship and asteroids
		if (dbSpriteExist(ast_id))
		{
			if ((radius * radius) > (distx * distx) + (disty * disty))
			{
				dbDeleteSprite(1);
			}
		}
		
		if (dbSpriteExist(3))
		{
			dbMoveSprite(3,10);

			if (dbSpriteHit(ast_id,3))
			{
				dbDeleteSprite(ast_id);
				dbDeleteSprite(3);
			} 
		}
	}
		

	private:

	//Asteroid Variables
	int asrotate;
	int asmove;
	int ast_id;
	
	int astx;
	int asty;
	
	//Asteroid Movement Variables
	int astx_inc;
	int asty_inc;
	int astx_cnt;
	int asty_cnt;

	//Circular Collision Variables
	int radius;
	int shipx;
	int shipy;
	int distx;
	int disty;


};

int Asteroids::numObjects=5;

// the main entry point for the application is this function
void DarkGDK ( void )
{
	// turn on sync rate and set maximum rate to 60 fps
	dbSyncOn   ( );
	dbSyncRate ( 60 ); 
	dbSetWindowSize(600,400); 

	//Load Images
	dbLoadImage("ship.png",1);
	dbLoadImage("pokeball.png",2);
	dbLoadImage("bullet.png",3);

	//Ship Variables
	int shiprotate = 0;

	//Bullet Variables
	int bulletx;
	int bullety;

	//Place Ship Sprite
	dbSprite(1,300,250,1);
	dbOffsetSprite(1,26,26);
		
	Asteroids ast1;

	
	//Vector
	//vector<Asteroids> asteroid;

	/*for (vector<Asteroids>::size_type numObjects = 0; numObjects < 10; numObjects++)
	{

	}
	*/

	// our main loop
	while ( LoopGDK ( ) )
	{
		ast1.Location();

		//Bullet
		bulletx = dbSpriteX(1);
		bullety = dbSpriteY(1);

		//Ship Movement (stays in the main loop)
		if (dbUpKey())
		{
			dbMoveSprite(1,5);
		}
		if (dbDownKey())
		{
			dbMoveSprite(1,-5);
		}
		if (dbLeftKey())
		{
			shiprotate = shiprotate -5;
			dbRotateSprite(1,shiprotate);
		}
		if (dbRightKey())
		{
			shiprotate = shiprotate +5;
			dbRotateSprite(1,shiprotate);
		}

		if (dbKeyState(0x39))
		{
			dbSprite(3,bulletx,bullety,3);
			dbRotateSprite(3,shiprotate);
			dbOffsetSprite(3,5,5); 
		}
		
		// update the screen
		dbSync ( );
	}

	// return back to windows
	return;
}
Hi, how about this.
In initializing,
1
2
3
4
5
// you get 10 asteroids in vector.
std::vector<Asteroids*> asteroid;
for (int i=0; i<10; ++i){
	asteroid.push_back(new Asteroids);
}


and, in the loop.
1
2
3
4
// you can operate each asteroids  
for (unsigned int i=0; i<asteroid.size(); ++i){
	asteroid[i]->location();
}


I used the pointer of 'Asteroids' for vector,
because 'Asteroids' doesn't have method to copy.
@pot

I see what your getting at. So that would create a total of 10 asteroids to be displayed on screen? Now another question, would that also work for assigning the asteroids a number? (I have a variable used to assign asteroids an id)

1
2
3
4
5
6
7
8

//the variable
int ast_id;

//as well as the assignment for it

ast_id = numObjects;
Topic archived. No new replies allowed.