Having some real trouble with this code for a game I'm working on.

This is a discussion on Having some real trouble with this code for a game I'm working on. within the Game Programming forums, part of the General Programming Boards category; I'm trying to make a vertical scrolling shooter, and ive made quite alot of progress, I've got the lasers to ...

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

    Having some real trouble with this code for a game I'm working on.

    I'm trying to make a vertical scrolling shooter, and ive made quite alot of progress, I've got the lasers to work alright and the background to scroll properly. Although I'm having real trouble getting aliens to work properly.

    Problems I'm having:
    1) Getting the aliens to move properly - They move onto the screen, scrolling with the background and then just get to a point where they don't move.
    2) Getting the lasers to damage the alien ship properly - The lasers do damage to the ship before they reach it. It looks a little weird when the ship blows up when u shoot it and it blows up before the shot reaches it.
    3) Sometimes when you shoot lasers at the alien ship, it doesnt register the damage no matter how many times you hit it
    4) If a ship does come onto the screen, another one isnt made - I cant get it to form a loop. Eventually I want to turn it into a series of levels.

    Anyway, I havent started on anything to do with the aliens shooting back just yet. As with the lasers from my previous posts, I'm using linked lists for the alien ships.

    Theres the code:
    Code:
    // This is the declaration of alien_root etc at the beginning of int main.
    /* Set up the Alien Structure */
        AlienStruct *alien_root=NULL;
        AlienStruct *current_alien = NULL;
        AlienStruct *previous_alien = NULL;
        AlienStruct *temp_alien = NULL;
        
        alien_root = new AlienStruct;
        alien_root->next_alien = NULL;
        alien_root->alien_type = 1;
    
    // Inside the main game loop - This is the main alien logic sequence.
    // I broke the main game loop into logic and graphics to make things more organised.
    /* Alien Movements */
                                   
                                   /* Generate New Aliens if too few on screen */
                                   // current_alien = alien_root;
                                   
                                   if( alien_counter < 1 )
                                   {
                                       caliens.AddNewAlien( rand()%SCREEN_DIMX, -(cgraphics.GetAlienBitmapDimension(1, 1)), 1, alien_root);
                                       alien_counter++;
                                   }
                                   
                                   /* Check if Aliens have been destroyed */
                                   current_alien = alien_root;
                                   
                                   while( current_alien->next_alien != NULL )
                                   {
                                          posx = cgraphics.GetAlienBitmapDimension(current_alien->alien_type, 0);
                                          posy = cgraphics.GetAlienBitmapDimension(current_alien->alien_type, 0);
                                          
                                          /* Test if alien has been hit by laser */
                                          while( current_node->next_laser != NULL )
                                          {
                                                 if( ((current_node->laser_position[0] > current_alien->alien_position[0])&&
                                                     (current_node->laser_position[0] < current_alien->alien_position[0] + posx))&&
                                                     ((current_node->laser_position[1] < current_alien->alien_position[1])&&
                                                     (current_node->laser_position[1] > current_alien->alien_position[1] + posy)) )
                                                 {
                                                                                      /* Take Damage of Laser away from Alien number of hit points */
                                                                                      current_alien->alien_hitpoints -= LASER_DAMAGE;
                                                                                      allegro_message("Damage");
                                                                                      
                                                                                      if( current_alien->alien_hitpoints <= 0 )
                                                                                      {
                                                                                          /* 'Tag' Alien Ship so that it will be exploded in the graphical section of the code */
                                                                                          current_alien->alien_destroy = true;
                                                                                      }
                                                                                      
                                                                                      /* Delete laser beams which hit the aliens */
                                                 }
                                                 
                                                 current_node = current_node->next_laser;
                                          }
                                          
                                          /* Test if alien has been hit by rocket launcher */
                                          
                                          current_alien = current_alien->next_alien;
                                   }
                                   
                                   /* Change the positions of the Aliens */
                                   current_alien = alien_root;
                                   
                                   if( acounter > 50 )
                                   {
                                       while( current_alien->next_alien != NULL )
                                       {
                                              current_alien->alien_position[0] = current_alien->alien_position[0] + current_alien->alien_velocity[0];
                                              current_alien->alien_position[1] = current_alien->alien_position[1] + current_alien->alien_velocity[1];
                                              
                                              current_alien = current_alien->next_alien;
                                              
                                              acounter = 0;
                                       }
                                   }
                                   
                                   /* Take into account the scrolling of the background */
                                   caliens.IncrementYRemoveAlienPosition(alien_root);
                                   
                                   /* Change the velocities of the Aliens */
                                   current_alien = alien_root;
                                   
                                   while( current_alien->next_alien != NULL )
                                   {
                                          caliens.ChangeAlienVelocity(current_alien);
                                          
                                          current_alien = current_alien->next_alien;
                                   }
    This next lot of code is from the header file "aliens.h" - It has all the methods called above.
    Code:
    void CGameAliens::AddNewAlien( int position_x, int position_y, unsigned int type, AlienStruct *alien_root)
    {
         AlienStruct *current_node=NULL;
         
         current_node = alien_root;
         
         /* Save the place in the linked list */
         while(current_node->next_alien != 0)
         {
                                        current_node = current_node->next_alien;
         }
         
         current_node->next_alien = new AlienStruct;
         
         /* Set the position of the alien */
         current_node->alien_position[0] = position_x;
         current_node->alien_position[1] = position_y;
         current_node->alien_type = type;
         current_node->alien_destroy = false;
         
         switch(type)
         {
                     case 1:
                          current_node->alien_hitpoints = ALIEN_TYPE_I_LIFE;
                          break;
                     case 2:
                          current_node->alien_hitpoints = ALIEN_TYPE_II_LIFE;
                          break;
                     case 3:
                          current_node->alien_hitpoints = ALIEN_TYPE_III_LIFE;
                          break;
                     case 4:
                          current_node->alien_hitpoints = ALIEN_TYPE_IV_LIFE;
                          break;
                     case 5:
                          current_node->alien_hitpoints = ALIEN_TYPE_V_LIFE;
                          break;
         }
         
         current_node = current_node->next_alien;
         current_node->next_alien = NULL;
         
         return;
    }
    
    void CGameAliens::DeleteAllAliens(AlienStruct *alien_root)
    {
         AlienStruct *current_node1;
         AlienStruct *current_node2;
         
         current_node1 = alien_root->next_alien;
         
         while(current_node1->next_alien != NULL)
         {
                                        current_node2 = current_node1->next_alien;
                                        delete current_node1;
                                        current_node1 = current_node2;
         }
         
         delete current_node1;
         
         delete alien_root;
         
         return;
    }
    
    void CGameAliens::IncrementYRemoveAlienPosition(AlienStruct *alien_root)
    {
         /* This Method increases Y position and removes any old Aliens */
         AlienStruct *current_node=NULL;
         AlienStruct *previous_node=NULL;
         
         current_node = alien_root;
         
         while(current_node->next_alien != NULL)
         {
                                        current_node->alien_position[1] = (unsigned int)(current_node->alien_position[1] + SCROLL_RATE);
                                        
                                        current_node = current_node->next_alien;
         }
         
         current_node = alien_root;
         
         while(current_node->next_alien != NULL)
         {
                                        if( current_node->alien_position[1] >= SCREEN_DIMY )
                                        {
                                                                           /* Get rid of Alien */
                                                                           previous_node = current_node;
                                                                           current_node = current_node->next_alien;
                                                                           previous_node->next_alien = current_node->next_alien;
                                                                           delete current_node;
                                                                           current_node = previous_node;
                                        }
                                        
                                        current_node = current_node->next_alien;
         }
         
         return;
    }
    
    void CGameAliens::ChangeAlienVelocity(AlienStruct *current_node)
    {
         unsigned int max_velocity=0;
         
         switch(current_node->alien_type)
         {
                                         case 1:
                                              max_velocity = ALIEN_TYPE_I_SPEED;
                                              break;
                                         case 2:
                                              max_velocity = ALIEN_TYPE_II_SPEED;
                                              break;
                                         case 3:
                                              max_velocity = ALIEN_TYPE_III_SPEED;
                                              break;
                                         case 4:
                                              max_velocity = ALIEN_TYPE_IV_SPEED;
                                              break;
                                         case 5:
                                              max_velocity = ALIEN_TYPE_V_SPEED;
                                              break;
         }
         
         /* Change X position, making sure the picture stays on the screen */
         if( (current_node->alien_position[0] < (SCREEN_DIMX - 300))&&(current_node->alien_position[0] > 0) )
         {
             current_node->alien_velocity[0] = -(current_node->alien_velocity[0]);
         }
         else
         {
             current_node->alien_velocity[0] = (rand()%max_velocity) - (max_velocity/2);
         }
         
         /* Change Y position */
         if( current_node->alien_position[1] > 0 )
         {
             current_node->alien_velocity[1] = -(current_node->alien_velocity[1]);
         }
         else
         {
             current_node->alien_velocity[1] = (rand()%max_velocity) - (max_velocity/2);
         }
         
         return;
    }
    This is the graphical aspect of the main game loop, w.r.t. drawing the aliens.
    Code:
    /* Draw the aliens */
                                   current_alien = alien_root;
                                   
                                   while(current_alien->next_alien != NULL)
                                   {
                                                                   if( current_alien->alien_destroy == false)
                                                                   {
                                                                       masked_blit(cgraphics.p_alien01, cgraphics.p_screen_buffer1, 0, 0, current_alien->alien_position[0], current_alien->alien_position[1] + cgraphics.p_alien01->h,
                                                                                                    cgraphics.p_alien01->w, cgraphics.p_alien01->h);
                                                                   }
                                                                   else
                                                                   {
                                                                       /* Draw Alien Ship Explosion */
                                                                       
                                                                       /* Get rid of this part of the linked list */
                                                                       temp_alien = current_alien;
                                                                       previous_alien->next_alien = current_alien->next_alien;
                                                                       delete temp_alien;
                                                                       temp_alien = NULL;
                                                                       allegro_message("Damage2");
                                                                       
                                                                       alien_counter--;
                                                                   }
                                                                   
                                                                   if(current_alien->next_alien != NULL)
                                                                   {
                                                                       previous_alien = current_alien;
                                                                       current_alien = current_alien->next_alien;
                                                                   }
                                                                   else
                                                                   {
                                                                       previous_alien = NULL;
                                                                       current_alien = NULL;
                                                                       break;
                                                                   }
                                   }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    ok. I'm just trying to get an alien ship to appear on the screen, and then to scroll down with the background. This is what I've got so far, although its not working.

    Code:
    ////////////////////////////////////////////////
    // ALIEN LOGIC SECTION //////////////////////////
    /////////////////////////////////////////////////
    /* Make Aliens Appear */
    if( alien_counter < 1 )
    {
         current_alien = alien_root;
                                       
         while(current_alien->next_alien != NULL)
         {
                      current_alien = current_alien->next_alien;
         }
                                       
         /* Add new Alien Here */
         current_alien->next_alien = new AlienStruct;
         current_alien = current_alien->next_alien;
         current_alien->alien_type = 1;
         current_alien->next_alien = NULL;
         current_alien->alien_destroy = false;
                                       
        current_alien->alien_position[0] = rand()%(SCREEN_DIMX - cgraphics.p_alien01->w);
        current_alien->alien_position[1] = cgraphics.p_alien01->h;
                                       
        current_alien->alien_velocity[0] = 0; /*(unsigned int)( (rand()%ALIEN_TYPE_I_SPEED) - (ALIEN_TYPE_I_SPEED/2) ); */
        current_alien->alien_velocity[1] = (unsigned int)( /*(rand()%ALIEN_TYPE_I_SPEED) - (ALIEN_TYPE_I_SPEED/2) + */(SCROLL_RATE*background_index));
                                       
         alien_counter++;
    }
                                   
    /* Make Aliens Move */
                                   
    current_alien = alien_root;
                                   
    while(current_alien->next_alien != NULL)
    {
          current_alien->alien_position[0] = current_alien->alien_position[0] + current_alien->alien_velocity[0];
          current_alien->alien_position[1] = current_alien->alien_position[1] + current_alien->alien_velocity[1];
                                                                   
          current_alien = current_alien->next_alien;
    }
    That is the code I'm using to get an alien ship 2 appear on the screen and move.

    This is how I'm drawing them onto the screen
    Code:
    /* Draw Aliens to the buffer */
    current_alien = alien_root;
                                   
    while(current_alien->next_alien != NULL)
    {
           masked_blit(cgraphics.p_alien01, cgraphics.p_screen_buffer1, 0, 0, current_alien->alien_position[0], current_alien->alien_position[1],
                                                                   cgraphics.p_alien01->w, cgraphics.p_alien01->h);
                                                                   
            current_alien = current_alien->next_alien;
    }
    Anyone got any ideas why the space ship isnt even appearing on the screen?

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    No one wants to pour through your source code on here. Narrow your question down a bit and then maybe some will help you.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Did you try setting the ship's positions to hardcoded values that would definitely appear on screen? Also, are you sure the code creating a new alien is called? Just some ideas on places to start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sphere code not working
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 10-03-2004, 07:29 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Easiest 'real' game to start with...
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 01-03-2002, 12:52 PM
  4. Easiest 'real' game to start with...
    By Leeman_s in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2002, 10:29 AM
  5. Replies: 7
    Last Post: 12-12-2001, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21