Thread: trouble with my top-down shooter game...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    trouble with my top-down shooter game...

    I have a ship moving around the screen, and it works fine. The only thing is that now I'm trying to write the code to make it fire bullets. Here is what I THOUGHT would make it fire bullets, but I press the space bar and it does nothing. It compiles fine, but does nothing.
    Code:
    #include <allegro.h> // You must include the Allegro Header file
    #include "bullets.h"
    
    void increase_speed();
    void init_all();
    void clean_all();
    void clean_bitmap(BITMAP* page);
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP** active_page);
    void look_for_bullet(int x, int y, BULLET bullets[]);
    void update_bullets(BULLET bullets[]);
    void draw_bullets(BITMAP** active_page, BULLET bullets[]);
    
    volatile long speed_counter = 0;//A long integer which will store the value of the speed counter.
    
    int main(int argc, char *argv[])
    {
    	allegro_init(); // Initialize Allegro
    	install_keyboard(); // Initialize keyboard routines
    	set_color_depth(16); // Set the color depth						
    	set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480
    
    	install_timer();
    	LOCK_VARIABLE(speed_counter);
    	LOCK_FUNCTION(increase_speed);
    	install_int_ex(increase_speed, BPS_TO_TIMER(150));//Set our BPS
    
    	BITMAP *ship, *page1, *page2, *page3, *active_page, *background; 
    
    	page1      = create_video_bitmap(SCREEN_W, SCREEN_H);
        page2      = create_video_bitmap(SCREEN_W, SCREEN_H);
        page3      = create_video_bitmap(SCREEN_W, SCREEN_H);
    	background = load_bitmap("lee.bmp", NULL);
        ship       = load_bitmap("ship.bmp", NULL); // Load our picture
    
    	BULLET bullets[20];
    	for(int i = 1; i<=15; i++)
    	{
    		bullets[i].bullet = load_bitmap("bullet.bmp", NULL);
    		bullets[i].occupied = false;
    		bullets[i].x = 0;
    		bullets[i].y = 0;
    	}
    
        clean_bitmap(page1);
        clean_bitmap(page2);
    	clean_bitmap(page3);
    
        active_page    = page1;
    
    	int x          = 0;
    	int y          = 0;
    	int wPage      = 1; //what page are we on?
    	bool validness = true;
    
    /*///////////////////////////////////////////////////////////////////////////////////*/
    /*////// Name: Main Loop ////////////////////////////////////////////////////////////*/
    /*///////////////////////////////////////////////////////////////////////////////////*/
    
    	while(!key[KEY_ESC])
    	{
    		while(speed_counter > 0)
    		{
    		    validness = true;
    
    			if(key[KEY_UP] && key[KEY_RIGHT])
    			{
    				y--;
    				x++;
    				if(y < 0)
    				{
    					y++;
    				}
    				if(x > 604)
    				{
    					x--;
    				}
    			}
    			else if(key[KEY_UP] && key[KEY_LEFT])
    			{
    				y--;// Moving down, so up the Y coordinate by 1
    				x--;
    			    if(y < 0)
    			    {
    				    y++;
    			    }
    				if(x < 0)
    				{
    					x++;
    				}
    			}
    			else if(key[KEY_DOWN] && key[KEY_LEFT])
    			{
    				y++;// Moving down, so up the Y coordinate by 1
    				x--;
    			    if(y > 440)
    			    {
    				    y--;
    			    }
    				if(x < 0)
    				{
    					x++;
    				}
    			}
    			else if(key[KEY_DOWN] && key[KEY_RIGHT])
    			{
    				y++;// Moving down, so up the Y coordinate by 1
    				x++;
    			    if(y > 440)
    			    {
    				    y--;
    			    }
    				if(x > 604)
    				{
    					x--;
    				}
    			}
    		    else if(key[KEY_RIGHT])
    		    {
    			    x++;// Moving right so up the X coordinate by 1
    			    if(x > 604)
    		    	{
    		    		validness = false;
    			    	x--;
    			    }
    		    }
    		    else if(key[KEY_LEFT])// Ditto' - only for left key
    		    {
    			    x--;// Moving left, so lower the X coordinate by 1
    			    if(x < 0)
    			    {
    				    validness = false;
    				    x++;
    			    }
    		    }
    		    else if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
    		    {
    			    y--;// Moving up, so lower the Y coordinate by 1
    			    if(y < 0)
    			    {
    				    validness = false;
    				    y++;
    			    }
    		    }
    		    else if(key[KEY_DOWN])// Ditto' - only for down
    		    {
    			    y++;// Moving down, so up the Y coordinate by 1
    			    if(y > 440)
    			    {
    				    validness = false;
    				    y--;
    			    }
    		    }
    			if(key[KEY_SPACE])
    			{
    				look_for_bullet(x, y, bullets);
    			}
    
    			speed_counter--;
    		};
    	
    		if(validness == true)
    		{
    		    acquire_screen();// Get the screen
    
    		    masked_blit(ship, active_page, 0,0, x, y,640,480);
    			//for(int a=1;a<=15;a++)
    			//{
    			    //masked_blit(bullets[a].bullet, active_page, 0,0, bullets[a].x, bullets[a].y, 640,480);
    			//}
    			draw_bullets(&active_page, bullets);
    
                show_video_bitmap(active_page);
    
    			update_bullets(bullets);
    
    			flip_page(&wPage, &page1, &page2, &page3, &active_page);
    
    			clean_bitmap(page1);
    			clean_bitmap(page2);
    			clean_bitmap(page3);
    
    		    release_screen();// Release it
    		}
    	};
    
    
    	destroy_bitmap(ship);//Release the bitmap data
    	destroy_bitmap(background);
    	destroy_bitmap(page1);
    	destroy_bitmap(page2);
    	destroy_bitmap(page3);
    
    	for(int d=1;d<=15;d++)
    	{
    		destroy_bitmap(bullets[i].bullet);
    	}
    
    	return(0);// Exit with no errors
    }
    END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function.
                   // It is Allegro specific.
    void increase_speed()
    {
    	speed_counter++;
    }
    END_OF_FUNCTION(increase_speed);
    
    void clean_bitmap(BITMAP* page)
    {
    	acquire_bitmap(page);
        clear_bitmap(page);
        release_bitmap(page);
    }
    END_OF_FUNCTION(clear_bitmap);
    
    void flip_page(int *wPage, BITMAP** page1, BITMAP** page2, BITMAP** page3, BITMAP** active_page)
    {
    	if(*active_page == *page1)
    	{
            *active_page = *page2;
    		*wPage       = 2;
    	}
        if(*active_page == *page2)
    	{
            *active_page = *page3;
    		*wPage       = 3;
    	}
    	if(*active_page == *page3)
    	{
    		*active_page = *page1;
    		*wPage       = 1;
    	}
    }
    END_OF_FUNCTION(flip_page);
    
    void look_for_bullet(int x, int y, BULLET bullets[])
    {
    	bool stop = false;
    	for(int i=1; i<=15; i++) //Look for bullet to use
    	{
    		if(stop = true)
    			continue;
    		if(bullets[i].occupied == false)
    			stop = true;
    	}
    	bullets[i].x = x;
    	bullets[i].y = y;
        bullets[i].occupied = true;
    }
    END_OF_FUNCTION(look_for_bullet);
    
    void update_bullets(BULLET bullets[])
    {
    	for(int i=1; i<=15; i++)
    	{
    		if(bullets[i].occupied == true)
    		{
    			if(bullets[i].y == 0)           //if its at edge of screen
    				bullets[i].occupied = false;
    			if(bullets[i].occupied == true)
    				bullets[i].y--;
    		}
    	
    	}
    }
    END_OF_FUNCTION(update_bullets);
    
    void draw_bullets(BITMAP** active_page, BULLET bullets[])
    {
    	for(int i=1; i<=15; i++)
    	{
    		if(bullets[i].occupied == true)
    		{
    		    masked_blit(bullets[i].bullet, *active_page, 0,0, bullets[i].x, bullets[i].y, 640,480);
    		}	
    	}
    }
    END_OF_FUNCTION(draw_bullets);

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void look_for_bullet(int x, int y, BULLET bullets[])
    >{
    >	bool stop = false;
    >	for(int i=1; i<=15; i++) //Look for bullet to use
    >	{
    >		if(stop = true)
    Try:
    		if(stop == true)
    
    >			continue;
    >		if(bullets[i].occupied == false)
    >			stop = true;
    >	}
    >	bullets[i].x = x;
    >	bullets[i].y = y;
    >    bullets[i].occupied = true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 11:03 AM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM