Allegro program screwing up

I am following a tutorial for game making with allegro. This code is meant to fire off a bullet when i click space. I havent added an update function yet, so the bullet isnt supposed to move. The problem is that the bullet goes off properly once, then if i move and fire again, the old bullet disappears (its not supposed to!) and a new one appears at my new x coordinate but at the old y coordinate. it keeps doing this with the first y coordinate. i have read this code through many times and cant find the problem. can you guys help?

The code: (the header file is down below)

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
  #include <allegro5\allegro.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_primitives.h>
#include "object.h"


//globals

const int height = 400;
const int width = 800;
enum keys {UP, DOWN, LEFT, RIGHT, SPACE};
bool keys[5] = {false, false, false, false, false};

//prototypes

void initship(spaceship &ship); //these
void drawship(spaceship &ship); //are
void moveshipup(spaceship &ship); //for
void moveshipdown(spaceship &ship); //our
void moveshipleft(spaceship &ship); //space
void moveshipright(spaceship &ship); //ship


void initbullet(bullet bullets[5], int size); //these
void drawbullet(bullet bullets[5], int size); //are
void firebullet(bullet bullets[5],int size,spaceship &ship); //for
void updatebullet(bullet bullets[5],int size); //bullets

//variables
bool done = false;
int FPS = 60;
int numberofbullets = 5; //how many you can have on screen

bool redraw = false;

//object variables
spaceship ship;
bullet bullets[5];

int main ()
{
	//inits start here
	ALLEGRO_DISPLAY *display =NULL;
	ALLEGRO_EVENT_QUEUE *eventqueue =NULL;

	if (!al_init())
	{
		al_show_native_message_box (NULL, NULL, NULL,
			"FAILED TO INITIALIZE ALLEGRO", NULL, NULL);
		
	}


	display = al_create_display (width, height);

	if (!display)
	{
		al_show_native_message_box (NULL, NULL, NULL,
			"FAILED TO INITIALIZE display", NULL, NULL);

	}

	al_init_font_addon();
	al_init_ttf_addon();
	ALLEGRO_FONT *font24 = al_load_font ("arial.ttf", 24, 0);
	ALLEGRO_FONT *font36 = al_load_font ("arial.ttf", 36, 0);
	ALLEGRO_FONT *font18 = al_load_font ("arial.ttf", 18, 0);

	al_init_primitives_addon();
	al_install_keyboard();

	eventqueue = al_create_event_queue();

	al_register_event_source(eventqueue, al_get_keyboard_event_source());
	al_register_event_source(eventqueue, al_get_display_event_source(display));

		ALLEGRO_TIMER *timer = NULL;
	timer = al_create_timer(1.0/FPS);
	al_register_event_source(eventqueue, al_get_timer_event_source(timer));

	initship (ship);
	initbullet(bullets,numberofbullets);

	//timer
	al_start_timer(timer);

	//inits end here


	while (!done)
	{
		
		ALLEGRO_EVENT ev;
		al_wait_for_event (eventqueue, &ev);

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				firebullet(bullets, numberofbullets,ship);
				break;
			}
		}

		else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
				done = true;
		}
		else if (ev.type == ALLEGRO_EVENT_KEY_UP)
		{			
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			case ALLEGRO_KEY_ESCAPE:
				done =true;

			}
		}
		else if (ALLEGRO_EVENT_TIMER)
		{
			if (keys[UP] == true)
				moveshipup(ship);
			if (keys[DOWN] == true)
				moveshipdown(ship);
			if (keys[LEFT] == true)
				moveshipleft(ship);
			if (keys[RIGHT] == true)
				moveshipright(ship);
			redraw = true;
		}

		if (redraw && al_is_event_queue_empty(eventqueue))
		{
			redraw = false;
			drawship(ship);
			drawbullet(bullets, numberofbullets);
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
		
		
	}

	al_destroy_display(display);

	return 0;
}


//***********************************************************
//***********************************************************
//***********************************************************
//*****FUNCTIONS*FUNCTIONS*FUNCTIONS*FUNCTIONS*FUNCTIONS*****
//***********************************************************
//***********************************************************
//***********************************************************


//ship stuff start
void initship(spaceship &ship)
{
	ship.x=40;
	ship.y=height/2;
	ship.ID=PLAYER;
	ship.lives=5;
	ship.speed=5;
	ship.boundx=6;
	ship.boundy=7;
	ship.score=0;
}

void drawship(spaceship &ship)
{
	al_draw_filled_rectangle(ship.x,ship.y,ship.x-30,ship.y-15, al_map_rgb(0,255,0));
	al_draw_filled_triangle(ship.x,ship.y,ship.x,ship.y-15,ship.x+10,ship.y-8, al_map_rgb(0,0,255));
}

void moveshipup(spaceship &ship)
{
	ship.y -= ship.speed;
	if (ship.y < 17)
		ship.y = 17;
}

void moveshipdown(spaceship &ship)
{
	ship.y += ship.speed;
	if (ship.y > height - 2)
		ship.y = height - 2;
}

void moveshipleft(spaceship &ship)
{
	ship.x -= ship.speed;
	if (ship.x < 32)
		ship.x = 32;
}

void moveshipright(spaceship &ship)
{
	ship.x += ship.speed;
	if (ship.x > 200)
		ship.x = 200;
}
//ship stuff end

void initbullet(bullet bullet[], int size) 
{
	for(int i = 0; i < size; i++)
	{
		bullet[i].ID = BULLET;
		bullet[i].speed = 0.2;
		bullet[i].live = false;
	}
}
void drawbullet(bullet bullet[], int size)
{
	for(int i = 0; i < size; i++)
	{
		if (bullet[i].live)
		al_draw_filled_circle(bullet[i].x,bullet[i].y, 2, al_map_rgb(255,0,0));
	}
}

void firebullet(bullet bullet[],int size, spaceship &ship) 
{
	for(int i = 0; i < size; i++)
	{
		if (!bullet[i].live)
		bullet[i].x = ship.x + 14;
		bullet[i].y = ship.y-8;
		bullet[i].live = true;
		break;
	}
}


and the header file:

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
#ifndef OBJECT_H
#define OBJECT_H

enum IDS {PLAYER, BULLET, ENEMY};


// our player
struct spaceship
{
	int ID;
	int x;
	int y;
	int lives;
	int speed;
	int boundx;
	int boundy;
	int score;
};

struct bullet
{
	int ID;
	int x;
	int y;
	bool live;
	float speed;
};

#endif 


thanks in advance!
nevermind i found the problem i was missing a pair of brackets
Topic archived. No new replies allowed.