All pastes #400040 Raw Edit

toma

public text v1 · immutable
·published 2007-03-18 13:07 UTC
rendered paste body
#include "SDL_Sidekick.h"

int main( int argc, char* args[] )
{
    //Init function
  if (init_SDL() == 0)
    return 1;
  srand(time(NULL));
  
  while (quit == false)
  {
 
    SDL_Flip(Screen);
    
    SDL_Delay( 2);
     
    if( SDL_PollEvent( &event ) )
    {     //Checks if user wants to quit
      switch (event.type)
      {
        case SDL_QUIT:
        {
          quit = true;
          break;
        }
        case SDL_MOUSEBUTTONDOWN:
        {
          if( event.button.button == SDL_BUTTON_LEFT )
          {
            break;
          }
        }
        case SDL_KEYDOWN:
        {
          switch( event.key.keysym.sym )
          {
            break;
          }
        }
      }   
    }
  }
  return 0;
}
#include "SDL_Sidekick.h"

struct S_Location
{
  int X;
  int Y;
};

struct S_Box
{ 
  //Natural Speed of the Box
  float Speed;
  //Coordinates
  float X;
  float Y;
  //Speeds
  float X_speed;
  float Y_speed;
  //Size of Box
  int X_size;
  int Y_size;
  S_Box() : X_speed(0), Y_speed(0), X_size(16), Y_size(16),
            X( 32 ), Y( 32 ), Speed (1)  {}
};
//Enemy linked list structure
struct Enemy_node
{
  S_Box Info;
  Enemy_node *Next;
};

int Calc_Box_Dest(S_Box &Object, S_Location Destination)
{
  //Distance between player and desired position
  float Dist_X = Object.X - Destination.X ;
  float Dist_Y = Object.Y - Destination.Y ;
  //Corrects float accuracy loss
  if( Dist_X  < Object.Speed && Dist_X > -Object.Speed) {Object.X = (int)Destination.X; }
  if( Dist_Y  < Object.Speed && Dist_Y > -Object.Speed) {Object.Y = (int)Destination.Y; }
  //Relation between Dist_X and Dist_Y;
  float Relation;
  //Case Players distance is negative
  if( Dist_X<0 ) {Dist_X=Dist_X*-1;}
  if( Dist_Y<0 ) {Dist_Y=Dist_Y*-1;}
  //If point is farther in X axis than Y axis
  if(Dist_X >= Dist_Y)
  {
    if(Dist_Y==0) {Relation =0;}
    else {Relation = Dist_Y / Dist_X;}
    
    if(Object.X == Destination.X) { Object.X_speed = 0; }
    else if(Object.X > Destination.X) { Object.X_speed = Object.Speed*-1; }
    else if(Object.X < Destination.X) { Object.X_speed = Object.Speed; }
  
    if(Object.Y == Destination.Y) { Object.Y_speed = 0; }
    else if(Object.Y > Destination.Y) { Object.Y_speed = Relation*Object.Speed*-1; }
    else if(Object.Y < Destination.Y) { Object.Y_speed = Relation*Object.Speed; }  
  }
  //If point is farther in Y axis than X axis
  if(Dist_X <= Dist_Y)
  {
    if(Dist_X==0) {Relation =0;}
    else {Relation = Dist_X / Dist_Y;}
    
    if(Object.X == Destination.X) { Object.X_speed = 0; }
    else if(Object.X > Destination.X) { Object.X_speed = Relation*Object.Speed*-1; }
    else if(Object.X < Destination.X) { Object.X_speed = Relation*Object.Speed; }
  
    if(Object.Y == Destination.Y) { Object.Y_speed = 0; }
    else if(Object.Y > Destination.Y) { Object.Y_speed = Object.Speed*-1; }
    else if(Object.Y < Destination.Y) { Object.Y_speed = Object.Speed; }  
  }
}  

int Calc_Box_location( S_Box &A_Box)
{
  //OUT OF BOUNDS CHECK
  if( A_Box.Y + A_Box.Y_speed <= 0) { A_Box.Y_speed = A_Box.Y_speed*-1;}
  if( A_Box.Y + A_Box.Y_speed >= 480-A_Box.Y_size) { A_Box.Y_speed = A_Box.Y_speed*-1;}
 
  if( A_Box.X + A_Box.X_speed <= 0) { A_Box.X_speed = A_Box.X_speed*-1;}
  if( A_Box.X + A_Box.X_speed >= 640-A_Box.X_size) { A_Box.X_speed = A_Box.X_speed*-1;}
  
  A_Box.Y = A_Box.Y + (A_Box.Y_speed);
  A_Box.X = A_Box.X + (A_Box.X_speed);
}


int game()
{
    //Sets up game
  srand(time(NULL));
  
  //Player Declared
  S_Box Player;
  Player.X = rand()%640-Player.X_size;
  Player.Y = rand()%480-Player.X_size;
  //Bullet Declared
  S_Box Weapon;
  Weapon.X = Player.X;
  Weapon.Y = Player.Y;
  Weapon.X_size = 8;
  Weapon.Y_size = 8;
  Weapon.Speed = 4;
  
  //Enemy Declared
  Enemy_node *EN_Conductor;
  Enemy_node *EN_Root;
  
  EN_Root = new Enemy_node;
  EN_Conductor = EN_Root;
  
  EN_Conductor->Info.X= rand()%640-Player.X_size;
  EN_Conductor->Info.Y= rand()%480-Player.Y_size;
  EN_Conductor->Next = NULL;
  S_Location Enemy_dest;
  
  //Player Moving
  S_Location Player_dest;
  Player_dest.X = (int)Player.X;
  Player_dest.Y = (int)Player.Y;
  
  //Player Shooting
  S_Location Weapon_dest;
  
  Weapon_dest.X =(int)Player.X;
  Weapon_dest.Y =(int)Player.Y;
 
 
  
  
 while (quit == false)
  {
    //Monster Moving
    EN_Conductor = EN_Root;
    
    //Player Moving
    Calc_Box_Dest( Player, Player_dest);
    Calc_Box_location(Player);
    //Bullet Moving
    Calc_Box_Dest( Weapon, Weapon_dest);
    Calc_Box_location(Weapon);
    
    //Returning Bullet back to player if it has no target
    if((int)Weapon.X_speed == 0 && (int)Weapon.Y_speed == 0 )
    {
      Weapon_dest.X =(int)Player.X-2;
      Weapon_dest.Y =(int)Player.Y-2;
    }
    
    
    apply_surface( 0, 0, Background, Screen);
    apply_surface( (int)Player.X, (int)Player.Y, Person, Screen);
    apply_surface( (int)Weapon.X, (int)Weapon.Y, Bullet, Screen );
    
    while(true)
    {
      Enemy_dest.X = (int)Player.X;
      Enemy_dest.Y = (int)Player.Y;
      
      Calc_Box_Dest( EN_Conductor->Info, Enemy_dest);
      Calc_Box_location(EN_Conductor->Info);
      
      apply_surface((int)EN_Conductor->Info.X,(int)EN_Conductor->Info.Y, Monster, Screen );
      
      if( EN_Conductor->Next == NULL ) { break;}
      else { EN_Conductor = EN_Conductor->Next;}
      
    }
    SDL_Flip(Screen);
    
    SDL_Delay( 2);
     
    if( SDL_PollEvent( &event ) )
    {     //Checks if user wants to quit
      switch (event.type)
      {
        case SDL_QUIT:
        {
          quit = true;
          break;
        }
        case SDL_MOUSEBUTTONDOWN:
        {
          if( event.button.button == SDL_BUTTON_LEFT )
          {
            Player_dest.X = event.button.x;
            Player_dest.Y = event.button.y;
            
          }
          if( event.button.button == SDL_BUTTON_RIGHT )
          {
            Weapon_dest.X = event.button.x;
            Weapon_dest.Y = event.button.y;
            
          }
        }
        case SDL_KEYDOWN:
        {
          switch( event.key.keysym.sym )
          {
            break;
          }
        }
      }   
    }
  }
return 0;
}