Bullets Not Updating

Started by
4 comments, last by BeerNutts 11 years, 11 months ago
Hi guys, I'm making a sidescrolling 2D shooter and I'm trying to get my bullets to update and move across the screen. Before I implemented my updateBullet function into my main game loop, the bullets popped up on the screen and stayed still as I expected them to. However, now, that doesn't even happen.

Here's my code:


#include <allegro5.h>
#include <allegro_native_dialog.h>
#include <allegro_primitives.h>
#include "objects.h"

// *** GLOBALS ***
const int WIDTH = 800, HEIGHT = 400, FPS = 60, NUM_BULLETS = 5;
enum KEYS { UP, DOWN, LEFT, RIGHT, SPACE };
bool keys[5] = { false, false, false, false, false };

// *** PROTOTYPES ***
void initShip(Spaceship & ship);
void drawShip(Spaceship & ship);
void moveShipUp(Spaceship & ship);
void moveShipDown(Spaceship & ship);
void moveShipLeft(Spaceship & ship);
void moveShipRight(Spaceship & ship);
void initBullet(Bullet bullet[], int size);
void drawBullet(Bullet bullet[], int size);
void fireBullet(Bullet bullet[], int size, Spaceship &ship);
void updateBullet(Bullet bullet[], int size);

int main(void)
{
// Init Allegro
if (!al_init()) {
al_show_native_message_box(NULL, "Error", "Error", "Failed to initialise allegro!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
// Primitive Variables
bool done = false, redraw = true;
// Object Variables
Spaceship ship;
Bullet bullets[5];
// Allegro Variables
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
display = al_create_display(WIDTH, HEIGHT);
if (!display) {
al_show_native_message_box(NULL, "Error", "Error", "Failed to create display!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
al_install_keyboard();
al_init_primitives_addon();
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
initShip(ship);
initBullet(bullets, NUM_BULLETS);
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_start_timer(timer);
while (!done) {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
if (keys[UP])
moveShipUp(ship);
if (keys[DOWN])
moveShipDown(ship);
if (keys

)
moveShipLeft(ship);
if (keys

)
moveShipRight(ship);
updateBullet(bullets, NUM_BULLETS);
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
done = true;
} else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
keys

= true;
break;
case ALLEGRO_KEY_RIGHT:
keys

= true;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = true;
fireBullet(bullets, NUM_BULLETS, ship);
break;
}
} else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
keys

= false;
break;
case ALLEGRO_KEY_RIGHT:
keys

= false;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
break;
}
}
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
drawShip(ship);
drawBullet(bullets, NUM_BULLETS);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
al_destroy_display(display);
return 0;
}
void initShip(Spaceship & ship)
{
ship.x = 20;
ship.y = HEIGHT / 2;
ship.ID = PLAYER;
ship.lives = 3;
ship.speed = 7;
ship.boundsX = 6;
ship.boundsY = 7;
ship.score = 0;
}
void drawShip(Spaceship & ship)
{
al_draw_filled_rectangle(ship.x, ship.y - 9, ship.x + 10, ship.y - 7, al_map_rgb(255, 0, 0));
al_draw_filled_rectangle(ship.x, ship.y + 9, ship.x + 10, ship.y + 7, al_map_rgb(255, 0, 0));
al_draw_filled_triangle(ship.x - 12, ship.y - 17, ship.x + 12, ship.y, ship.x - 12, ship.y + 17, al_map_rgb(0, 255, 0));
al_draw_filled_rectangle(ship.x - 12, ship.y - 2, ship.x + 15, ship.y + 2, al_map_rgb(0, 0, 255));
}
void moveShipUp(Spaceship & ship)
{
ship.y -= ship.speed;
if (ship.y < 0)
ship.y = 0;
}
void moveShipDown(Spaceship & ship)
{
ship.y += ship.speed;
if (ship.y > HEIGHT)
ship.y = HEIGHT;
}
void moveShipLeft(Spaceship & ship)
{
ship.x -= ship.speed;
if (ship.x < 0)
ship.x = 0;
}
void moveShipRight(Spaceship & ship)
{
ship.x += ship.speed;
if (ship.x > 300)
ship.x = 300;
}
void initBullet(Bullet bullet[], int size)
{
for (int i = 0; i < 0; i++)
{
bullet.ID = BULLET;
bullet.speed = 10;
bullet.live = false;
}
}
void drawBullet(Bullet bullet[], int size)
{
for (int i = 0; i < size; i++)
{
if(bullet.live)
al_draw_filled_circle(bullet.x, bullet.y, 2, al_map_rgb(255, 255, 255));
}
}
void fireBullet(Bullet bullet[], int size, Spaceship &ship)
{
for (int i = 0; i < size; i++)
{
bullet.x = ship.x + 17;
bullet.y = ship.y;
bullet.live = true;
break;
}
}
void updateBullet(Bullet bullet[], int size)
{
for (int i = 0; i < size; i++)
{
if(bullet.live)
{
bullet.x += bullet.speed;
if(bullet.x > WIDTH)
bullet.live = false;
}
}
}



If anyone could tell me why this is happening that'd be greatly appreciated.
Advertisement

Before I implemented my updateBullet function into my main game loop, the bullets popped up on the screen and stayed still as I expected them to. However, now, that doesn't even happen.


When you bring these things to a forum, think like you're in QA. You need to describe the problem both well and succinctly. Don't tell us what doesn't happen, tell us what DOES happen. Does the bullet never appear? Appear briefly then vanish? Go backwards? Appear at random locations on screen? Does the game crash?

Maybe the bullet speed is too big... or negative. Maybe something is decreasing WIDTH so the bullet becomes not-live when it should be live. To me the bullet code looks poorly thought out. Look at the below:


[color=#000088]void[color=#000000] fireBullet[color=#666600]([color=#660066]Bullet[color=#000000] bullet[color=#666600][],[color=#000000] [color=#000088]int[color=#000000] size[color=#666600],[color=#000000] [color=#660066]Spaceship[color=#000000] [color=#666600]&[color=#000000]ship[color=#666600])
[color=#666600]{
[color=#000088]for[color=#000000] [color=#666600]([color=#000088]int[color=#000000] i [color=#666600]=[color=#000000] [color=#006666]0[color=#666600];[color=#000000] i [color=#666600]<[color=#000000] size[color=#666600];[color=#000000] i[color=#666600]++)
[color=#666600]{
[color=#000000] bullet[color=#666600][[color=#000000]i[color=#666600]].[color=#000000]x [color=#666600]=[color=#000000] ship[color=#666600].[color=#000000]x [color=#666600]+[color=#000000] [color=#006666]17[color=#666600];
[color=#000000] bullet[color=#666600][[color=#000000]i[color=#666600]].[color=#000000]y [color=#666600]=[color=#000000] ship[color=#666600].[color=#000000]y[color=#666600];
[color=#000000] bullet[color=#666600][[color=#000000]i[color=#666600]].[color=#000000]live [color=#666600]=[color=#000000] [color=#000088]true[color=#666600];
[color=#000000] [color=#000088]break[color=#666600];
[color=#666600]}
[color=#666600]}

Doesn't the break statement mean the loop will only run once, always firing bullet 0? Also all your code seems to refer to NUM_BULLETS. Shouldn't you have a variable for the MAXIMUM number of bullets and another for the ACTUAL number of bullets? So actual bullets would increase until it reached MAXIMUM bullets and then they wouldn't be able to fire any more until the bullets hit something or go offscreen?

EDIT: Apologies if this sounded too harsh. Sometimes I forget this is the Beginners section. ;)
Assuming you don't move the ship from the starting position, the bullet would be at 37 imediately upon firing, be at 637 within one second, and off the screen in 363/600 of a second. My guess is it's moving too fast.


I have to ask... why don't you put your prototype's into their classes? ex: ship.moveUp() or bullts.update()
Also your initBullet function is going to do exactly nothing right now.
f@dzhttp://festini.device-zero.de

Also your initBullet function is going to do exactly nothing right now.


Nice catch, i<0.
A few suggestions:
#1, you need to add some debugging to your game. I would suggest, if you have a console open while you play, you just printf() values to the console while you run, and it will help you understand what's happening, especially with the bullets. You could also log it to file.

#2, You need to modify how you handle firing bullets. Everytime you hit space, you're putting 5 bullets at the same spot in front of the space ship. You need to:
a: add a refire speed, so you only fire bullets every x milliseconds
b: In firebullet, don't do anything to a bullet that's marked live.

Good Luck

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement