Thread: Vertical Scroller laser cannon problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Vertical Scroller laser cannon problem

    I'm making an allegro vertical scroller at the moment, its going well and ive made alot of progress over the last day or 2. anyway, i've got a bit of a problem with the lasers. I'm in the process of adding weapons to the player, and the way I do it is to keep a record of the locations of all the laser beams on the screen in an array, then when it comes to refreshing, all the positions are known and u just cycle through them one at a time using a for loop. But, when a laser hits the top of the window, i needed a way of getting it to free up the memory slot in the array (i know that its not great practice for something like this, but the array is a fixed size and can take 100 coordinates at a time - given the rate they move off the screen, i thought 100 sounded enough) so that I could use it again for later shots.

    The way I decided 2 do it was: everytime the main game loop executes, i would count the number of beams which had left the screen and then move the rest of the coordinates along by that many spaces.

    e.g. Imagine the coordinates as:
    0, 1, 2, 3, 4, ...

    representing different beams, then suppose that i count up how many have left and find there are 2, then i would move everything along 2, so 2->0; 3->1 etc. Then when it comes 2 displaying them, i just count up 1, 2, 3 etc upto the number of rounds shot. (I know that this method should work, cos all the beams travel at the same speed and the player can't go faster than the beams, so the beams must leave in the order they were shot).

    That was the idea anyway, although for some reason, its turned out that when i try shooting in the game, it more i shoot, the less it lets me shoot, im not entirely sure why, but here is some of my code:

    Code:
    /* This is the whole 'shift everything along by an offset' thing */
    for(idx1=0; idx1<shot_counter; idx1++)
                                   {
                                               if( cweapons.GetLaserPosition(1, idx1) < 0 )
                                               {
                                                   offset++;
                                               }
                                   }
                                   
                                   for(idx1=0; idx1<shot_counter; idx1++)
                                   {
                                               if( cweapons.GetLaserPosition(1, idx1) < 0 )
                                               {
                                                   for(idx2=(idx1+offset); idx2 < shot_counter; idx2++)
                                                   {
                                                                        posx = (int)cweapons.GetLaserPosition(0, idx2);
                                                                        posy = (int)cweapons.GetLaserPosition(1, idx2);
                                                                        
                                                                        cweapons.SetLaserPosition(0, idx2 - offset, posx);
                                                                        cweapons.SetLaserPosition(1, idx2 - offset, posy);
                                                   }
                                                   
                                                   shot_counter -= offset;
                                               }
                                                   cweapons.DecrementLaserPosition(1, idx1);
                                   }
    This following part is the way the shot is dealt with when the space button is pressed (dont worry about things like rof_index, thats just a way of regulating the rate of fire).

    Code:
    if( (key[KEY_SPACE])&&(shot_counter < MAX_LASER_NUM)&&(rof_index > 1) )
                                   {
                                       /* Most of this isn't really related to the internals of the laser shots, more just getting the laser shots started */
                                       posx = (int)cphysics.GetComPosition(0, 0, 1);
                                       posy = (int)cphysics.GetComPosition(1, 0, 1);
                                       
                                       /* Adjust posx so that shot comes out of middle of ship */
                                       posx = posx + (cgraphics.p_player_ship->w/2);
                                       
                                       if(laser_cannon == false)
                                       {
                                                       posx = posx + (int)(0.60*cgraphics.p_player_ship->w/2);
                                                       laser_cannon = true;
                                       }
                                       else
                                       {
                                           posx = posx - (int)(0.61*cgraphics.p_player_ship->w/2);
                                           laser_cannon = false;
                                       }
                                       
                                       cweapons.SetLaserPosition(0, shot_counter, posx);
                                       cweapons.SetLaserPosition(1, shot_counter, posy);
                                       
                                       /* Reset the rate of fire index */
                                       rof_index = 0;
                                       
                                       shot_counter++;
                                   }
    And this is how I'm rendering it to the screen:
    Code:
    for(idx1=0; idx1 < shot_counter; idx1++)
                                   {
                                               /* Add the laser shots to the screen buffer */
                                               if( cweapons.GetLaserPosition(1, idx1) > 0 )
                                               {
                                                   masked_blit(cgraphics.p_laser, cgraphics.p_screen_buffer1,0,0,(int)cweapons.GetLaserPosition(0,idx1),
                                                                        (int)cweapons.GetLaserPosition(1,idx1), cgraphics.p_laser->w, cgraphics.p_laser->h);
                                               }
                                   }
    Btw, I almost forgot to mention, in all the function calls etc, the index 0 corresponds to x and 1 corresponds to y (im sure you guys would have been able to figure that one out for yourselves, but I thought Id make things a little easier :P), lol. All the laser positions are stored in an array called 'm_laser_position[2][100]'
    Last edited by Swarvy; 05-01-2009 at 06:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. problem with laser gun!
    By actionbasti in forum Game Programming
    Replies: 3
    Last Post: 11-24-2003, 12:56 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM