Abdulg

Lobster Pots V3 [C++]

Feb 13th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.84 KB | None | 0 0
  1. //Not responsible for brain damage caused by reading this code
  2. #include "SDL.h"
  3. #include "SDL_image.h"
  4. #include "SDL_ttf.h"
  5. #include "SDL_mixer.h"
  6. #include <string>
  7. #include <sstream>
  8. #include <time.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <fstream>
  12. //Actual game shit
  13. int Chance, YN, Inshore, Outshore, Profit, Buying, HMoney1, HMoney2, HMoney3, PotPrice, Lobsters, LobsterPrice, LobsterCatch, Selling, TotalLobsters = 0;
  14. int DaysG = 7;
  15. int Money = 200;
  16. int LobsterPots = 0;
  17. int *Temp = 0;
  18. char Temp2 = NULL;
  19. std::string String;
  20. std::string HName1;
  21. std::string HName2;
  22. std::string HName3;
  23. SDL_Surface *Text;
  24. std::stringstream LobstersHeader;
  25. bool Entered = false;
  26. SDL_Surface *MenuScreen = NULL;
  27. SDL_Surface *Black = NULL;
  28. SDL_Surface *Buttons = NULL;
  29. SDL_Surface *ButtonsR = NULL;
  30. SDL_Surface *Leaderboards = NULL;
  31. SDL_Surface *Message = NULL;
  32. SDL_Surface *Message2 = NULL;
  33. SDL_Surface *MouseCoords;
  34. SDL_Surface *Screen = NULL;
  35. SDL_Surface *Background = NULL;
  36. SDL_Surface *LeHighScores = NULL;
  37. SDL_Surface *Splash = NULL;
  38. SDL_Surface *Marketplace = NULL;
  39. SDL_Surface *DaysText = NULL;
  40. SDL_Surface *MoneyText = NULL;
  41. SDL_Surface *PotsText = NULL;
  42. SDL_Surface *LobstersText = NULL;
  43. SDL_Surface *Number = NULL;
  44. SDL_Surface *Sunny = NULL;
  45. SDL_Surface *Stormy = NULL;
  46. SDL_Surface *BlackRectangle = NULL;
  47. std::stringstream BuyPotsStream;
  48. std::stringstream BuyPotsStream2;
  49. TTF_Font *Font = NULL;
  50. TTF_Font *FontS = NULL;
  51. TTF_Font *FontSS = NULL;
  52. Mix_Music *Music = NULL;
  53. Mix_Music *MenuMusic = NULL;
  54.  
  55. int x , y = 0;
  56.  
  57. std::stringstream MouseXY;
  58.  
  59. SDL_Rect ButtonClips[8];
  60.  
  61. const int ScreenWidth = 640;
  62. const int ScreenHeight = 320;
  63. const int ScreenBBP = 32;
  64.  
  65. bool Quit = false;
  66. bool NumberGenerated = false;
  67.  
  68. SDL_Colour TextColour = {225,225,225};
  69. SDL_Colour TextColourBlack = {0,0,0};
  70. std::stringstream PotsHeader;
  71.  
  72. SDL_Surface *LoadImage( std::string filename )
  73. {
  74.     SDL_Surface* LoadedImage = NULL;
  75.     SDL_Surface* OptimizedImage = NULL;
  76.         LoadedImage = IMG_Load( filename.c_str() );
  77.     if( LoadedImage != NULL )
  78.     {
  79.         OptimizedImage = SDL_DisplayFormat( LoadedImage );
  80.         SDL_FreeSurface( LoadedImage );
  81.     }
  82.     return OptimizedImage;
  83. }
  84.  
  85. void ApplySurface( int x, int y, SDL_Surface* Source, SDL_Surface* Destination, SDL_Rect* Clip = NULL )
  86. {
  87.     SDL_Rect offset;
  88.  
  89.     offset.x = x;
  90.     offset.y = y;
  91.    
  92.     SDL_BlitSurface( Source, Clip, Destination, &offset );
  93. }
  94.  
  95.     bool LoadFiles()
  96. {
  97.         MenuScreen = LoadImage("Resources/Images/MenuScreen.png");
  98.         if (MenuScreen == NULL) return false;
  99.         Buttons = LoadImage ("Resources/Images/Buttons.png");
  100.         if (Buttons == NULL) return false;
  101.         ButtonsR = LoadImage ("Resources/Images/Rollover.png");
  102.         if (ButtonsR == NULL) return false;
  103.         Black = LoadImage ("Resources/Images/Black.png");
  104.         if (Black == NULL) return false;
  105.         BlackRectangle = LoadImage ("Resources/Images/BlackRectangle.png");
  106.         if (BlackRectangle == NULL) return false;
  107.         Font = TTF_OpenFont("Resources/Fonts/Arcade.ttf",30);
  108.         if (Font == NULL) return false;
  109.         FontS = TTF_OpenFont("Resources/Fonts/Arcade.ttf",20);
  110.         if (FontS == NULL) return false;
  111.         FontSS = TTF_OpenFont("Resources/Fonts/Arcade.ttf",10);
  112.         if (FontSS == NULL) return false;
  113.         Music = Mix_LoadMUS("Resources/Sounds/NinjaTuna.wav");
  114.         if (Music == NULL) return false;
  115.         Background = LoadImage ("Resources/Images/Background.png");
  116.         if (Background == NULL) return false;
  117.         Marketplace = LoadImage ("Resources/Images/Marketplace.png");
  118.         if (Marketplace == NULL) return false;
  119.         Splash = LoadImage ("Resources/Images/Splash.png");
  120.         if (Splash == NULL) return false;
  121.         MenuMusic = Mix_LoadMUS ("Resources/Sounds/RockLobster.wav");
  122.         if (MenuMusic == NULL) return false;
  123.         Stormy = LoadImage ("Resources/Images/Stormy.png");
  124.         if (Stormy == NULL) return false;
  125.         Sunny = LoadImage ("Resources/Images/Sunny.png");
  126.         if (Sunny == NULL) return false;
  127.         LeHighScores = LoadImage ("Resources/Images/Scores.png");
  128.         if (LeHighScores == NULL) return false;
  129.         std::ifstream ScoresInput;
  130.         ScoresInput.open("HighScores.plsdntedit");
  131.         ScoresInput >> HName1 >> HMoney1 >> HName2 >> HMoney2 >> HName3 >> HMoney3;
  132.         ScoresInput.close();
  133.         return true;
  134. }
  135.  
  136. void CleanUp()
  137. {
  138.     SDL_FreeSurface(MenuScreen);
  139.     SDL_FreeSurface(Buttons);
  140.     SDL_FreeSurface(ButtonsR);
  141.     SDL_FreeSurface(Black);
  142.     SDL_FreeSurface(BlackRectangle);
  143.     SDL_FreeSurface(Background);
  144.     SDL_FreeSurface(Marketplace);
  145.     SDL_FreeSurface(Stormy);
  146.     SDL_FreeSurface(Sunny);
  147.     SDL_FreeSurface(Splash);
  148.     TTF_CloseFont(Font);
  149.     TTF_CloseFont(FontS);
  150.     TTF_CloseFont(FontSS);
  151.     TTF_Quit;
  152.     SDL_Quit();
  153. }
  154.     bool Initiate()
  155. {
  156.     if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return false;
  157.     if (TTF_Init() == -1) return false;
  158.     Screen = SDL_SetVideoMode(ScreenWidth,ScreenHeight,ScreenBBP,SDL_SWSURFACE);
  159.     if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) return false;
  160.     SDL_WM_SetCaption("Lobster pots", NULL);
  161.     SDL_EnableUNICODE(SDL_ENABLE);
  162.     return true;
  163. }
  164.  
  165. void SetClips()
  166. {
  167.     /*Clip ranges*/
  168.     //Newgame
  169.     ButtonClips [ 0 ].x = 0;
  170.     ButtonClips [ 0 ].y = 0;
  171.     ButtonClips [ 0 ].w = 150;
  172.     ButtonClips [ 0 ].h = 20;
  173.  
  174.     //High Scores
  175.     ButtonClips [ 1 ].x = 151;
  176.     ButtonClips [ 1 ].y = 0;
  177.     ButtonClips [ 1 ].w = 150;
  178.     ButtonClips [ 1 ].h = 20;
  179.  
  180.     //Weather
  181.     ButtonClips [ 2 ].x = 302;
  182.     ButtonClips [ 2 ].y = 0;
  183.     ButtonClips [ 2 ].w = 150;
  184.     ButtonClips [ 2 ].h = 20;
  185.  
  186.     //Sell lobsters
  187.     ButtonClips [ 3 ].x = 453;
  188.     ButtonClips [ 3 ].y = 0;
  189.     ButtonClips [ 3 ].w = 150;
  190.     ButtonClips [ 3 ].h = 20;
  191.  
  192.     //Buy pots
  193.     ButtonClips [ 4 ].x = 604;
  194.     ButtonClips [ 4 ].y = 0;
  195.     ButtonClips [ 4 ].w = 150;
  196.     ButtonClips [ 4 ].h = 20;
  197.  
  198.     //Cancel
  199.     ButtonClips [ 5 ].x = 755;
  200.     ButtonClips [ 5 ].y = 0;
  201.     ButtonClips [ 5 ].w = 150;
  202.     ButtonClips [ 5 ].h = 20;
  203.  
  204.     //Confirm
  205.     ButtonClips [ 6 ].x = 906;
  206.     ButtonClips [ 6 ].y = 0;
  207.     ButtonClips [ 6 ].w = 150;
  208.     ButtonClips [ 6 ].h = 20;
  209.  
  210.     //Next Day
  211.     ButtonClips [ 7 ].x = 1057;
  212.     ButtonClips [ 7 ].y = 0;
  213.     ButtonClips [ 7 ].w = 150;
  214.     ButtonClips [ 7 ].h = 20;
  215. }
  216.  
  217. Uint8 *KeyStates = SDL_GetKeyState( NULL );
  218. SDL_Event event;
  219.  
  220. int GetNumberInput()
  221. {
  222.     SDL_Surface *WorkYouPrick = NULL;
  223.     int NumberToReturn = 0;
  224.     std::stringstream SpareStream;
  225.     SpareStream << NumberToReturn;
  226.     while (Quit == false)
  227.     {
  228.         WorkYouPrick = TTF_RenderText_Solid(Font,SpareStream.str().c_str(),TextColour);
  229.         ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  230.         ApplySurface ((ScreenWidth-WorkYouPrick->w)/2,(ScreenHeight-WorkYouPrick->h)/2,WorkYouPrick,Screen);
  231.         SDL_Flip(Screen);
  232.         while(SDL_PollEvent(&event))
  233.         {
  234.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) { ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);  return NumberToReturn;}
  235.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT)
  236.             {
  237.                 NumberToReturn++;
  238.                 SpareStream.str("");
  239.                 SpareStream << NumberToReturn;
  240.             }
  241.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT)
  242.             {
  243.                 if (NumberToReturn != 0) NumberToReturn--;
  244.                 SpareStream.str("");
  245.                 SpareStream << NumberToReturn;
  246.             }
  247.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q)
  248.             {
  249.                 if (NumberToReturn - 10 < 0) NumberToReturn = 0;
  250.                 else NumberToReturn = NumberToReturn - 10;
  251.                 SpareStream.str("");
  252.                 SpareStream << NumberToReturn;
  253.             }
  254.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_e)
  255.             {
  256.                 NumberToReturn = NumberToReturn + 10;
  257.                 SpareStream.str("");
  258.                 SpareStream << NumberToReturn;
  259.             }
  260.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_a)
  261.             {
  262.                 if (NumberToReturn - 100 < 0) NumberToReturn = 0;
  263.                 else NumberToReturn = NumberToReturn -  100;
  264.                 SpareStream.str("");
  265.                 SpareStream << NumberToReturn;
  266.             }
  267.             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_d)
  268.             {
  269.                 NumberToReturn = NumberToReturn + 100;
  270.                 SpareStream.str("");
  271.                 SpareStream << NumberToReturn;
  272.             }
  273.             if (event.type == SDL_QUIT) Quit = true;
  274.         }
  275.     }
  276. }
  277. enum Gamestate {SCORES,INTRO,MAINMENU,GAME,NEXTDAY,BUYPOTS,SELLLOBSTERS,WEATHER};
  278. Gamestate State = INTRO;
  279. bool MusicPlaying = false;
  280. bool Applied = false;
  281.  
  282.  
  283. int RandomNumber(int First, int Second)
  284. {
  285.     return rand () % First + Second;
  286. }
  287.  
  288.  
  289.  
  290. int YouFeelingLucky = 0;
  291.  
  292. void GenerateNumbers()
  293. {
  294.     srand (time(NULL));
  295.     YouFeelingLucky = RandomNumber(100,1);
  296.     Chance = RandomNumber(100,1);
  297.     PotPrice = RandomNumber(20,10);
  298.     LobsterPrice = RandomNumber(25,15);
  299.     Entered = false;
  300. }
  301.  
  302.  
  303.  
  304. //The key press interpreter
  305. class StringInput
  306. {
  307.     private:
  308.     //The text surface
  309.     SDL_Surface *text;
  310.  
  311.     public:
  312.     //The storage string
  313.     std::string str;
  314.  
  315.     //Initializes variables
  316.     StringInput();
  317.  
  318.     //Does clean up
  319.     ~StringInput();
  320.  
  321.     //Handles input
  322.     void handle_input();
  323.  
  324.     //Shows the message on screen
  325.     void show_centered();
  326. };
  327.  
  328.  
  329. StringInput::StringInput()
  330. {
  331.     //Initialize the string
  332.     str = "";
  333.  
  334.     //Initialize the surface
  335.     text = NULL;
  336.  
  337.     //Enable Unicode
  338.     SDL_EnableUNICODE( SDL_ENABLE );
  339. }
  340.  
  341. StringInput::~StringInput()
  342. {
  343.     //Free text surface
  344.     SDL_FreeSurface( text );
  345.  
  346.     //Disable Unicode
  347.     SDL_EnableUNICODE( SDL_DISABLE );
  348. }
  349.  
  350. void StringInput::handle_input()
  351. {
  352.     //If a key was pressed
  353.     if( event.type == SDL_KEYDOWN )
  354.     {
  355.         //Keep a copy of the current version of the string
  356.         std::string temp = str;
  357.  
  358.         //If the string less than maximum size
  359.         if( str.length() <= 8 )
  360.         {
  361.             //If the key is a number
  362.             if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
  363.             {
  364.                 //Append the character
  365.                 str += (char)event.key.keysym.unicode;
  366.             }
  367.             //If the key is a uppercase letter
  368.             else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
  369.             {
  370.                 //Append the character
  371.                 str += (char)event.key.keysym.unicode;
  372.             }
  373.             //If the key is a lowercase letter
  374.             else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
  375.             {
  376.                 //Append the character
  377.                 str += (char)event.key.keysym.unicode;
  378.             }
  379.         }
  380.  
  381.         //If backspace was pressed and the string isn't blank
  382.         if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
  383.         {
  384.             //Remove a character from the end
  385.             str.erase( str.length() - 1 );
  386.             text = TTF_RenderText_Solid( Font, str.c_str(), TextColour);
  387.             if ( str.length() == 1 ) ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  388.             SDL_Flip(Screen);
  389.         }
  390.         //If the string was changed
  391.         if( str != temp )
  392.         {
  393.             //Free the old surface
  394.             SDL_FreeSurface( text );
  395.  
  396.             //Render a new text surface
  397.             text = TTF_RenderText_Solid( Font, str.c_str(), TextColour);
  398.         }
  399.     }
  400. }
  401.  
  402. void StringInput::show_centered()
  403. {
  404.     //If the surface isn't blank
  405.     if( text != NULL )
  406.     {
  407.         //Show the name
  408.         ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  409.         ApplySurface( ( ScreenWidth - text->w ) / 2, ( ScreenHeight - text->h ) / 2, text, Screen );
  410.         SDL_Flip(Screen);
  411.     }
  412. }
  413.  
  414.  
  415. int main (int argc, char* argv [])
  416. {
  417.     /*Initiating and loading files*/
  418.     if (Initiate() == false) return -1;
  419.     if (LoadFiles() == false) return -1;
  420.     SetClips();
  421.  
  422.     srand(time(NULL));
  423.     std::stringstream High420Scores;
  424.     SDL_Flip(Screen);
  425.  
  426.     while (Quit == false)
  427.     {
  428.         switch (State)
  429.         {
  430.         case SCORES:
  431.             ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
  432.             ApplySurface(0,0,LeHighScores,Screen);
  433.             High420Scores << HName1;
  434.             Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
  435.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 10, Message, Screen );
  436.             High420Scores.str("");
  437.             High420Scores << "With " << HMoney1;
  438.             Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
  439.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 50, Message, Screen );
  440.  
  441.             High420Scores.str("");
  442.             High420Scores << HName2;
  443.             Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
  444.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 100, Message, Screen );
  445.             High420Scores.str("");
  446.             High420Scores << "With " << HMoney2;
  447.             Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
  448.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 140, Message, Screen );
  449.             SDL_Flip(Screen);
  450.  
  451.             High420Scores.str("");
  452.             High420Scores << HName3;
  453.             Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
  454.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 190, Message, Screen );
  455.             High420Scores.str("");
  456.             High420Scores << "With " << HMoney3;
  457.             Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
  458.             ApplySurface( ( ScreenWidth - Message->w ) / 2, 230, Message, Screen );
  459.             SDL_Flip(Screen);
  460.             while ((Quit == false) && (State == SCORES))
  461.             {
  462.                 while (SDL_PollEvent(&event))
  463.                 {
  464.                     if (event.type == SDL_MOUSEMOTION)
  465.                     {
  466.                         x = event.motion.x;
  467.                         y = event.motion.y;
  468.                         if ((x > 20 ) && (x < 170) &&  (y > 280) && (y<300)) ApplySurface(20, 280 , ButtonsR, Screen, &ButtonClips [6]); //Cancel
  469.                         else ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
  470.                         SDL_Flip(Screen);
  471.                     }
  472.                     if (event.type == SDL_MOUSEBUTTONDOWN)
  473.                     {
  474.                         if (event.button.button == SDL_BUTTON_LEFT)
  475.                         {
  476.                             if ((x > 20 ) && (x < 170) &&  (y > 280) && (y<300))
  477.                             {
  478.                                 Applied = false;
  479.                                 State = MAINMENU;
  480.                                 break;
  481.                             }
  482.                         }
  483.                     }
  484.                     if (event.type == SDL_QUIT)
  485.                     {
  486.                         Quit = true;
  487.                     }
  488.                 }
  489.             }
  490.             break;
  491.         case INTRO:
  492.             Message = TTF_RenderText_Solid(FontS,"Abdul Ghani presents",TextColour);
  493.             ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
  494.             Message = NULL;
  495.             SDL_Flip(Screen);
  496.             SDL_FreeSurface(Message);
  497.             SDL_Delay(5000);
  498.             SDL_Flip(Screen);
  499.             Message = TTF_RenderText_Solid(FontS,"With art by Ben Whitelaw",TextColour);
  500.             ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h + 30 ) / 2, Message, Screen );
  501.             SDL_Flip(Screen);
  502.             SDL_Delay(5000);
  503.             State = MAINMENU;
  504.             break;
  505.         case MAINMENU:
  506.             if (MusicPlaying == false)
  507.             {
  508.                     Mix_PlayMusic(MenuMusic,-1);
  509.                     MusicPlaying = true;
  510.             }
  511.             if (Applied == false)
  512.             {
  513.             ApplySurface(0,0,MenuScreen,Screen);
  514.             ApplySurface(20,280,Buttons,Screen,&ButtonClips[0]);
  515.             ApplySurface(480,280,Buttons,Screen,&ButtonClips[1]);
  516.             Applied = true;
  517.             }
  518.             SDL_Flip(Screen);
  519.             while (SDL_PollEvent(&event))
  520.             {
  521.                 if (event.type == SDL_MOUSEMOTION)
  522.                 {
  523.                     x = event.motion.x;
  524.                     y = event.motion.y;
  525.                     if ((x > 20) && (x < 170) && (y > 280) && (y < 300)) // New game
  526.                     {
  527.                         ApplySurface(20,280,ButtonsR,Screen,&ButtonClips[0]);
  528.                     }
  529.                     else ApplySurface(20,280,Buttons,Screen,&ButtonClips[0]);
  530.  
  531.                     if (( x > 480) && ( x < 630) && (y > 280) && (y < 300)) // High Scores
  532.                     {
  533.                         ApplySurface(480,280,ButtonsR,Screen,&ButtonClips[1]);
  534.                     }
  535.                     else ApplySurface(480,280,Buttons,Screen,&ButtonClips[1]);
  536.                 }
  537.  
  538.                 if (event.type == SDL_MOUSEBUTTONDOWN)
  539.                 {
  540.                     if (event.button.button == SDL_BUTTON_LEFT)
  541.                     {
  542.                         x = event.button.x;
  543.                         y = event.button.y;
  544.                         if ((x > 20) && (x < 170) && (y > 280) && (y < 300)) // If left mouse down over new game button
  545.                         {
  546.                             ApplySurface(0,0,Splash,Screen);
  547.                             SDL_Flip(Screen);
  548.                             SDL_Delay(10000);
  549.                             Applied = false;
  550.                             Mix_HaltMusic();
  551.                             MusicPlaying = false;
  552.                             State = GAME;
  553.                         }
  554.                         if (( x > 480) && ( x < 630) && (y > 280) && (y < 300)) // If left mouse down over high scores button
  555.                         {
  556.                             State = SCORES;
  557.                         }
  558.                         if (event.type == SDL_QUIT) Quit = true;
  559.                     }
  560.                 }
  561.                 if (event.type == SDL_QUIT)
  562.                 {
  563.                     Quit = true;
  564.                 }
  565.             }
  566.             break;
  567.         case GAME:
  568.             if (MusicPlaying == false)
  569.             {
  570.                     Mix_PlayMusic(Music,-1);
  571.                     MusicPlaying = true;
  572.             }
  573.             ApplySurface(0,0,Marketplace,Screen);
  574.             SDL_Flip(Screen);
  575.             while (Quit == false && State == GAME)
  576.             {
  577.                 if (NumberGenerated == false)
  578.                 {
  579.                     GenerateNumbers();
  580.                     NumberGenerated = true;
  581.                 }
  582.  
  583.                 if ((Money < LobsterPrice) && (Lobsters == 0) && (LobsterPots == 0))
  584.                 {
  585.                     ApplySurface(0,0,Black,Screen);
  586.                     Message = TTF_RenderText_Solid(FontS,"You ran out of things!",TextColour);
  587.                     ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
  588.                     Message = TTF_RenderText_Solid(FontS,"You starved to death!",TextColour);
  589.                     ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h + 30 ) / 2, Message, Screen );
  590.                     SDL_Flip(Screen);
  591.                     SDL_Delay(10000);
  592.                     Quit = true;
  593.                 }
  594.                 if (DaysG == 0)
  595.                 {
  596.                     bool NameEntered = false;
  597.                     StringInput Name;
  598.                     ApplySurface(0,0,Black,Screen);
  599.                     Money = Money + (LobsterPots * 5) + (Lobsters * LobsterPrice);
  600.                     std::stringstream ShitWilly;
  601.                     std::stringstream WillyShit;
  602.                     ShitWilly << "You got " << Money;
  603.                     WillyShit << "Your " << LobsterPots << " pots sold for " << LobsterPots * 5 << ", and your " << Lobsters << " lobsters sold for " << Lobsters * LobsterPrice << "!";
  604.                     Message = TTF_RenderText_Solid(FontS,ShitWilly.str().c_str(),TextColour);
  605.                     Message2 = TTF_RenderText_Solid(FontSS,WillyShit.str().c_str(),TextColour);
  606.                     ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
  607.                     ApplySurface((ScreenWidth-Message2->w)/2,40,Message2,Screen);
  608.                     Message = TTF_RenderText_Solid(FontS,"What is your name?",TextColour);
  609.                     ApplySurface((ScreenWidth-Message->w)/2,290,Message,Screen);
  610.                     SDL_Flip(Screen);
  611.                     while (NameEntered == false)
  612.                     {
  613.                         while (SDL_PollEvent(&event))
  614.                         {
  615.                             if (event.type == SDL_QUIT) Quit = true;
  616.                             Name.handle_input();
  617.                             Name.show_centered();
  618.                             if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN)
  619.                             {
  620.                                 NameEntered = true;
  621.                             }
  622.                         }
  623.                     }
  624.                     if (Money > HMoney1)
  625.                     {
  626.                         Message = TTF_RenderText_Solid(FontS,"You beat the high score!",TextColour);
  627.                         ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  628.                         ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
  629.                         SDL_Flip(Screen);
  630.                         HMoney3 = HMoney2;
  631.                         HName3 = HName2;
  632.                         HName2 = HName1;
  633.                         HMoney2 = HMoney1;
  634.                         HName1 = Name.str;
  635.                         HMoney1 = Money;
  636.                     }
  637.                     else if (Money > HMoney2)
  638.                     {
  639.                         Message = TTF_RenderText_Solid(FontS,"You came second!",TextColour);
  640.                         ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  641.                         ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
  642.                         SDL_Flip(Screen);
  643.                         HName3 = HName2;
  644.                         HMoney3 = HMoney2;
  645.                         HName2 = Name.str;
  646.                         HMoney2 = Money;
  647.                     }
  648.                     else if (Money > HMoney3)
  649.                     {
  650.                         Message = TTF_RenderText_Solid(FontS,"You came third!",TextColour);
  651.                         ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  652.                         ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
  653.                         SDL_Flip(Screen);
  654.                         HName3 = Name.str;
  655.                         HMoney3 = Money;
  656.                     }
  657.                     std::ofstream AlmostDone;
  658.                     AlmostDone.open("HighScores.plsdntedit");
  659.                     AlmostDone << HName1 << std::endl << HMoney1 << std::endl << HName2 << std::endl << HMoney2 << std::endl << HName3 << std::endl << HMoney3;
  660.                     AlmostDone.close();
  661.                     SDL_Delay(10000);
  662.                     Quit = true;
  663.                 }
  664.                 SDL_Flip(Screen);
  665.                 if (Applied == false && Quit == false)
  666.                 {
  667.                 std::stringstream Days;
  668.                 std::stringstream MoneyStream;
  669.                 std::stringstream PotsLeft;
  670.                 std::stringstream LobstersLeft;
  671.                 Days << "Days left: " << DaysG;
  672.                 DaysText = TTF_RenderText_Solid(FontS,Days.str().c_str(),TextColourBlack);
  673.                 MoneyStream << "Money: " << Money << " pounds";
  674.                 MoneyText = TTF_RenderText_Solid(FontS,MoneyStream.str().c_str(),TextColourBlack);
  675.                 PotsLeft << "Pots: " << LobsterPots;
  676.                 PotsText = TTF_RenderText_Solid (FontS,PotsLeft.str().c_str(),TextColourBlack);
  677.                 LobstersLeft << "Lobsters: " << Lobsters;
  678.                 LobstersText = TTF_RenderText_Solid (FontS,LobstersLeft.str().c_str(),TextColourBlack);
  679.                 ApplySurface((ScreenWidth-DaysText->w) / 2, 70, DaysText,Screen);
  680.                 ApplySurface((ScreenWidth-MoneyText->w) / 2, 100, MoneyText,Screen);
  681.                 ApplySurface((ScreenWidth-PotsText->w) / 2, 130, PotsText,Screen);
  682.                 ApplySurface((ScreenWidth-LobstersText->w) / 2, 160, LobstersText,Screen);
  683.                 ApplySurface(5,280,Buttons,Screen,&ButtonClips[7]);
  684.                 ApplySurface(160,280,Buttons,Screen,&ButtonClips[4]);
  685.                 ApplySurface(315,280,Buttons,Screen,&ButtonClips[3]);
  686.                 ApplySurface(470,280,Buttons,Screen,&ButtonClips[2]);
  687.                 Applied = true;
  688.                 }
  689.                 SDL_Flip(Screen);
  690.                 while (SDL_PollEvent (&event))
  691.                 {
  692.                     if (event.type == SDL_QUIT) Quit = true;
  693.                 }
  694.                     if (event.type == SDL_MOUSEMOTION)
  695.                 {
  696.                     x = event.motion.x;
  697.                     y = event.motion.y;
  698.                     if ((x > 5) && (x < 155) && (y > 280) && (y < 300)) // Next Day
  699.                     {
  700.                         ApplySurface(5,280,ButtonsR,Screen,&ButtonClips[7]);
  701.                     }
  702.                     else ApplySurface(5,280,Buttons,Screen,&ButtonClips[7]);
  703.  
  704.                     if ((x > 160) && (x < 310) && (y > 280) && (y < 300)) // Buy Pots
  705.                     {
  706.                         ApplySurface(160,280,ButtonsR,Screen,&ButtonClips[4]);
  707.                     }
  708.                     else ApplySurface(160,280,Buttons,Screen,&ButtonClips[4]);
  709.  
  710.                     if (( x > 315) && ( x < 465) && (y > 280) && (y < 300)) // Sell Lobsters
  711.                     {
  712.                         ApplySurface(315,280,ButtonsR,Screen,&ButtonClips[3]);
  713.                     }
  714.                     else ApplySurface(315,280,Buttons,Screen,&ButtonClips[3]);
  715.  
  716.                     if (( x > 470) && (x < 620) && (y > 280) && (y < 300)) // Weather
  717.                     {
  718.                         ApplySurface(470,280,ButtonsR,Screen,&ButtonClips[2]);
  719.                     }
  720.                     else ApplySurface(470,280,Buttons,Screen,&ButtonClips[2]);
  721.                 }
  722.                     if (event.type == SDL_MOUSEBUTTONDOWN)
  723.                     {
  724.                         if (event.button.button == SDL_BUTTON_LEFT)
  725.                         {
  726.                             x = event.button.x;
  727.                             y = event.button.y;
  728.  
  729.                             if ((x > 5) && (x < 155) && (y > 280) && (y < 300)) // Next Day
  730.                             {
  731.                                 Applied = false;
  732.                                 State = NEXTDAY;
  733.                                 break;
  734.                             }
  735.                             if ((x > 160) && (x < 310) && (y > 280) && (y < 300)) // Buy Pots
  736.                             {
  737.                                 Applied = false;
  738.                                 State = BUYPOTS;
  739.                                 break;
  740.                             }
  741.                             if (( x > 315) && ( x < 465) && (y > 280) && (y < 300)) // Sell Lobsters
  742.                             {
  743.                                 Applied = false;
  744.                                 State = SELLLOBSTERS;
  745.                                 break;
  746.                             }
  747.                                 if (( x > 470) && (x < 620) && (y > 280) && (y < 300)) // Weather
  748.                             {
  749.                                 Applied = false;
  750.                                 State = WEATHER;
  751.                                 break;
  752.                             }
  753.                         }
  754.                     }
  755.             }
  756.             break;
  757.         case NEXTDAY:
  758.             ApplySurface(0,0,Black,Screen);
  759.             SDL_Flip(Screen);
  760.             while (Entered == false)
  761.             {
  762.                 Message = TTF_RenderText_Solid(FontSS,"How many pots do you want to put inshore?",TextColour);
  763.                 ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
  764.                 Inshore = GetNumberInput();
  765.                 if (Inshore > LobsterPots)
  766.                 {
  767.                     ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  768.                     Message2 = TTF_RenderText_Solid(FontS,"You don't have enough pots!",TextColour);
  769.                     ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
  770.                     SDL_Flip(Screen);
  771.                     SDL_Delay(2000);
  772.                 }
  773.                 else
  774.                 {
  775.                     Outshore = LobsterPots - Inshore;
  776.                     std::stringstream PlacedPots;
  777.                     PlacedPots << "You placed " << Inshore << " pots inshore and " << Outshore << " pots outshore.";
  778.                     Message = TTF_RenderText_Solid(FontSS,PlacedPots.str().c_str(),TextColour);
  779.                     ApplySurface(0,0,Black,Screen);
  780.                     ApplySurface ((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
  781.                     SDL_Flip(Screen);
  782.                     SDL_Delay(5000);
  783.                     Entered = true;
  784.                 }
  785.             }
  786.  
  787.             if (YouFeelingLucky < Chance)
  788.             {
  789.                 ApplySurface(0,0,Stormy,Screen);
  790.                 Message = TTF_RenderText_Solid(FontS,"It was stormy!",TextColour);
  791.                 ApplySurface((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
  792.                 SDL_Flip(Screen);
  793.                 LobsterPots = LobsterPots - Outshore;
  794.                 Lobsters = Lobsters + Inshore;
  795.                 NumberGenerated = false;
  796.                 DaysG--;
  797.                 SDL_Delay(5000);
  798.                 State = GAME;
  799.             }
  800.             else
  801.             {
  802.             ApplySurface(0,0,Sunny,Screen);
  803.             Message = TTF_RenderText_Solid(FontS,"It was sunny!",TextColour);
  804.             ApplySurface((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
  805.             SDL_Flip(Screen);
  806.             Lobsters = Lobsters + Inshore + (Outshore * 2);
  807.             NumberGenerated = false;
  808.             DaysG--;
  809.             SDL_Delay(5000);
  810.             State = GAME;
  811.             }
  812.             while (Quit == false && State == NEXTDAY)
  813.             {
  814.                 while (SDL_PollEvent(&event))
  815.                 {
  816.                     SDL_Flip(Screen);
  817.                     if (event.type == SDL_QUIT)
  818.                     {
  819.                         Quit = true;
  820.                     }
  821.                 }
  822.             }
  823.         break;
  824.         case BUYPOTS:
  825.             ApplySurface(0,0,Black,Screen);
  826.             SDL_Flip(Screen);
  827.             PotsHeader.str("");
  828.             BuyPotsStream.str("");
  829.             PotsHeader << "Today, pots cost " << PotPrice << " pounds. You have " << Money << " pounds.";
  830.             BuyPotsStream << "Max amount of pots that you can buy:" << Money / PotPrice;
  831.             Message = TTF_RenderText_Solid(FontSS,PotsHeader.str().c_str(),TextColour);
  832.             ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
  833.             Message2 = TTF_RenderText_Solid(FontSS,BuyPotsStream.str().c_str(),TextColour);
  834.             ApplySurface((ScreenWidth-Message2->w)/2,40,Message2,Screen);
  835.             SDL_Flip(Screen);
  836.             while (Quit == false && State == BUYPOTS)
  837.             {
  838.                 SDL_Flip(Screen);
  839.                 int Selection = GetNumberInput();
  840.                 if (Selection * PotPrice > Money)
  841.                 {
  842.                     ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  843.                     Message2 = TTF_RenderText_Solid(FontS,"You don't have enough money!",TextColour);
  844.                     ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
  845.                     SDL_Flip(Screen);
  846.                     SDL_Delay(2000);
  847.                 }
  848.                 else
  849.                 {
  850.                     LobsterPots = LobsterPots + Selection;
  851.                     Money = Money - (PotPrice * Selection);
  852.                     PotsHeader.str("");
  853.                     PotsHeader << "You bought " << Selection << " pots for " << PotPrice * Selection << " pounds!";
  854.                     Message2 = TTF_RenderText_Solid (FontSS,PotsHeader.str().c_str(),TextColour);
  855.                     ApplySurface (0,0,Black,Screen);
  856.                     ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
  857.                     SDL_Flip(Screen);
  858.                     SDL_Delay(2000);
  859.                     State = GAME;
  860.                 }
  861.             }
  862.             break;
  863.         case SELLLOBSTERS:
  864.             ApplySurface(0,0,Black,Screen);
  865.             LobstersHeader.str("");
  866.             LobstersHeader << "Today, lobsters sell at " << LobsterPrice <<".";
  867.             Message = TTF_RenderText_Solid(FontSS,LobstersHeader.str().c_str(),TextColour);
  868.             ApplySurface((ScreenWidth - Message->w) / 2,10,Message,Screen);
  869.             LobstersHeader.str("");
  870.             LobstersHeader << "How many lobsters would you like to sell?";
  871.             Message = TTF_RenderText_Solid(FontSS,LobstersHeader.str().c_str(),TextColour);
  872.             ApplySurface((ScreenWidth - Message->w) / 2,40,Message,Screen);
  873.             SDL_Flip(Screen);
  874.             Selling = GetNumberInput();
  875.             if (Selling > Lobsters)
  876.             {
  877.                 ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
  878.                 Message2 = TTF_RenderText_Solid(FontS,"You don't have that many!",TextColour);
  879.                 ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
  880.                 SDL_Flip(Screen);
  881.                 SDL_Delay(2000);
  882.             }
  883.             else
  884.             {
  885.                 ApplySurface(0,0,Black,Screen);
  886.                 Lobsters = Lobsters - Selling;
  887.                 Money = Money + (LobsterPrice * Selling);
  888.                 LobstersHeader.str("");
  889.                 LobstersHeader << "You sold " << Selling << " lobsters for " << Selling * LobsterPrice << " pounds!";
  890.                 Message2 = TTF_RenderText_Solid (FontSS,LobstersHeader.str().c_str(),TextColour);
  891.                 ApplySurface(( ScreenWidth - Message2->w ) / 2, ( ScreenHeight - Message2->h )/ 2,Message2,Screen);
  892.                 SDL_Flip(Screen);
  893.                 SDL_Delay(5000);
  894.                 State = GAME;
  895.             }
  896.             break;
  897.         case WEATHER:
  898.             ApplySurface(0,0,Black,Screen);
  899.             std::stringstream StormChance;
  900.             StormChance << "There is a " << Chance << "% of a storm tonight.";
  901.             Message = TTF_RenderText_Solid(FontSS,StormChance.str().c_str(),TextColour);
  902.             ApplySurface(( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h )/ 2,Message,Screen);
  903.             ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
  904.             SDL_Flip(Screen);
  905.             while (Quit == false && State == WEATHER)
  906.             {
  907.                 while (SDL_PollEvent(&event))
  908.                 {
  909.                     SDL_Flip(Screen);
  910.                     if (event.type == SDL_QUIT)
  911.                     {
  912.                         Quit = true;
  913.                     }
  914.  
  915.                     if (event.type == SDL_MOUSEMOTION)
  916.                     {
  917.                         x = event.motion.x;
  918.                         y = event.motion.y;
  919.  
  920.                         if ((x > 20 ) && (x < 170) &&  (y > 280) && (y<300)) ApplySurface(20, 280 , ButtonsR, Screen, &ButtonClips [6]); //Cancel
  921.                         else ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
  922.                     }
  923.  
  924.                     if (event.type == SDL_MOUSEBUTTONDOWN)
  925.                     {
  926.                         if (event.button.button == SDL_BUTTON_LEFT)
  927.                         {
  928.                             if ((x > 20 ) && (x < 170) &&  (y > 280) && (y<300))
  929.                             {
  930.                                 SDL_Delay(500);
  931.                                 State = GAME;
  932.                                 break;
  933.                             }
  934.                         }
  935.                     }
  936.                 }
  937.             }
  938.         }
  939.     }
  940.     CleanUp();
  941.     return 0;
  942. }
Advertisement
Add Comment
Please, Sign In to add comment