All pastes #349261 Raw Edit

Mirchiss

public cpp v1 · immutable
·published 2007-02-10 17:47 UTC
rendered paste body
#include "SDL_Sidekick.h"#include <ctime>#include <cstdlib>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 structurestruct 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);}//On 1 The 2 objects Have int Calc_hits(S_Box Obj_alpha , S_Box Obj_beta ){  int Dist_x, Dist_y;    Dist_x= (int)Obj_alpha.X - (int)Obj_beta.X;  if( Dist_x<0 ){ Dist_x = Dist_x*-1;}    Dist_y= (int)Obj_alpha.Y - (int)Obj_beta.Y;   if( Dist_y<0 ){ Dist_y = Dist_y*-1;}      //Objects have collided  if( ( Dist_x < Obj_alpha.X_size &&      Dist_y < Obj_alpha.Y_size )       ||      (Dist_x < Obj_beta.X_size  &&       Dist_y < Obj_beta.Y_size )    )  { return 1;}  //objects havent collided  else {return 0;}      }int main( int argc, char* args[] ){    //Init function  if (init_SDL() == 9)    return 1;    Bullet = load_image("bullet.bmp");  Person = load_image("person.bmp");  Background = load_image("BD.bmp");  Monster = load_image("enemy.bmp");  Start_screen = load_image("SS.bmp");    srand(time(NULL));               //Player Declared  S_Box Player;  Player.X = rand()%640-Player.X_size;  Player.Y = rand()%480-Player.X_size;  int Score=0;  int Highscore =0;  //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;  int E_Waiting_creation = 0;    EN_Root = new Enemy_node;  EN_Conductor = EN_Root;    EN_Conductor->Info.X= rand()%640-EN_Conductor->Info.X_size;  EN_Conductor->Info.Y= rand()%480-EN_Conductor->Info.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;    bool Goto_game = false;  while (quit == false && Goto_game == false)  {    //PreGame Print  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 );  apply_surface((int)EN_Conductor->Info.X,(int)EN_Conductor->Info.Y, Monster, Screen );  apply_surface( 0, 0, Start_screen, Screen);  SDL_Flip(Screen);      if( SDL_PollEvent( &event ) )    {     //Checks if user wants to quit      switch (event.type)      {        case SDL_QUIT:        {          quit = true;          break;        }        case SDL_KEYDOWN:        {          switch( event.key.keysym.sym )          {            case SDLK_a: Goto_game = true;          }        }      }    }  }    while (quit == false)  {    char Show_score[100];        if( Score > Highscore)    {      Highscore = Score;    }         sprintf(Show_score,"Score: %i HighScore: %i (c) Mirchiss", Score, Highscore);    SDL_WM_SetCaption( Show_score, NULL);        //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 Bitmaps    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 );        EN_Conductor = EN_Root;    while(true)    {      Enemy_dest.X = (int)Player.X;      Enemy_dest.Y = (int)Player.Y;            //Checks if player has collided with Monster      if ( Calc_hits( EN_Conductor->Info, Player) == 1 )      {        EN_Condcuctor=EN_Root;        while( EN_Condcuctor->Next != NULL;)        {          EN_Condcuctor = EN_Condcuctor->Next;        }                                //Sets A new spawning place for monster        EN_Conductor->Info.X = rand()%(640 - EN_Conductor->Info.X_size);        EN_Conductor->Info.Y = rand()%(480 - EN_Conductor->Info.Y_size);        Score =0;              }            //Checks if weapon has hit Monster      if( Calc_hits( Weapon, EN_Conductor->Info ) == 1 )      {        //Sets the que for a new monster to be added to game        E_Waiting_creation++;        //Spawns the existing monster again        EN_Conductor->Info.X = rand()%(640 - EN_Conductor->Info.X_size);        EN_Conductor->Info.Y = rand()%(480 - EN_Conductor->Info.Y_size);        Score++;              }      //Moves the monster      Calc_Box_Dest( EN_Conductor->Info, Enemy_dest);      Calc_Box_location(EN_Conductor->Info);            //Applies the monsters image to screen      apply_surface((int)EN_Conductor->Info.X,(int)EN_Conductor->Info.Y, Monster, Screen );            //Breaks if there is no next node      if( EN_Conductor->Next == NULL ) { break;}      //Goes to next node      EN_Conductor = EN_Conductor->Next;          }    //Loop runs if there is a line of monsters to be spawned    EN_Conductor = EN_Root;    while( E_Waiting_creation > 0)    {            //Adds a new monster if we are in the end of the linked list      if(EN_Conductor->Next == NULL)      {        //Makes new node        EN_Conductor->Next = new Enemy_node;        //Goes to new node        EN_Conductor = EN_Conductor->Next;        EN_Conductor->Next = NULL;        //Sets Monster coordinates        EN_Conductor->Info.X = rand()%(640 - EN_Conductor->Info.X_size);        EN_Conductor->Info.Y = rand()%(480 - EN_Conductor->Info.Y_size);                //reduces the monster from the number of monsters needed to spawn        E_Waiting_creation--;      }            //Goes forward in the linked list      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;}