My first Allegro game

Hi guys so lately I've been working on a basic 2d shooter and I've run into a problem, my game starts up but doesn't do anything, and closes outputting The program '[3000] Asteroids.exe: Native' has exited with code 0 (0x0). any ideas?

Atm blitprojectile and blit explosion don't really do anything, they're just placeholders really.

Code below.
Last edited on
can you post your code with identation? i't hard to know what's happening here.
and please use the [code] tags.
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
294
295
#include <allegro.h>
#include <cmath>

#define MAIN_MENU 0
#define	PLAY 1
#define	INSTRUCTIONS 2
#define	YOU_WIN 3
#define	YOU_LOSE 4

void moveAsteroid(void);
void respondToKeyboard(void);
void moveProjectile(void);

volatile int spaceship_x;
volatile int spaceship_y;
volatile int asteroid1_x;
volatile int asteroid2_x;
volatile int asteroid3_x;
volatile int asteroid4_x;
volatile int asteroid5_x;
volatile int asteroid1_y;
volatile int asteroid2_y;
volatile int asteroid3_y;
volatile int asteroid4_y;
volatile int asteroid5_y;
volatile int projectile_x;
volatile int projectile_y;
volatile int explosion_x;
volatile int explosion_y;
volatile int background_x;
volatile int background_y;

int score;
int lives;
int height = 45;
int width = 45;
int menu=0;
int blitexplosion;
int blitprojectile;

BITMAP *spaceship;
BITMAP *asteroid;
BITMAP *projectile;
BITMAP *explosion;
BITMAP *buffer;
BITMAP *background;
BITMAP *menubackground;
BITMAP *menubackgroundwin;
BITMAP *menubackgroundloss;
BITMAP *menubackgroundinstructions;
SAMPLE *explosionSound;
SAMPLE *music;
SAMPLE *fire;
SAMPLE *menumusic;
FONT *pongFont;

bool Collision(float player_x,float player_y, float asteroid_x, float asteroid_y, int width, int height, float projectile_x,float projectile_y, int projectile_width,int projectile_height)
{
	if(player_x + width < asteroid1_x || player_x > asteroid1_x + width || player_y + height < asteroid1_y || player_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid2_x || player_x > asteroid2_x + width || player_y + height < asteroid2_y || player_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid3_x || player_x > asteroid3_x + width || player_y + height < asteroid3_y || player_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid4_x || player_x > asteroid4_x + width || player_y + height < asteroid4_y || player_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid5_x || player_x > asteroid5_x + width || player_y + height < asteroid5_y || player_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid1_x || projectile_x > asteroid1_x + width || projectile_y + height < asteroid1_y || projectile_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid2_x || projectile_x > asteroid2_x + width || projectile_y + height < asteroid2_y || projectile_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid3_x || projectile_x > asteroid3_x + width || projectile_y + height < asteroid3_y || projectile_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid4_x || projectile_x > asteroid4_x + width || projectile_y + height < asteroid4_y || projectile_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid5_x || projectile_x > asteroid5_x + width || projectile_y + height < asteroid5_y || projectile_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}	
	return NULL;
}

int main(void)
{
	allegro_init();
	install_keyboard();
	install_timer();
	install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
	spaceship=load_bitmap("ship",NULL);
	asteroid=load_bitmap("asteroid",NULL);
	projectile=load_bitmap("projectile",NULL);
	explosion=load_bitmap("explosion",NULL);
	background=load_bitmap("background",NULL);
	menubackground=load_bitmap("menu",NULL);
	menubackgroundwin=load_bitmap("menubackgroundwin",NULL);
	menubackgroundloss=load_bitmap("menubackgroundloss",NULL);
	menubackgroundinstructions=load_bitmap("menubackgroundinstructions",NULL);
	explosionSound=load_sample("explosion.wav");
	fire=load_sample("fire");
	music=load_sample("music");
	menumusic=load_sample("menumusic");
	pongFont=load_font("pongfont.pcx",NULL,NULL);
	buffer=create_bitmap(SCREEN_W,SCREEN_H);
	menu = 0;
	spaceship_y=590;
	spaceship_x=SCREEN_W /2;
	asteroid1_x=590;
	asteroid2_x=590;
	asteroid3_x=590;
	asteroid4_x=590;
	asteroid5_x=590;
	asteroid1_y=40;
	asteroid2_y=140;
	asteroid3_y=240;
	asteroid4_y=340;
	asteroid5_y=440;
	score=0;
	lives=3;
	install_int(moveProjectile,5);
	install_int(respondToKeyboard,5);
	install_int(moveAsteroid,5);

	if(key[KEY_1])
	{
		menu=MAIN_MENU;
	}
	if(key[KEY_2])
	{
		menu=PLAY;
	}
	if(key[KEY_3])
	{
		menu=INSTRUCTIONS;
	}
	if (score==100)
	{
		menu=YOU_WIN;
	}
	if (lives==0)
	{
		menu=YOU_LOSE;
	}
	while(!key[KEY_ESC])
	{
		switch (menu)
		{	
			case MAIN_MENU:
				clear_bitmap;
				blit(menubackground,buffer,0,0,0,0,640,480);
				play_sample(menumusic,255,128,1000,1);
			case PLAY:
				clear_bitmap;
				blit(background,buffer,0,0,0,0,640,480);
				play_sample(music,255,128,1000,1);
				blit(background,buffer,0,0,background_x,background_y,background->w,background->h);
				blit(asteroid,buffer,0,0,asteroid1_x,asteroid1_y,asteroid->w,asteroid->h);//Do cords
				blit(asteroid,buffer,0,0,asteroid2_x,asteroid2_y,asteroid->w,asteroid->h);//Do cords
				blit(asteroid,buffer,0,0,asteroid3_x,asteroid3_y,asteroid->w,asteroid->h);//Do cords
				blit(asteroid,buffer,0,0,asteroid4_x,asteroid4_y,asteroid->w,asteroid->h);//Do cords
				blit(asteroid,buffer,0,0,asteroid5_x,asteroid5_y,asteroid->w,asteroid->h);//Do cords
				blit(spaceship,buffer,0,0,0,spaceship_y,asteroid->w,asteroid->h);//cords?
				textprintf_ex(buffer,pongFont,75,0,makecol(0,0,0),-1,"lives %d",lives);
				textprintf_ex(buffer,pongFont,400,0,makecol(0,0,0),-1,"Score %d",score);
				clear_bitmap(buffer);
				line(buffer,0,30,640,30,makecol(0,0,0));
			case INSTRUCTIONS:
				clear_bitmap;
				blit(menubackgroundinstructions,buffer,0,0,0,0,640,480);
				play_sample(menumusic,255,128,1000,1);
			case YOU_WIN:
				clear_bitmap;
				blit(menubackgroundwin,buffer,0,0,0,0,640,480);
				play_sample(menumusic,255,128,1000,1);
			case YOU_LOSE:
				clear_bitmap;
				blit(menubackgroundloss,buffer,0,0,0,0,640,480);
				play_sample(menumusic,255,128,1000,1);
		}
	}

	remove_int(moveAsteroid);
	remove_int(respondToKeyboard);
	destroy_bitmap(asteroid);
	destroy_bitmap(spaceship);
	destroy_bitmap(projectile);
	destroy_bitmap(background);
	destroy_bitmap(menubackground);
	destroy_bitmap(menubackgroundwin);
	destroy_bitmap(menubackgroundloss);
	destroy_bitmap(menubackgroundinstructions);
	destroy_bitmap(explosion);
	destroy_bitmap(buffer);
	destroy_sample(explosionSound);
	destroy_sample(fire);
	destroy_sample(music);
	destroy_sample(menumusic);
	return 0;
}
END_OF_MAIN()
Last edited on
Also here are my functions as I couldn't fit them in the previous post.
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
void moveAsteroid()
{
	++asteroid1_x;
	++asteroid2_x;
	++asteroid3_x;
	++asteroid4_x;
	++asteroid5_x;
	if (asteroid1_x<=-20)
	{
		asteroid1_x = 600;
	}
	if (asteroid2_x<=-20)
	{
		asteroid2_x = 600;
	}
	if (asteroid3_x<=-20)
	{
		asteroid3_x = 600;
	}
	if (asteroid4_x<=-20)
	{
		asteroid4_x = 600;
	}
	if (asteroid5_x<=-20)
	{
		asteroid5_x = 600;
	}
}

void respondToKeyboard()
{
	if (key[KEY_W])
	{
		spaceship_y -=1;
	}
	if (key[KEY_S])
	{
		spaceship_y+=1;
	}
	if (spaceship_y<30)
	{
		spaceship_y=30;
	}
	if (spaceship_y>440)
	{
		spaceship_y=440;
	}
	if (key[KEY_SPACE])
	{
		blitprojectile=true;
		play_sample(fire,255,128,1000,0);
	}
}

void moveProjectile()
{
	--projectile_x;
	if (projectile_x >=620)
	{
		blitprojectile = false;

	}
}
You check if keys are down before the loop (line 205-216). It is very unlikely that the user has pressed any keys yet.

Line 217 and 221 you are using the assignment operator (=) instead of the equal to comparison operator (==). This has the side effect that menu will be set to 3.

You have defined constants for the different menu states so why not use them? In the switch statement you have like this case 0: MAIN_MENU;. Is the expression MAIN_MENU; used as a comment? MAIN_MENU is not zero so I guess this is a mistake. Using constants avoid such mistakes. Using the constant the line would look like this: case MAIN_MENU:.

I see you have removed the loop from the switch statement. You should probably put the switch statement inside a loop because without any looping the switch statement will run once and then close down.
Last edited on
Thanks for the help so far, I've changed some more stuff and updated the code, and I removed the loop for testing and forgot to add it back in, atm when I try debugging I get a read access error, but I'm not sure why.

Error:
Unhandled exception at 0x10005ae9 in Asteroids.exe: 0xC0000005: Access violation reading location 0x00000000
Last edited on
1
2
3
4
5
6
7
8
9
10
11
switch (menu)
{	
	while(!key[KEY_ESC])
	{
case MAIN_MENU:
		blit(menubackground,buffer,0,0,0,0,640,480);
		play_sample(menumusic,255,128,1000,1);
		break;
...
	}
}
The way switch statements jump to case labels is very similar to how goto works. If menu == MAIN_MENU when it comes to the switch statement it will jump to case MAIN_MENU:, ignoring anything in between. When it comes to the break statement it will break out of the loop, so the switch will not run more than once.

What you probably want is to have the while loop outside the switch statement.

The error message you received looks like you are trying to dereference a null pointer. Easiest way to find out where things go wrong is to use a debugger.
Last edited on
Using the debugger it says the next function to execute would be on line 247
1
2
3
4
5
switch (menu)
		{	
			case MAIN_MENU:
				blit(menubackground,buffer,0,0,0,0,640,480);
				play_sample(menumusic,255,128,1000,1);


Updated the code also.
Last edited on
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

bool Collision(float player_x,float player_y, float asteroid_x, float asteroid_y, int width, int height, float projectile_x,float projectile_y, int projectile_width,int projectile_height)
{
	if(player_x + width < asteroid1_x || player_x > asteroid1_x + width || player_y + height < asteroid1_y || player_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid2_x || player_x > asteroid2_x + width || player_y + height < asteroid2_y || player_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid3_x || player_x > asteroid3_x + width || player_y + height < asteroid3_y || player_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid4_x || player_x > asteroid4_x + width || player_y + height < asteroid4_y || player_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid5_x || player_x > asteroid5_x + width || player_y + height < asteroid5_y || player_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid1_x || projectile_x > asteroid1_x + width || projectile_y + height < asteroid1_y || projectile_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid2_x || projectile_x > asteroid2_x + width || projectile_y + height < asteroid2_y || projectile_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid3_x || projectile_x > asteroid3_x + width || projectile_y + height < asteroid3_y || projectile_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid4_x || projectile_x > asteroid4_x + width || projectile_y + height < asteroid4_y || projectile_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid5_x || projectile_x > asteroid5_x + width || projectile_y + height < asteroid5_y || projectile_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}	
	return NULL;
}

int main(void)
{
	allegro_init();
	install_keyboard();
	install_timer();
	install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED,ScreenWidth,ScreenHeight,0,0);
	spaceship=load_bitmap("ship.bmp",NULL);
	asteroid=load_bitmap("asteroid.bmp",NULL);
	projectile=load_bitmap("projectile.bmp",NULL);
	explosion=load_bitmap("explosion.bmp",NULL);
	background=load_bitmap("background.bmp",NULL);
	menubackground=load_bitmap("menubackground.bmp",NULL);
	menubackgroundwin=load_bitmap("menubackgroundwin.bmp",NULL);
	menubackgroundloss=load_bitmap("menubackgroundloss.bmp",NULL);
	menubackgroundinstructions=load_bitmap("menubackgroundinstructions.bmp",NULL);
	explosionSound=load_sample("explosion.wav");
	fire=load_sample("fire.wav");
	music=load_sample("music.wav");
	pongfont=load_font("pongfont.pcx",NULL,NULL);
	buffer=create_bitmap(SCREEN_W,SCREEN_H);
	menu = MAIN_MENU;
	spaceship_y=590;
	spaceship_x=SCREEN_W /2;
	asteroid1_x=590;
	asteroid2_x=590;
	asteroid3_x=590;
	asteroid4_x=590;
	asteroid5_x=590;
	asteroid1_y=40;
	asteroid2_y=140;
	asteroid3_y=240;
	asteroid4_y=340;
	asteroid5_y=440;
	score=0;
	lives=3;
	install_int(moveProjectile,5);
	install_int(respondToKeyboard,5);
	install_int(moveAsteroid,5);

	if(key[KEY_1])
	{
		menu=MAIN_MENU;
	}
	if(key[KEY_2])
	{
		menu=PLAY;
	}
	//if(key[KEY_3])
	//{
		//menu=INSTRUCTIONS;
	//}
	//if (score==100)
	//{
		//menu=YOU_WIN;
	//}
	//if (lives==0)
	//{
		//menu=YOU_LOSE;
	//}

	if (spaceship==0)
	{ 
		allegro_message("spaceship didn't load");
	}
	if(asteroid==0)
	{ 
		allegro_message("asteroid didn't load");
	}
	if(projectile==0)
	{ 
		allegro_message("projectile didn't load");
	}
	if(explosion==0)
	{ 
		allegro_message("explosion didn't load");
	}
	if(background==0)
	{ 
		allegro_message("background didn't load");
	}
	if(menubackground==0)
	{ 
		allegro_message("menubackground didn't load");
	}
	if(menubackgroundwin==0)
	{ 
		allegro_message("menubackgroundwin didn't load");
	}
	if(menubackgroundloss==0)
	{ 
		allegro_message("menubackgroundloss didn't load");
	}
	if(menubackgroundinstructions==0)
	{ 
		allegro_message("menubackgroundinstructions didn't load");
	}
	if(explosionSound==0)
	{ 
		allegro_message("explosionsound didn't load");
	}
	if(music==0)
	{ 
		allegro_message("music didn't load");
	}
	if(fire==0)
	{ 
		allegro_message("fire didn't load");
	}
	if(pongfont==0)
	{ 
		allegro_message("pongfont didn't load");
	}
	while(!key[KEY_ESC])
	{
		switch (menu)
		{	
			case MAIN_MENU:
				blit(background,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
				play_sample(music,255,128,1000,0);
			case PLAY:
				blit(background,screen,0,0,0,0,640,480);
				play_sample(music,255,128,1000,0);
				blit(asteroid,buffer,0,0,asteroid1_x,asteroid1_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid2_x,asteroid2_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid3_x,asteroid3_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid4_x,asteroid4_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid5_x,asteroid5_y,asteroid->w,asteroid->h);
				blit(spaceship,buffer,0,0,spaceship_x,spaceship_y,spaceship->w,spaceship->h);
				textprintf_ex(buffer,pongfont,75,0,makecol(0,0,0),-1,"lives %d",lives);
				textprintf_ex(buffer,pongfont,400,0,makecol(0,0,0),-1,"Score %d",score);
				line(buffer,0,30,640,30,makecol(0,0,0));
			//case INSTRUCTIONS:
				//blit(menubackgroundinstructions,screen,0,0,0,0,640,480);
				//play_sample(music,255,128,1000,0);
			//case YOU_WIN:
				//blit(menubackgroundwin,screen,0,0,0,0,640,480);
				//play_sample(music,255,128,1000,0);
			//case YOU_LOSE:
				//blit(menubackgroundloss,screen,0,0,0,0,640,480);
				//play_sample(music,255,128,1000,0);
		}
	}

	remove_int(moveAsteroid);
	remove_int(respondToKeyboard);
	destroy_bitmap(asteroid);
	destroy_bitmap(spaceship);
	destroy_bitmap(projectile);
	destroy_bitmap(background);
	destroy_bitmap(menubackground);
	destroy_bitmap(menubackgroundwin);
	destroy_bitmap(menubackgroundloss);
	destroy_bitmap(menubackgroundinstructions);
	destroy_bitmap(explosion);
	destroy_bitmap(buffer);
	destroy_sample(explosionSound);
	destroy_sample(fire);
	destroy_sample(music);
	//destroy_sample(menumusic);
	return 0;
}
END_OF_MAIN()

is what I have atm, some stuff is commented out for testing, but my switch just keeps flipping between different images and not allowing any input
Last edited on
Updated where I'm at, having issues with my switch that I can't figure out.
You have removed the breaks from the switch statement so if it jumps to case MAIN_MENU it will also run the code under case PLAY. It will not automatically stop just because there is a case label.
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
294
bool Collision(float player_x,float player_y, float asteroid_x, float asteroid_y, int width, int height, float projectile_x,float projectile_y, int projectile_width,int projectile_height)
{
	if(player_x + width < asteroid1_x || player_x > asteroid1_x + width || player_y + height < asteroid1_y || player_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid2_x || player_x > asteroid2_x + width || player_y + height < asteroid2_y || player_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid3_x || player_x > asteroid3_x + width || player_y + height < asteroid3_y || player_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid4_x || player_x > asteroid4_x + width || player_y + height < asteroid4_y || player_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(player_x + width < asteroid5_x || player_x > asteroid5_x + width || player_y + height < asteroid5_y || player_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		lives--;
		blitexplosion = true;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid1_x || projectile_x > asteroid1_x + width || projectile_y + height < asteroid1_y || projectile_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid2_x || projectile_x > asteroid2_x + width || projectile_y + height < asteroid2_y || projectile_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid3_x || projectile_x > asteroid3_x + width || projectile_y + height < asteroid3_y || projectile_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid4_x || projectile_x > asteroid4_x + width || projectile_y + height < asteroid4_y || projectile_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid5_x || projectile_x > asteroid5_x + width || projectile_y + height < asteroid5_y || projectile_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}	
	return NULL;
}

int main(void)
{
	allegro_init();
	install_keyboard();
	install_timer();
	install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED,ScreenWidth,ScreenHeight,0,0);
	spaceship=load_bitmap("ship.bmp",NULL);
	asteroid=load_bitmap("asteroid.bmp",NULL);
	projectile=load_bitmap("projectile.bmp",NULL);
	explosion=load_bitmap("explosion.bmp",NULL);
	background=load_bitmap("background.bmp",NULL);
	menubackground=load_bitmap("menubackground.bmp",NULL);
	menubackgroundwin=load_bitmap("menubackgroundwin.bmp",NULL);
	menubackgroundloss=load_bitmap("menubackgroundloss.bmp",NULL);
	menubackgroundinstructions=load_bitmap("menubackgroundinstructions.bmp",NULL);
	explosionSound=load_sample("explosion.wav");
	fire=load_sample("fire.wav");
	music=load_sample("music.wav");
	pongfont=load_font("pongfont.pcx",NULL,NULL);
	buffer=create_bitmap(SCREEN_W,SCREEN_H);
	menu = MAIN_MENU;
	spaceship_y=SCREEN_H /2;
	spaceship_x=50;
	asteroid1_x=590;
	asteroid2_x=590;
	asteroid3_x=590;
	asteroid4_x=590;
	asteroid5_x=590;
	asteroid1_y=40;
	asteroid2_y=140;
	asteroid3_y=240;
	asteroid4_y=340;
	asteroid5_y=440;
	score=0;
	lives=3;
	install_int(moveProjectile,5);
	install_int(respondToKeyboard,5);
	install_int(moveAsteroid,5);

	if(key[KEY_1])
	{
		menu=MAIN_MENU;
	}
	if(key[KEY_2])
	{
		menu=PLAY;
	}
	if(key[KEY_3])
	{
		menu=INSTRUCTIONS;
	}
	if (score==100)
	{
		menu=YOU_WIN;
	}
	if (lives==0)
	{
		menu=YOU_LOSE;
	}

	if (spaceship==0)
	{ 
		allegro_message("spaceship didn't load");
	}
	if(asteroid==0)
	{ 
		allegro_message("asteroid didn't load");
	}
	if(projectile==0)
	{ 
		allegro_message("projectile didn't load");
	}
	if(explosion==0)
	{ 
		allegro_message("explosion didn't load");
	}
	if(background==0)
	{ 
		allegro_message("background didn't load");
	}
	if(menubackground==0)
	{ 
		allegro_message("menubackground didn't load");
	}
	if(menubackgroundwin==0)
	{ 
		allegro_message("menubackgroundwin didn't load");
	}
	if(menubackgroundloss==0)
	{ 
		allegro_message("menubackgroundloss didn't load");
	}
	if(menubackgroundinstructions==0)
	{ 
		allegro_message("menubackgroundinstructions didn't load");
	}
	if(explosionSound==0)
	{ 
		allegro_message("explosionsound didn't load");
	}
	if(music==0)
	{ 
		allegro_message("music didn't load");
	}
	if(fire==0)
	{ 
		allegro_message("fire didn't load");
	}
	if(pongfont==0)
	{ 
		allegro_message("pongfont didn't load");
	}
	play_sample(music,150,120,1000,0);
	while(!key[KEY_ESC])
	{   
		switch (menu)
		{	
			case MAIN_MENU:
				blit(menubackground,buffer,0,0,0,0,SCREEN_W,SCREEN_H);
				blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
				clear_bitmap(buffer);
				break;
			case PLAY:
				blit(background,buffer,0,0,0,0,640,480);
				blit(asteroid,buffer,0,0,asteroid1_x,asteroid1_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid2_x,asteroid2_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid3_x,asteroid3_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid4_x,asteroid4_y,asteroid->w,asteroid->h);
				blit(asteroid,buffer,0,0,asteroid5_x,asteroid5_y,asteroid->w,asteroid->h);
				blit(spaceship,buffer,0,0,spaceship_x,spaceship_y,spaceship->w,spaceship->h);
				textprintf_ex(buffer,pongfont,75,0,makecol(0,0,0),-1,"lives %d",lives);
				textprintf_ex(buffer,pongfont,400,0,makecol(0,0,0),-1,"Score %d",score);
				line(buffer,0,30,640,30,makecol(0,0,0));
				blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
				clear_bitmap(buffer);
				break;
			case INSTRUCTIONS:
				blit(menubackgroundinstructions,buffer,0,0,0,0,640,480);
				blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
				clear_bitmap(buffer);
				break;
			case YOU_WIN:
				blit(menubackgroundwin,buffer,0,0,0,0,640,480);
				blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
				clear_bitmap(buffer);
				break;
			case YOU_LOSE:
				blit(menubackgroundloss,buffer,0,0,0,0,640,480);
				blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
				clear_bitmap(buffer);
				break;
		}
	}

	remove_int(moveAsteroid);
	remove_int(respondToKeyboard);
	destroy_bitmap(asteroid);
	destroy_bitmap(spaceship);
	destroy_bitmap(projectile);
	destroy_bitmap(background);
	destroy_bitmap(menubackground);
	destroy_bitmap(menubackgroundwin);
	destroy_bitmap(menubackgroundloss);
	destroy_bitmap(menubackgroundinstructions);
	destroy_bitmap(explosion);
	destroy_bitmap(buffer);
	destroy_sample(explosionSound);
	destroy_sample(fire);
	destroy_sample(music);
	return 0;
}
END_OF_MAIN()
Updated the code again, my menu loads now, but it won't switch to any of the other cases.
All the if statements you have before the loop will only run once, at the start of the program.
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
bool Collision(float spaceship_x,float spaceship_y,float asteroid_x,float asteroid_y,int width,int height)//controlls collision for spaceship, asteroid, and projectile
{
	if(spaceship_x + width < asteroid1_x || spaceship_x > asteroid1_x + width || spaceship_y + height < asteroid1_y || spaceship_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		allegro_message("collision!");
		lives--;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(spaceship_x + width < asteroid2_x || spaceship_x > asteroid2_x + width || spaceship_y + height < asteroid2_y || spaceship_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		allegro_message("collision!");
		lives--;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(spaceship_x + width < asteroid3_x || spaceship_x > asteroid3_x + width || spaceship_y + height < asteroid3_y || spaceship_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		allegro_message("collision!");
		lives--;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(spaceship_x + width < asteroid4_x || spaceship_x > asteroid4_x + width || spaceship_y + height < asteroid4_y || spaceship_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		allegro_message("menubackgroundwin didn't load");
		lives--;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(spaceship_x + width < asteroid5_x || spaceship_x > asteroid5_x + width || spaceship_y + height < asteroid5_y || spaceship_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		allegro_message("menubackgroundwin didn't load");
		lives--;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid1_x || projectile_x > asteroid1_x + width || projectile_y + height < asteroid1_y || projectile_y > asteroid1_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid2_x || projectile_x > asteroid2_x + width || projectile_y + height < asteroid2_y || projectile_y > asteroid2_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid3_x || projectile_x > asteroid3_x + width || projectile_y + height < asteroid3_y || projectile_y > asteroid3_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid4_x || projectile_x > asteroid4_x + width || projectile_y + height < asteroid4_y || projectile_y > asteroid4_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}
	if(projectile_x + width < asteroid5_x || projectile_x > asteroid5_x + width || projectile_y + height < asteroid5_y || projectile_y > asteroid5_y + height)
	{
		//no collision
	}
	else
	{
		score++;
		blitexplosion = true;
		blitprojectile = false;
		asteroid1_x=600;
		play_sample(explosionSound,255,128,1000,0);
	}	
	return NULL;
}



my last real issue, but this collision code doesn't do anything atm
Last edited on
Topic archived. No new replies allowed.