Jump to content
  • Advertisement
Sign in to follow this  

Making a bullet appear and be fired for a space shooter game

This topic is 3402 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

Hey I'm in the process of making a space shooter to show off to my friends :D but I've come over a problem, I cant fire a bullet it wont appear, and I'm sure if it appeared it wouldn't fire either >.< here's my code below: Feel free to help me because This is really slowing down the game :) #include <allegro.h> void awesomesauce(); int main() { allegro_init(); install_mouse(); install_keyboard(); install_timer(); set_color_depth(16); int width = 240, height = 160; set_gfx_mode(GFX_AUTODETECT,240,160,0,0); BITMAP *UFO = NULL; UFO = load_bitmap("UFO.bmp",NULL); BITMAP *galaxy = NULL; galaxy = load_bitmap("galaxy.bmp",NULL); BITMAP *buffer = NULL; buffer = create_bitmap(240,160); BITMAP *bullet = NULL; bullet = load_bitmap("bullet.bmp",NULL); int i; int ufo_x = 0; int ufo_y = 0; int bullet_y = ufo_y; int bullet_x = ufo_x; { for(;i < 50;i ++) bullet_y --; blit(screen,screen,0,0,0,0,640,480); // the awesomesauce function draw_sprite(bullet,screen,0,0); } while(!key[KEY_ESC]) { if(key[KEY_RIGHT]) { ufo_x ++; } if(key[KEY_LEFT]) { ufo_x --; } if(key[KEY_UP]) { ufo_y --; } if(key[KEY_DOWN]) { ufo_y ++; } if(key[KEY_SPACE]) { awesomesauce; } masked_blit(galaxy,buffer,0,0,0,0,240,160); draw_sprite(buffer,UFO,ufo_x,ufo_y); masked_blit(buffer,screen,0,0,0,0,240,160); clear_bitmap(buffer); } destroy_bitmap(UFO); destroy_bitmap(buffer); return 0; } END_OF_MAIN() Thanks for reading this post, and I'd really apreciate your guy's help in fixing this problem

Share this post


Link to post
Share on other sites
Advertisement
For the moment I am going to retain your programs simple structure. I will just mention for future reference that your code would benefit from making real data types.

First off I am going to clean it up a little. I have marked the areas I am going to change/remove in this next snippet.

(To include snippets like this, use [source][/source] around the code. You can use [code][/code] for very short snippets)

#include <allegro.h>

// this isn't implemented, so I am removing it
// void awesomesauce();

int main()
{
allegro_init();

install_mouse();
install_keyboard();
install_timer();

set_color_depth(16);

// I am going to mark these as constants.
// I am also going to use them anywhere the values 240 and 160 appeared
// Finally, I am going to give them better names.
int screen_width = 240, screen_height = 160;

set_gfx_mode(GFX_AUTODETECT,screen_width,screen_height,0,0);

BITMAP *UFO = load_bitmap("UFO.bmp",NULL);
BITMAP *galaxy = load_bitmap("galaxy.bmp",NULL);
BITMAP *bullet = load_bitmap("bullet.bmp",NULL);

BITMAP *buffer = create_bitmap(screen_width,screen_height);

// int i; This isn't needed

int ufo_x = 0;
int ufo_y = 0;
// I am going to initialise these directly to zero
int bullet_y = 0;
int bullet_x = 0;

// I am going to remove this code too
#if 0
{
// I just want to point out that it is wrong.
// You can see from my indentation here the way I believe you intended the code to work.
// However, because you didn't include curly braces on the for loop
// it will only execute the marked line repeatedly.
//
// Also, the co-ordinates 640x480 seem a little large for
// the window we defined earlier =)
for(;i < 50;i ++)
bullet_y --; // <-- This is inside the loop

// These lines are not included in the loop
blit(screen,screen,0,0,0,0,640,480);
draw_sprite(bullet,screen,0,0);
}
#endif

while(!key[KEY_ESC])
{
if(key[KEY_RIGHT])
{
ufo_x ++;
}

if(key[KEY_LEFT])
{
ufo_x --;
}

if(key[KEY_UP])
{
ufo_y --;
}

if(key[KEY_DOWN])
{
ufo_y ++;
}

if(key[KEY_SPACE])
{
// I assume space is the shoot key
// I am retaining this for future use
// awesomesauce;
}

masked_blit(galaxy,buffer,0,0,0,0,screen_width,screen_height);

draw_sprite(buffer,UFO,ufo_x,ufo_y);

masked_blit(buffer,screen,0,0,0,0,screen_width,screen_height);
clear_bitmap(buffer);
}

// Are we missing a few extra calls to destroy_bitmap?
destroy_bitmap(UFO);
destroy_bitmap(buffer);

return 0;
}
END_OF_MAIN()



I don't have allegro installed, but I assume your code works in this state. Lets add in the bullet:

#include <allegro.h>

int main()
{
allegro_init();

install_mouse();
install_keyboard();
install_timer();

set_color_depth(16);

const int screen_width = 240, screen_height = 160;

set_gfx_mode(GFX_AUTODETECT, screen_width, screen_height, 0, 0);

BITMAP *UFO = load_bitmap("UFO.bmp",NULL);
BITMAP *galaxy = load_bitmap("galaxy.bmp",NULL);
BITMAP *bullet = load_bitmap("bullet.bmp",NULL);

BITMAP *buffer = create_bitmap(240,160);

int ufo_x = 0;
int ufo_y = 0;
int bullet_y = 0;
int bullet_x = 0;

bool show_bullet = false;

while(!key[KEY_ESC])
{
if(key[KEY_RIGHT])
{
ufo_x ++;
}

if(key[KEY_LEFT])
{
ufo_x --;
}

if(key[KEY_UP])
{
ufo_y --;
}

if(key[KEY_DOWN])
{
ufo_y ++;
}

if(key[KEY_SPACE]) // If the player hits the space key
{
// First check that the bullet isn't already in use.
if(!show_bullet)
{
// If not, then set the bullet position to that of the UFO
// And tell it to be drawn.
show_bullet = true;
bullet_x = ufo_x;
bullet_y = ufo_y;
}
}

// every frame move the bullet towards the side of the screen
bullet_x++;

// if the bullet has moved beyond the screen, stop showing it.
if(bullet_x > screen_width)
{
show_bullet = false;
}

masked_blit(galaxy,buffer,0,0,0,0,screen_width,screen_height);

draw_sprite(buffer,UFO,ufo_x,ufo_y);

// Don't draw the bullet sprite unless the player
// has hit space!
if(show_bullet)
{
draw_sprite(buffer,bullet,bullet_x,bullet_y);
}

masked_blit(buffer,screen,0,0,0,0,screen_width,screen_height);
clear_bitmap(buffer);
}

// Don't forget those additional calls to destroy bitmap!
destroy_bitmap(UFO);
destroy_bitmap(buffer);

return 0;
}
END_OF_MAIN()


The main part was adding the drawing code (and a little code to move the bullet) to the main loop itself. Your code tried to draw the bullet before the main loop, but remember that image will only be shown for about 1 60th of a second and will be overdrawn by the draw calls in the main loop.

Now, this code hasn't been compiled or tested, but I imagine with a little work it will do what you want. It still isn't great code, it only handles a single bullet at a time. Maybe later we can start adding more structure to the game, allowing you to have a number of bullets on screen at once - when you have a better grasp of how the current code works. [smile]

Share this post


Link to post
Share on other sites
YES this is PERFECT it does exactly what i wanted it to do! I love you man! this has been getting at me for a bit and you fixed it! woo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!