Question about code

I'm having an issue having the image for the bullet track the ships location and shoot horizontally. I had it track the ship for my asteroids program and now I can't figure out why it won't track.

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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

// 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"

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,5);
		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(7))
		{
			dbMoveSprite(7,bullet_speed);

			if (dbSpriteHit(ast_id,7))
			{
				dbDeleteSprite(ast_id);
				dbDeleteSprite(7);
				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;

	//Bullet variables
	int bullet_speed;



};

int Asteroids::numObjects=10;

// 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 );

	//Load Images
	dbLoadImage("ship.png",1);
	dbLoadImage("nightsky.jpg",2);
	dbLoadImage("city.png",3);
	
	dbCreateAnimatedSprite(4,"sprite.png",3,1,4); //creates animated civilian sprite
	
	dbLoadImage("pokeball.png",5);
	dbLoadImage("cloud.png",6);
	dbLoadImage("bullet.png",7);
	
	dbSetWindowSize(1024,768);  

	//Sprites
	dbSprite(2,0,0,2); //Sky
	dbSprite(3,0,280,3); //City
	dbSprite(1,1,250,1); //Ship
	dbSprite(6,0,0,6); //Cloud
	dbSprite(4,0,400,4); //animated sprite

	//Sprite Priority
	dbSetSpritePriority (2,0); //back image
	dbSetSpritePriority (3,1);
	dbSetSpritePriority (7,2);
	dbSetSpritePriority (1,3);
	dbSetSpritePriority (4,4);
	dbSetSpritePriority (6,5);
	dbSetSpritePriority (5,6); //front Images
	
	
	

	//Variables
	float left = 0.0;
	float right = 1.0;
	
	float leftcity = 0.0;
	float rightcity = 1.0;

	float leftcloud = 0.0;
	float rightcloud = 1.0;

	//Variables
	int bulletx = dbSpriteX(1);
	int bullety = dbSpriteY(1);

	std::vector<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();
		}
		dbPlaySprite(4,1,3,300);
		
		//Scrolling Background
		dbSetSpriteTextureCoord(2, 0, left, 0);
		dbSetSpriteTextureCoord(2, 1, right, 0);
		dbSetSpriteTextureCoord(2, 2, left, 1);
		dbSetSpriteTextureCoord(2, 3, right, 1);

		//Scrolling City
		dbSetSpriteTextureCoord(3, 0, leftcity, 0);
		dbSetSpriteTextureCoord(3, 1, rightcity, 0);
		dbSetSpriteTextureCoord(3, 2, leftcity, 1);
		dbSetSpriteTextureCoord(3, 3, rightcity, 1);

		//Scrolling Clouds
		dbSetSpriteTextureCoord(6, 0, leftcloud, 0);
		dbSetSpriteTextureCoord(6, 1, rightcloud, 0);
		dbSetSpriteTextureCoord(6, 2, leftcloud, 1);
		dbSetSpriteTextureCoord(6, 3, rightcloud, 1);

		//Background Movement
		if (dbLeftKey())
		{
			left += 0.002;
			right += 0.002;

			leftcity += 0.002;
			rightcity += 0.002;

			leftcloud += 0.0023;
			rightcloud += 0.0023;
		}

		//Ship Movement
		if (dbUpKey())
		{
			dbMoveSprite(1,5);
		}
		if (dbDownKey())
		{
			dbMoveSprite(1,-5);
		}
		if (dbSpriteExist(1)) {

			if (dbKeyState(0x39))
			{
				dbSprite(7,bulletx,bullety,7);
				dbOffsetSprite(7,5,5); 
			}
		}

		//Civilian Collision
		if (dbSpriteExist(4))
		{
			if (dbSpriteHit(1,4))
			{
				dbDeleteSprite(4);
			}
		}
		

		// update the screen
		dbSync ( );
	}

	// return back to windows
	return;
}
Last edited on
Topic archived. No new replies allowed.