Sunteți pe pagina 1din 12

//The headers #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <string> #include "windows.

h" //Screen attributes const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //The surfaces SDL_Surface *background = NULL; SDL_Surface *card = NULL; SDL_Surface *card2 = NULL; SDL_Surface *card3 = NULL; SDL_Surface *card4 = NULL; SDL_Surface *digit1 = NULL; SDL_Surface *digit2 = NULL; SDL_Surface *screen = NULL; SDL_Surface *mainMenu = NULL; SDL_Surface *instructions = NULL; SDL_Surface *mainMenuSelectPlay = NULL; SDL_Surface *mainMenuSelectInstructions = NULL; SDL_Surface *mainMenuSelectPlayText = NULL; SDL_Surface *endScreen = NULL; int compCards[5]; int totalHand; bool userBust; bool compBust; int int int int card1Value card2Value card3Value card4Value = = = = 0; 0; 0; 0;

int selectPos = 1; std::string gameMode = "mainMenu"; bool hitOne = false; bool hitTwo = false; //The event structure that will be used SDL_Event event; SDL_Surface *load_image( std::string filename ) { //The image that's loaded SDL_Surface* loadedImage = NULL; //The optimized image that will be used SDL_Surface* optimizedImage = NULL; //Load the image

loadedImage = IMG_Load( filename.c_str() ); //If the image loaded if( loadedImage != NULL ) { //Create an optimized image optimizedImage = SDL_DisplayFormat( loadedImage ); //Free the old image SDL_FreeSurface( loadedImage ); } //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 ); } bool init() { //Initialize all SDL subsystems if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false; } //Set up the screen screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSU RFACE ); //If there was an error in setting up the screen if( screen == NULL ) { return false; } //Set the window caption SDL_WM_SetCaption( "BlackJack", NULL ); SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL); //If everything initialized fine return true; } void card_picker() {

srand( time( NULL ) ); int cardNumber = rand() % 9 + 1; card1Value = cardNumber + 1; switch( cardNumber ) { case 1 : card = break; case 2 : card = break; case 3 : card = break; case 4 : card = break; case 5 : card = break; case 6 : card = break; case 7 : card = break; case 8 : card = break; case 9 : card = break; } } void card2_picker() { int card2Number = rand() % 9 + 1; card2Value = card2Number + 1; switch( card2Number ) { case 1 : card2 = break; case 2 : card2 = break; case 3 : card2 = break; case 4 : card2 = load_image( "cards/2.png" ); load_image( "cards/3.png" ); load_image( "cards/4.png" ); load_image( "cards/5.png" ); load_image( "cards/2.png" ); load_image( "cards/3.png" ); load_image( "cards/4.png" ); load_image( "cards/5.png" ); load_image( "cards/6.png" ); load_image( "cards/7.png" ); load_image( "cards/8.png" ); load_image( "cards/9.png" ); load_image( "cards/10.png" );

break; case 5 : card2 = break; case 6 : card2 = break; case 7 : card2 = break; case 8 : card2 = break; case 9 : card2 = break; } } void card4_picker() {

load_image( "cards/6.png" ); load_image( "cards/7.png" ); load_image( "cards/8.png" ); load_image( "cards/9.png" ); load_image( "cards/10.png" );

int card4Number = rand() % 9 + 1; card4Value = card4Number + 1; switch( card4Number ) { case 1 : card4 = break; case 2 : card4 = break; case 3 : card4 = break; case 4 : card4 = break; case 5 : card4 = break; case 6 : card4 = break; case 7 : card4 = break; case 8 : card4 = break; case 9 : card4 = break; } load_image( "cards/2.png" ); load_image( "cards/3.png" ); load_image( "cards/4.png" ); load_image( "cards/5.png" ); load_image( "cards/6.png" ); load_image( "cards/7.png" ); load_image( "cards/8.png" ); load_image( "cards/9.png" ); load_image( "cards/10.png" );

} void card3_picker() { int card3Number = rand() % 9 + 1; card3Value = card3Number + 1; switch( card3Number ) { case 1 : card3 = break; case 2 : card3 = break; case 3 : card3 = break; case 4 : card3 = break; case 5 : card3 = break; case 6 : card3 = break; case 7 : card3 = break; case 8 : card3 = break; case 9 : card3 = break; } } bool load_files() { //Load the image background = load_image( "background.png" ); card_picker(); card2_picker(); mainMenu = load_image( "Menus/mainMenu.jpg" ); instructions = load_image( "Menus/instructions.png" ); mainMenuSelectPlay = load_image( "Menus/selectGame.png" ); mainMenuSelectInstructions = load_image( "Menus/selectInstructions.png" ); load_image( "cards/2.png" ); load_image( "cards/3.png" ); load_image( "cards/4.png" ); load_image( "cards/5.png" ); load_image( "cards/6.png" ); load_image( "cards/7.png" ); load_image( "cards/8.png" ); load_image( "cards/9.png" ); load_image( "cards/10.png" );

mainMenuSelectPlayText = load_image( "Menus/selectTextGame.png" ); //If there was an error in loading the image if( background == NULL ) { return false; } //If everything loaded fine return true; }

void clean_up() { //Free the image SDL_FreeSurface( background ); SDL_FreeSurface( card ); SDL_FreeSurface( card2 ); SDL_FreeSurface( card3 ); SDL_FreeSurface( card4 ); SDL_FreeSurface( digit1 ); SDL_FreeSurface( digit2 ); SDL_FreeSurface( screen ); SDL_FreeSurface( mainMenu ); SDL_FreeSurface( instructions ); SDL_FreeSurface( mainMenuSelectPlay ); SDL_FreeSurface( mainMenuSelectInstructions ); SDL_FreeSurface( mainMenuSelectPlayText ); SDL_FreeSurface( endScreen ); //Quit SDL SDL_Quit(); } int compBlock() {

compCards[0] compCards[1] compCards[2] compCards[3] compCards[4] compCards[5]

= = = = = =

rand() % 10 + 1; rand() % 10 + 1; 0; 0; 0; 0;

while(compCards[0] + compCards[1] + compCards[2] + compCards[3] + compCards[ 4] + compCards[5] <= 17) { if (compCards[2] == 0) { compCards[2] = rand() % 10 + 1; goto skip; } if (compCards[3] == 0) { compCards[3] = rand() % 10 + 1; goto skip; } if (compCards[4] == 0) { compCards[4] = rand() % 10 + 1; goto skip; } if (compCards[5] == 0) { compCards[5] = rand() % 10 + 1; goto skip; }

skip: if( compCards[0] + compCards[1] + compCards[2] + compCards[3] + compCards[4] + compCards[5] > 21 ) { compBust = true; } } safeExit: int compHand; compHand = compCards[0] + compCards[1] + compCards[2] + compCards[3] + compC ards[4] + compCards[5]; return compHand; } void endGame() { int x = totalHand; int y = compBlock(); if( userBust && compBust ) { endScreen = load_image( "Menus/push.png" ); goto end; } if( userBust && !compBust ) { endScreen = load_image( "Menus/lose.png" ); goto end; }

if( !userBust && compBust ) { endScreen = load_image( "Menus/win.png" ); goto end; } if( !userBust && !compBust ) { if( y > x ) { endScreen = load_image( "Menus/lose.png" ); } if( x > y ) { endScreen = load_image( "Menus/win.png" ); } if( x == y ) { endScreen = load_image( "Menus/push.png" ); } } end: gameMode = "end game"; }

int main( int argc, char* args[] ) { //Make sure the program waits for a quit bool quit = false; //Initialize if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } //While the user hasn't quit while( quit == false ) { //While there's an event to handle while( SDL_PollEvent( &event ) ) { //Apply the surface to the screen if( gameMode == "end game" ) { apply_surface( 0, 0, endScreen, screen ); } if( gameMode == "instructions" ) { apply_surface( 0, 0, instructions, screen );

} if( gameMode == "mainMenu" ) { apply_surface( 0, 0, mainMenu, screen ); switch( selectPos ) { case 1 : apply_surface( 211, 99, mainMenuSelectPlay, screen ); break; case 2 : apply_surface( 211, 177, mainMenuSelectInstructions, screen ); break; case 3 : apply_surface( 211, 250, mainMenuSelectPlayText, screen ); break; default : apply_surface( 211, 187, mainMenuSelectPlay, screen ); break; } } if( gameMode == "game" ) { apply_surface( 0, 0, background, screen ); apply_surface( 10, 10, card, screen ); apply_surface( 95, 10, card2, screen ); apply_surface( 180, 10, card3, screen ); apply_surface( 265, 10, card4, screen ); } totalHand = card1Value + card2Value + card3Value + card4Value; switch( totalHand ) { case 2 : digit1 digit2 break; case 3 : digit1 digit2 break; case 4 : digit1 digit2 break; case 5 : digit1 digit2 break; case 6 : digit1 digit2 = load_image( "numbers/2.png" ); = NULL; = load_image( "numbers/3.png" ); = NULL; = load_image( "numbers/4.png" ); = NULL; = load_image( "numbers/5.png" ); = NULL; = load_image( "numbers/6.png" ); = NULL;

break; case 7 : digit1 digit2 break; case 8 : digit1 digit2 break; case 9 : digit1 digit2 break; case 10 : digit1 digit2 break; case 11 : digit1 digit2 break; case 12 : digit1 digit2 break; case 13 : digit1 digit2 break; case 14 : digit1 digit2 break; case 15 : digit1 digit2 break; case 16 : digit1 digit2 break; case 17 : digit1 digit2 break; case 18 : digit1 digit2 break; case 19 : digit1 digit2 break; case 20 : digit1 digit2 break; case 21 : digit1 digit2

= load_image( "numbers/7.png" ); = NULL; = load_image( "numbers/8.png" ); = NULL; = load_image( "numbers/9.png" ); = NULL; = load_image( "numbers/1.png" ); = load_image( "numbers/0.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/2.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/3.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/4.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/5.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/6.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/7.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/8.png" ); = load_image( "numbers/1.png" ); = load_image( "numbers/9.png" ); = load_image( "numbers/2.png" ); = load_image( "numbers/0.png" ); = load_image( "numbers/2.png" ); = load_image( "numbers/1.png" );

break; default : digit1 = load_image( "numbers/busted.png" ); digit2 = NULL; userBust = true; break; } if( gameMode == "game" ) { apply_surface( 490, 10, digit1, screen ); apply_surface( 565, 10, digit2, screen ); } if( SDL_Flip( screen ) == -1 ) { return 1; } //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } if( gameMode == "game" ) { if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_UP ) { if( hitOne == false ) { card3_picker(); hitOne = true; } else { if( hitTwo == false ) { card4_picker(); hitTwo = true; } } } if( event.key.keysym.sym == SDLK_DOWN ) { endGame(); } } } if( gameMode == "mainMenu" ) { if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_DOWN ) { if( selectPos != 3 ) { selectPos = selectPos + 1; } else { selectPos = 1; } }

} if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_RETURN ) { switch( selectPos ) { case 1 : gameMode = "game"; break; case 2 : gameMode = "instructions"; break; case 3 : gameMode = "text game"; break; } } } } if( gameMode == "instructions" ) { if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_ESCAPE ) { gameMode = "mainMenu"; } } } if( gameMode == "text game" ) { if( event.type == SDL_KEYDOWN ) { if( event.key.keysym.sym == SDLK_RETURN ) { system("textBlackJack.exe"); quit = true; } } } } }

//Free the surface and quit SDL clean_up(); return 0; }

S-ar putea să vă placă și