Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Bullets Not Updating


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Shenaynay   Members   -  Reputation: 134

Like
0Likes
Like

Posted 03 May 2012 - 05:17 PM

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[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;
  }
}
}

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

Sponsor:

#2 jefferytitan   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 03 May 2012 - 06:42 PM

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:


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;
}
}

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, 03 May 2012 - 06:43 PM.


#3 slynk   Members   -  Reputation: 144

Like
0Likes
Like

Posted 03 May 2012 - 08:41 PM

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()

#4 Trienco   Members   -  Reputation: 1302

Like
1Likes
Like

Posted 03 May 2012 - 10:27 PM

Also your initBullet function is going to do exactly nothing right now.
f@dzhttp://festini.device-zero.de

#5 jefferytitan   Members   -  Reputation: 1006

Like
0Likes
Like

Posted 03 May 2012 - 10:51 PM

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


Nice catch, i<0.

#6 BeerNutts   Members   -  Reputation: 1565

Like
0Likes
Like

Posted 04 May 2012 - 11:39 AM

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)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS