Linked List Problem

I'm working on a linked list version of my asteroids program. I run into an error when I change it from a vector to a linked list. I'm not sure how to go about fixing it. Any help would be appreciated.

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
234
235
236
237
238
239
240
// 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 <list>

class Asteroids {
public:

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


		//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;
		
		//Check Value of Asteroid
		if (astDestroyed)
			return;

		//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 >= 520) 
			{
				asty_cnt = (asty_cnt * (-1));
			}

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

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

			if (astx <=-100)
			{
				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);
				astDestroyed = true;
			} 
		}
	}
		

	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;
	bool astDestroyed;



};

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); 
	
	//Random Asteroid Location
	srand ((unsigned int) time(0));

	//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);
		
	
	std::list<Asteroids*> asteroid;
		for (int i=0; i<10; ++i){
			asteroid.push_back(new Asteroids);
		}
	// our main loop
	while ( LoopGDK ( ) )
	{
		for (unsigned int i=0; i<asteroid.size(); ++i){
			asteroid[i]->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);
			dbMoveSprite(3,5);
			dbOffsetSprite(3,5,5); 
		}
		
		// update the screen
		dbSync ( );
	}

	// return back to windows
	return;
}
1
2
3
for (unsigned int i=0; i<asteroid.size(); ++i){
    asteroid[i]->Location();
}

Linked Lists don't feature random access. That's the only way they can guarantee their efficiency and flexibility.

Since you're looping through the list anyway, you can use an iterator that starts at begin(), stops when it reaches end() and increase the iterator rather than an index.

Can't really find the iterator page itself, but here's a little snippet(*):
1
2
for (list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
    cout << *it << " ";


The iterator is basically a pointer to a "current item". Increasing the iterator jumps to the next item. To use the item, dereference it (or use arrow notation to access members).

* http://www.cplusplus.com/reference/stl/list/list/
Topic archived. No new replies allowed.