Sign in to follow this  

Bullets Not Updating

This topic is 2052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[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[LEFT])
moveShipLeft(ship);
if (keys[RIGHT])
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[LEFT] = true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 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[LEFT] = false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = 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[i].ID = BULLET;
bullet[i].speed = 10;
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, 255, 255));
}
}
void fireBullet(Bullet bullet[], int size, Spaceship &ship)
{
for (int i = 0; i < size; i++)
{
bullet[i].x = ship.x + 17;
bullet[i].y = ship.y;
bullet[i].live = true;
break;
}
}
void updateBullet(Bullet bullet[], int size)
{
for (int i = 0; i < size; i++)
{
if(bullet[i].live)
{
bullet[i].x += bullet[i].speed;
if(bullet[i].x > WIDTH)
bullet[i].live = false;
}
}
}
[/CODE]

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

Share this post


Link to post
Share on other sites
[quote name='robbiewoods05' timestamp='1336087028' post='4937250']
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.
[/quote]

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

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. ;) Edited by jefferytitan

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 2052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this