#ifndef SDL_VALUE_H_1134123 #define SDL_VALUE_H_1134123 //The headers #include "SDL/SDL.h" //Event structure SDL_Event event; //For ending program bool quit = false; int mouse_x=0; int mouse_y=0; //The attributes of the screen const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //surfaces SDL_Surface *Screen = NULL; SDL_Surface *Background = NULL; SDL_Surface *Person = NULL; SDL_Surface *Bullet = NULL; SDL_Surface *Monster = NULL; SDL_Surface *Start_screen = NULL; SDL_Surface *Dead_monster = NULL; SDL_Surface *You_have_died = NULL; SDL_Surface *Double_speed = NULL; SDL_Surface *Shield = NULL; SDL_Surface *Monster_slow = NULL; #endif #ifndef SDLSIDEKICK_H_1123123 #define SDLSIDEKICK_H_1123123 #include "SDL_value.h" //#include #include using namespace std; //Load image function SDL_Surface *load_image( char filename [50] ) { //Temporary storage for the image that's loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image loadedImage = SDL_LoadBMP( filename ); //If nothing went wrong in loading the image if( loadedImage != NULL ) { //Create an optimized image optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); //Map the color key Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 255, 0, 255); //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey ); } //Return the optimized image return optimizedImage; } void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Temporary rectangle to hold the offsets SDL_Rect offset; //Get the offsets offset.x = x; offset.y = y; //Blit the surface SDL_BlitSurface( source, NULL, destination, &offset ); } int init_SDL() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING) == -1 ) { //MessageBox(NULL,"ERROR","SDL has failed to init",MB_OK); return 0; } //Set up the screen Screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE ); //Set the window caption SDL_WM_SetCaption( "Raz0r Wizard (c) Mirchiss", NULL); //If there was in error in setting up the screen if( Screen == NULL ) { //MessageBox(NULL,"ERROR","Error setting video mode",MB_OK); return 0; } } #endif #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; }