Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Not responsible for brain damage caused by reading this code
- #include "SDL.h"
- #include "SDL_image.h"
- #include "SDL_ttf.h"
- #include "SDL_mixer.h"
- #include <string>
- #include <sstream>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- //Actual game shit
- int Chance, YN, Inshore, Outshore, Profit, Buying, HMoney1, HMoney2, HMoney3, PotPrice, Lobsters, LobsterPrice, LobsterCatch, Selling, TotalLobsters = 0;
- int DaysG = 7;
- int Money = 200;
- int LobsterPots = 0;
- int *Temp = 0;
- char Temp2 = NULL;
- std::string String;
- std::string HName1;
- std::string HName2;
- std::string HName3;
- SDL_Surface *Text;
- std::stringstream LobstersHeader;
- bool Entered = false;
- SDL_Surface *MenuScreen = NULL;
- SDL_Surface *Black = NULL;
- SDL_Surface *Buttons = NULL;
- SDL_Surface *ButtonsR = NULL;
- SDL_Surface *Leaderboards = NULL;
- SDL_Surface *Message = NULL;
- SDL_Surface *Message2 = NULL;
- SDL_Surface *MouseCoords;
- SDL_Surface *Screen = NULL;
- SDL_Surface *Background = NULL;
- SDL_Surface *LeHighScores = NULL;
- SDL_Surface *Splash = NULL;
- SDL_Surface *Marketplace = NULL;
- SDL_Surface *DaysText = NULL;
- SDL_Surface *MoneyText = NULL;
- SDL_Surface *PotsText = NULL;
- SDL_Surface *LobstersText = NULL;
- SDL_Surface *Number = NULL;
- SDL_Surface *Sunny = NULL;
- SDL_Surface *Stormy = NULL;
- SDL_Surface *BlackRectangle = NULL;
- std::stringstream BuyPotsStream;
- std::stringstream BuyPotsStream2;
- TTF_Font *Font = NULL;
- TTF_Font *FontS = NULL;
- TTF_Font *FontSS = NULL;
- Mix_Music *Music = NULL;
- Mix_Music *MenuMusic = NULL;
- int x , y = 0;
- std::stringstream MouseXY;
- SDL_Rect ButtonClips[8];
- const int ScreenWidth = 640;
- const int ScreenHeight = 320;
- const int ScreenBBP = 32;
- bool Quit = false;
- bool NumberGenerated = false;
- SDL_Colour TextColour = {225,225,225};
- SDL_Colour TextColourBlack = {0,0,0};
- std::stringstream PotsHeader;
- SDL_Surface *LoadImage( std::string filename )
- {
- SDL_Surface* LoadedImage = NULL;
- SDL_Surface* OptimizedImage = NULL;
- LoadedImage = IMG_Load( filename.c_str() );
- if( LoadedImage != NULL )
- {
- OptimizedImage = SDL_DisplayFormat( LoadedImage );
- SDL_FreeSurface( LoadedImage );
- }
- return OptimizedImage;
- }
- void ApplySurface( int x, int y, SDL_Surface* Source, SDL_Surface* Destination, SDL_Rect* Clip = NULL )
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface( Source, Clip, Destination, &offset );
- }
- bool LoadFiles()
- {
- MenuScreen = LoadImage("Resources/Images/MenuScreen.png");
- if (MenuScreen == NULL) return false;
- Buttons = LoadImage ("Resources/Images/Buttons.png");
- if (Buttons == NULL) return false;
- ButtonsR = LoadImage ("Resources/Images/Rollover.png");
- if (ButtonsR == NULL) return false;
- Black = LoadImage ("Resources/Images/Black.png");
- if (Black == NULL) return false;
- BlackRectangle = LoadImage ("Resources/Images/BlackRectangle.png");
- if (BlackRectangle == NULL) return false;
- Font = TTF_OpenFont("Resources/Fonts/Arcade.ttf",30);
- if (Font == NULL) return false;
- FontS = TTF_OpenFont("Resources/Fonts/Arcade.ttf",20);
- if (FontS == NULL) return false;
- FontSS = TTF_OpenFont("Resources/Fonts/Arcade.ttf",10);
- if (FontSS == NULL) return false;
- Music = Mix_LoadMUS("Resources/Sounds/NinjaTuna.wav");
- if (Music == NULL) return false;
- Background = LoadImage ("Resources/Images/Background.png");
- if (Background == NULL) return false;
- Marketplace = LoadImage ("Resources/Images/Marketplace.png");
- if (Marketplace == NULL) return false;
- Splash = LoadImage ("Resources/Images/Splash.png");
- if (Splash == NULL) return false;
- MenuMusic = Mix_LoadMUS ("Resources/Sounds/RockLobster.wav");
- if (MenuMusic == NULL) return false;
- Stormy = LoadImage ("Resources/Images/Stormy.png");
- if (Stormy == NULL) return false;
- Sunny = LoadImage ("Resources/Images/Sunny.png");
- if (Sunny == NULL) return false;
- LeHighScores = LoadImage ("Resources/Images/Scores.png");
- if (LeHighScores == NULL) return false;
- std::ifstream ScoresInput;
- ScoresInput.open("HighScores.plsdntedit");
- ScoresInput >> HName1 >> HMoney1 >> HName2 >> HMoney2 >> HName3 >> HMoney3;
- ScoresInput.close();
- return true;
- }
- void CleanUp()
- {
- SDL_FreeSurface(MenuScreen);
- SDL_FreeSurface(Buttons);
- SDL_FreeSurface(ButtonsR);
- SDL_FreeSurface(Black);
- SDL_FreeSurface(BlackRectangle);
- SDL_FreeSurface(Background);
- SDL_FreeSurface(Marketplace);
- SDL_FreeSurface(Stormy);
- SDL_FreeSurface(Sunny);
- SDL_FreeSurface(Splash);
- TTF_CloseFont(Font);
- TTF_CloseFont(FontS);
- TTF_CloseFont(FontSS);
- TTF_Quit;
- SDL_Quit();
- }
- bool Initiate()
- {
- if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return false;
- if (TTF_Init() == -1) return false;
- Screen = SDL_SetVideoMode(ScreenWidth,ScreenHeight,ScreenBBP,SDL_SWSURFACE);
- if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) return false;
- SDL_WM_SetCaption("Lobster pots", NULL);
- SDL_EnableUNICODE(SDL_ENABLE);
- return true;
- }
- void SetClips()
- {
- /*Clip ranges*/
- //Newgame
- ButtonClips [ 0 ].x = 0;
- ButtonClips [ 0 ].y = 0;
- ButtonClips [ 0 ].w = 150;
- ButtonClips [ 0 ].h = 20;
- //High Scores
- ButtonClips [ 1 ].x = 151;
- ButtonClips [ 1 ].y = 0;
- ButtonClips [ 1 ].w = 150;
- ButtonClips [ 1 ].h = 20;
- //Weather
- ButtonClips [ 2 ].x = 302;
- ButtonClips [ 2 ].y = 0;
- ButtonClips [ 2 ].w = 150;
- ButtonClips [ 2 ].h = 20;
- //Sell lobsters
- ButtonClips [ 3 ].x = 453;
- ButtonClips [ 3 ].y = 0;
- ButtonClips [ 3 ].w = 150;
- ButtonClips [ 3 ].h = 20;
- //Buy pots
- ButtonClips [ 4 ].x = 604;
- ButtonClips [ 4 ].y = 0;
- ButtonClips [ 4 ].w = 150;
- ButtonClips [ 4 ].h = 20;
- //Cancel
- ButtonClips [ 5 ].x = 755;
- ButtonClips [ 5 ].y = 0;
- ButtonClips [ 5 ].w = 150;
- ButtonClips [ 5 ].h = 20;
- //Confirm
- ButtonClips [ 6 ].x = 906;
- ButtonClips [ 6 ].y = 0;
- ButtonClips [ 6 ].w = 150;
- ButtonClips [ 6 ].h = 20;
- //Next Day
- ButtonClips [ 7 ].x = 1057;
- ButtonClips [ 7 ].y = 0;
- ButtonClips [ 7 ].w = 150;
- ButtonClips [ 7 ].h = 20;
- }
- Uint8 *KeyStates = SDL_GetKeyState( NULL );
- SDL_Event event;
- int GetNumberInput()
- {
- SDL_Surface *WorkYouPrick = NULL;
- int NumberToReturn = 0;
- std::stringstream SpareStream;
- SpareStream << NumberToReturn;
- while (Quit == false)
- {
- WorkYouPrick = TTF_RenderText_Solid(Font,SpareStream.str().c_str(),TextColour);
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- ApplySurface ((ScreenWidth-WorkYouPrick->w)/2,(ScreenHeight-WorkYouPrick->h)/2,WorkYouPrick,Screen);
- SDL_Flip(Screen);
- while(SDL_PollEvent(&event))
- {
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) { ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen); return NumberToReturn;}
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT)
- {
- NumberToReturn++;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT)
- {
- if (NumberToReturn != 0) NumberToReturn--;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_q)
- {
- if (NumberToReturn - 10 < 0) NumberToReturn = 0;
- else NumberToReturn = NumberToReturn - 10;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_e)
- {
- NumberToReturn = NumberToReturn + 10;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_a)
- {
- if (NumberToReturn - 100 < 0) NumberToReturn = 0;
- else NumberToReturn = NumberToReturn - 100;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_d)
- {
- NumberToReturn = NumberToReturn + 100;
- SpareStream.str("");
- SpareStream << NumberToReturn;
- }
- if (event.type == SDL_QUIT) Quit = true;
- }
- }
- }
- enum Gamestate {SCORES,INTRO,MAINMENU,GAME,NEXTDAY,BUYPOTS,SELLLOBSTERS,WEATHER};
- Gamestate State = INTRO;
- bool MusicPlaying = false;
- bool Applied = false;
- int RandomNumber(int First, int Second)
- {
- return rand () % First + Second;
- }
- int YouFeelingLucky = 0;
- void GenerateNumbers()
- {
- srand (time(NULL));
- YouFeelingLucky = RandomNumber(100,1);
- Chance = RandomNumber(100,1);
- PotPrice = RandomNumber(20,10);
- LobsterPrice = RandomNumber(25,15);
- Entered = false;
- }
- //The key press interpreter
- class StringInput
- {
- private:
- //The text surface
- SDL_Surface *text;
- public:
- //The storage string
- std::string str;
- //Initializes variables
- StringInput();
- //Does clean up
- ~StringInput();
- //Handles input
- void handle_input();
- //Shows the message on screen
- void show_centered();
- };
- StringInput::StringInput()
- {
- //Initialize the string
- str = "";
- //Initialize the surface
- text = NULL;
- //Enable Unicode
- SDL_EnableUNICODE( SDL_ENABLE );
- }
- StringInput::~StringInput()
- {
- //Free text surface
- SDL_FreeSurface( text );
- //Disable Unicode
- SDL_EnableUNICODE( SDL_DISABLE );
- }
- void StringInput::handle_input()
- {
- //If a key was pressed
- if( event.type == SDL_KEYDOWN )
- {
- //Keep a copy of the current version of the string
- std::string temp = str;
- //If the string less than maximum size
- if( str.length() <= 8 )
- {
- //If the key is a number
- if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
- {
- //Append the character
- str += (char)event.key.keysym.unicode;
- }
- //If the key is a uppercase letter
- else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
- {
- //Append the character
- str += (char)event.key.keysym.unicode;
- }
- //If the key is a lowercase letter
- else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
- {
- //Append the character
- str += (char)event.key.keysym.unicode;
- }
- }
- //If backspace was pressed and the string isn't blank
- if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
- {
- //Remove a character from the end
- str.erase( str.length() - 1 );
- text = TTF_RenderText_Solid( Font, str.c_str(), TextColour);
- if ( str.length() == 1 ) ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- SDL_Flip(Screen);
- }
- //If the string was changed
- if( str != temp )
- {
- //Free the old surface
- SDL_FreeSurface( text );
- //Render a new text surface
- text = TTF_RenderText_Solid( Font, str.c_str(), TextColour);
- }
- }
- }
- void StringInput::show_centered()
- {
- //If the surface isn't blank
- if( text != NULL )
- {
- //Show the name
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- ApplySurface( ( ScreenWidth - text->w ) / 2, ( ScreenHeight - text->h ) / 2, text, Screen );
- SDL_Flip(Screen);
- }
- }
- int main (int argc, char* argv [])
- {
- /*Initiating and loading files*/
- if (Initiate() == false) return -1;
- if (LoadFiles() == false) return -1;
- SetClips();
- srand(time(NULL));
- std::stringstream High420Scores;
- SDL_Flip(Screen);
- while (Quit == false)
- {
- switch (State)
- {
- case SCORES:
- ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
- ApplySurface(0,0,LeHighScores,Screen);
- High420Scores << HName1;
- Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 10, Message, Screen );
- High420Scores.str("");
- High420Scores << "With " << HMoney1;
- Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 50, Message, Screen );
- High420Scores.str("");
- High420Scores << HName2;
- Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 100, Message, Screen );
- High420Scores.str("");
- High420Scores << "With " << HMoney2;
- Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 140, Message, Screen );
- SDL_Flip(Screen);
- High420Scores.str("");
- High420Scores << HName3;
- Message = TTF_RenderText_Solid(Font,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 190, Message, Screen );
- High420Scores.str("");
- High420Scores << "With " << HMoney3;
- Message = TTF_RenderText_Solid(FontS,High420Scores.str().c_str(),TextColourBlack);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, 230, Message, Screen );
- SDL_Flip(Screen);
- while ((Quit == false) && (State == SCORES))
- {
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_MOUSEMOTION)
- {
- x = event.motion.x;
- y = event.motion.y;
- if ((x > 20 ) && (x < 170) && (y > 280) && (y<300)) ApplySurface(20, 280 , ButtonsR, Screen, &ButtonClips [6]); //Cancel
- else ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
- SDL_Flip(Screen);
- }
- if (event.type == SDL_MOUSEBUTTONDOWN)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- if ((x > 20 ) && (x < 170) && (y > 280) && (y<300))
- {
- Applied = false;
- State = MAINMENU;
- break;
- }
- }
- }
- if (event.type == SDL_QUIT)
- {
- Quit = true;
- }
- }
- }
- break;
- case INTRO:
- Message = TTF_RenderText_Solid(FontS,"Abdul Ghani presents",TextColour);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
- Message = NULL;
- SDL_Flip(Screen);
- SDL_FreeSurface(Message);
- SDL_Delay(5000);
- SDL_Flip(Screen);
- Message = TTF_RenderText_Solid(FontS,"With art by Ben Whitelaw",TextColour);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h + 30 ) / 2, Message, Screen );
- SDL_Flip(Screen);
- SDL_Delay(5000);
- State = MAINMENU;
- break;
- case MAINMENU:
- if (MusicPlaying == false)
- {
- Mix_PlayMusic(MenuMusic,-1);
- MusicPlaying = true;
- }
- if (Applied == false)
- {
- ApplySurface(0,0,MenuScreen,Screen);
- ApplySurface(20,280,Buttons,Screen,&ButtonClips[0]);
- ApplySurface(480,280,Buttons,Screen,&ButtonClips[1]);
- Applied = true;
- }
- SDL_Flip(Screen);
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_MOUSEMOTION)
- {
- x = event.motion.x;
- y = event.motion.y;
- if ((x > 20) && (x < 170) && (y > 280) && (y < 300)) // New game
- {
- ApplySurface(20,280,ButtonsR,Screen,&ButtonClips[0]);
- }
- else ApplySurface(20,280,Buttons,Screen,&ButtonClips[0]);
- if (( x > 480) && ( x < 630) && (y > 280) && (y < 300)) // High Scores
- {
- ApplySurface(480,280,ButtonsR,Screen,&ButtonClips[1]);
- }
- else ApplySurface(480,280,Buttons,Screen,&ButtonClips[1]);
- }
- if (event.type == SDL_MOUSEBUTTONDOWN)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- x = event.button.x;
- y = event.button.y;
- if ((x > 20) && (x < 170) && (y > 280) && (y < 300)) // If left mouse down over new game button
- {
- ApplySurface(0,0,Splash,Screen);
- SDL_Flip(Screen);
- SDL_Delay(10000);
- Applied = false;
- Mix_HaltMusic();
- MusicPlaying = false;
- State = GAME;
- }
- if (( x > 480) && ( x < 630) && (y > 280) && (y < 300)) // If left mouse down over high scores button
- {
- State = SCORES;
- }
- if (event.type == SDL_QUIT) Quit = true;
- }
- }
- if (event.type == SDL_QUIT)
- {
- Quit = true;
- }
- }
- break;
- case GAME:
- if (MusicPlaying == false)
- {
- Mix_PlayMusic(Music,-1);
- MusicPlaying = true;
- }
- ApplySurface(0,0,Marketplace,Screen);
- SDL_Flip(Screen);
- while (Quit == false && State == GAME)
- {
- if (NumberGenerated == false)
- {
- GenerateNumbers();
- NumberGenerated = true;
- }
- if ((Money < LobsterPrice) && (Lobsters == 0) && (LobsterPots == 0))
- {
- ApplySurface(0,0,Black,Screen);
- Message = TTF_RenderText_Solid(FontS,"You ran out of things!",TextColour);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
- Message = TTF_RenderText_Solid(FontS,"You starved to death!",TextColour);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h + 30 ) / 2, Message, Screen );
- SDL_Flip(Screen);
- SDL_Delay(10000);
- Quit = true;
- }
- if (DaysG == 0)
- {
- bool NameEntered = false;
- StringInput Name;
- ApplySurface(0,0,Black,Screen);
- Money = Money + (LobsterPots * 5) + (Lobsters * LobsterPrice);
- std::stringstream ShitWilly;
- std::stringstream WillyShit;
- ShitWilly << "You got " << Money;
- WillyShit << "Your " << LobsterPots << " pots sold for " << LobsterPots * 5 << ", and your " << Lobsters << " lobsters sold for " << Lobsters * LobsterPrice << "!";
- Message = TTF_RenderText_Solid(FontS,ShitWilly.str().c_str(),TextColour);
- Message2 = TTF_RenderText_Solid(FontSS,WillyShit.str().c_str(),TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
- ApplySurface((ScreenWidth-Message2->w)/2,40,Message2,Screen);
- Message = TTF_RenderText_Solid(FontS,"What is your name?",TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,290,Message,Screen);
- SDL_Flip(Screen);
- while (NameEntered == false)
- {
- while (SDL_PollEvent(&event))
- {
- if (event.type == SDL_QUIT) Quit = true;
- Name.handle_input();
- Name.show_centered();
- if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN)
- {
- NameEntered = true;
- }
- }
- }
- if (Money > HMoney1)
- {
- Message = TTF_RenderText_Solid(FontS,"You beat the high score!",TextColour);
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
- SDL_Flip(Screen);
- HMoney3 = HMoney2;
- HName3 = HName2;
- HName2 = HName1;
- HMoney2 = HMoney1;
- HName1 = Name.str;
- HMoney1 = Money;
- }
- else if (Money > HMoney2)
- {
- Message = TTF_RenderText_Solid(FontS,"You came second!",TextColour);
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
- SDL_Flip(Screen);
- HName3 = HName2;
- HMoney3 = HMoney2;
- HName2 = Name.str;
- HMoney2 = Money;
- }
- else if (Money > HMoney3)
- {
- Message = TTF_RenderText_Solid(FontS,"You came third!",TextColour);
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- ApplySurface( ( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h - 30 ) / 2, Message, Screen );
- SDL_Flip(Screen);
- HName3 = Name.str;
- HMoney3 = Money;
- }
- std::ofstream AlmostDone;
- AlmostDone.open("HighScores.plsdntedit");
- AlmostDone << HName1 << std::endl << HMoney1 << std::endl << HName2 << std::endl << HMoney2 << std::endl << HName3 << std::endl << HMoney3;
- AlmostDone.close();
- SDL_Delay(10000);
- Quit = true;
- }
- SDL_Flip(Screen);
- if (Applied == false && Quit == false)
- {
- std::stringstream Days;
- std::stringstream MoneyStream;
- std::stringstream PotsLeft;
- std::stringstream LobstersLeft;
- Days << "Days left: " << DaysG;
- DaysText = TTF_RenderText_Solid(FontS,Days.str().c_str(),TextColourBlack);
- MoneyStream << "Money: " << Money << " pounds";
- MoneyText = TTF_RenderText_Solid(FontS,MoneyStream.str().c_str(),TextColourBlack);
- PotsLeft << "Pots: " << LobsterPots;
- PotsText = TTF_RenderText_Solid (FontS,PotsLeft.str().c_str(),TextColourBlack);
- LobstersLeft << "Lobsters: " << Lobsters;
- LobstersText = TTF_RenderText_Solid (FontS,LobstersLeft.str().c_str(),TextColourBlack);
- ApplySurface((ScreenWidth-DaysText->w) / 2, 70, DaysText,Screen);
- ApplySurface((ScreenWidth-MoneyText->w) / 2, 100, MoneyText,Screen);
- ApplySurface((ScreenWidth-PotsText->w) / 2, 130, PotsText,Screen);
- ApplySurface((ScreenWidth-LobstersText->w) / 2, 160, LobstersText,Screen);
- ApplySurface(5,280,Buttons,Screen,&ButtonClips[7]);
- ApplySurface(160,280,Buttons,Screen,&ButtonClips[4]);
- ApplySurface(315,280,Buttons,Screen,&ButtonClips[3]);
- ApplySurface(470,280,Buttons,Screen,&ButtonClips[2]);
- Applied = true;
- }
- SDL_Flip(Screen);
- while (SDL_PollEvent (&event))
- {
- if (event.type == SDL_QUIT) Quit = true;
- }
- if (event.type == SDL_MOUSEMOTION)
- {
- x = event.motion.x;
- y = event.motion.y;
- if ((x > 5) && (x < 155) && (y > 280) && (y < 300)) // Next Day
- {
- ApplySurface(5,280,ButtonsR,Screen,&ButtonClips[7]);
- }
- else ApplySurface(5,280,Buttons,Screen,&ButtonClips[7]);
- if ((x > 160) && (x < 310) && (y > 280) && (y < 300)) // Buy Pots
- {
- ApplySurface(160,280,ButtonsR,Screen,&ButtonClips[4]);
- }
- else ApplySurface(160,280,Buttons,Screen,&ButtonClips[4]);
- if (( x > 315) && ( x < 465) && (y > 280) && (y < 300)) // Sell Lobsters
- {
- ApplySurface(315,280,ButtonsR,Screen,&ButtonClips[3]);
- }
- else ApplySurface(315,280,Buttons,Screen,&ButtonClips[3]);
- if (( x > 470) && (x < 620) && (y > 280) && (y < 300)) // Weather
- {
- ApplySurface(470,280,ButtonsR,Screen,&ButtonClips[2]);
- }
- else ApplySurface(470,280,Buttons,Screen,&ButtonClips[2]);
- }
- if (event.type == SDL_MOUSEBUTTONDOWN)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- x = event.button.x;
- y = event.button.y;
- if ((x > 5) && (x < 155) && (y > 280) && (y < 300)) // Next Day
- {
- Applied = false;
- State = NEXTDAY;
- break;
- }
- if ((x > 160) && (x < 310) && (y > 280) && (y < 300)) // Buy Pots
- {
- Applied = false;
- State = BUYPOTS;
- break;
- }
- if (( x > 315) && ( x < 465) && (y > 280) && (y < 300)) // Sell Lobsters
- {
- Applied = false;
- State = SELLLOBSTERS;
- break;
- }
- if (( x > 470) && (x < 620) && (y > 280) && (y < 300)) // Weather
- {
- Applied = false;
- State = WEATHER;
- break;
- }
- }
- }
- }
- break;
- case NEXTDAY:
- ApplySurface(0,0,Black,Screen);
- SDL_Flip(Screen);
- while (Entered == false)
- {
- Message = TTF_RenderText_Solid(FontSS,"How many pots do you want to put inshore?",TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
- Inshore = GetNumberInput();
- if (Inshore > LobsterPots)
- {
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- Message2 = TTF_RenderText_Solid(FontS,"You don't have enough pots!",TextColour);
- ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
- SDL_Flip(Screen);
- SDL_Delay(2000);
- }
- else
- {
- Outshore = LobsterPots - Inshore;
- std::stringstream PlacedPots;
- PlacedPots << "You placed " << Inshore << " pots inshore and " << Outshore << " pots outshore.";
- Message = TTF_RenderText_Solid(FontSS,PlacedPots.str().c_str(),TextColour);
- ApplySurface(0,0,Black,Screen);
- ApplySurface ((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
- SDL_Flip(Screen);
- SDL_Delay(5000);
- Entered = true;
- }
- }
- if (YouFeelingLucky < Chance)
- {
- ApplySurface(0,0,Stormy,Screen);
- Message = TTF_RenderText_Solid(FontS,"It was stormy!",TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
- SDL_Flip(Screen);
- LobsterPots = LobsterPots - Outshore;
- Lobsters = Lobsters + Inshore;
- NumberGenerated = false;
- DaysG--;
- SDL_Delay(5000);
- State = GAME;
- }
- else
- {
- ApplySurface(0,0,Sunny,Screen);
- Message = TTF_RenderText_Solid(FontS,"It was sunny!",TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,(ScreenHeight-Message->h)/2,Message,Screen);
- SDL_Flip(Screen);
- Lobsters = Lobsters + Inshore + (Outshore * 2);
- NumberGenerated = false;
- DaysG--;
- SDL_Delay(5000);
- State = GAME;
- }
- while (Quit == false && State == NEXTDAY)
- {
- while (SDL_PollEvent(&event))
- {
- SDL_Flip(Screen);
- if (event.type == SDL_QUIT)
- {
- Quit = true;
- }
- }
- }
- break;
- case BUYPOTS:
- ApplySurface(0,0,Black,Screen);
- SDL_Flip(Screen);
- PotsHeader.str("");
- BuyPotsStream.str("");
- PotsHeader << "Today, pots cost " << PotPrice << " pounds. You have " << Money << " pounds.";
- BuyPotsStream << "Max amount of pots that you can buy:" << Money / PotPrice;
- Message = TTF_RenderText_Solid(FontSS,PotsHeader.str().c_str(),TextColour);
- ApplySurface((ScreenWidth-Message->w)/2,10,Message,Screen);
- Message2 = TTF_RenderText_Solid(FontSS,BuyPotsStream.str().c_str(),TextColour);
- ApplySurface((ScreenWidth-Message2->w)/2,40,Message2,Screen);
- SDL_Flip(Screen);
- while (Quit == false && State == BUYPOTS)
- {
- SDL_Flip(Screen);
- int Selection = GetNumberInput();
- if (Selection * PotPrice > Money)
- {
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- Message2 = TTF_RenderText_Solid(FontS,"You don't have enough money!",TextColour);
- ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
- SDL_Flip(Screen);
- SDL_Delay(2000);
- }
- else
- {
- LobsterPots = LobsterPots + Selection;
- Money = Money - (PotPrice * Selection);
- PotsHeader.str("");
- PotsHeader << "You bought " << Selection << " pots for " << PotPrice * Selection << " pounds!";
- Message2 = TTF_RenderText_Solid (FontSS,PotsHeader.str().c_str(),TextColour);
- ApplySurface (0,0,Black,Screen);
- ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
- SDL_Flip(Screen);
- SDL_Delay(2000);
- State = GAME;
- }
- }
- break;
- case SELLLOBSTERS:
- ApplySurface(0,0,Black,Screen);
- LobstersHeader.str("");
- LobstersHeader << "Today, lobsters sell at " << LobsterPrice <<".";
- Message = TTF_RenderText_Solid(FontSS,LobstersHeader.str().c_str(),TextColour);
- ApplySurface((ScreenWidth - Message->w) / 2,10,Message,Screen);
- LobstersHeader.str("");
- LobstersHeader << "How many lobsters would you like to sell?";
- Message = TTF_RenderText_Solid(FontSS,LobstersHeader.str().c_str(),TextColour);
- ApplySurface((ScreenWidth - Message->w) / 2,40,Message,Screen);
- SDL_Flip(Screen);
- Selling = GetNumberInput();
- if (Selling > Lobsters)
- {
- ApplySurface ((ScreenWidth-BlackRectangle->w)/2,(ScreenHeight-BlackRectangle->h)/2,BlackRectangle,Screen);
- Message2 = TTF_RenderText_Solid(FontS,"You don't have that many!",TextColour);
- ApplySurface ((ScreenWidth-Message2->w)/2,(ScreenHeight-Message2->h)/2,Message2,Screen);
- SDL_Flip(Screen);
- SDL_Delay(2000);
- }
- else
- {
- ApplySurface(0,0,Black,Screen);
- Lobsters = Lobsters - Selling;
- Money = Money + (LobsterPrice * Selling);
- LobstersHeader.str("");
- LobstersHeader << "You sold " << Selling << " lobsters for " << Selling * LobsterPrice << " pounds!";
- Message2 = TTF_RenderText_Solid (FontSS,LobstersHeader.str().c_str(),TextColour);
- ApplySurface(( ScreenWidth - Message2->w ) / 2, ( ScreenHeight - Message2->h )/ 2,Message2,Screen);
- SDL_Flip(Screen);
- SDL_Delay(5000);
- State = GAME;
- }
- break;
- case WEATHER:
- ApplySurface(0,0,Black,Screen);
- std::stringstream StormChance;
- StormChance << "There is a " << Chance << "% of a storm tonight.";
- Message = TTF_RenderText_Solid(FontSS,StormChance.str().c_str(),TextColour);
- ApplySurface(( ScreenWidth - Message->w ) / 2, ( ScreenHeight - Message->h )/ 2,Message,Screen);
- ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
- SDL_Flip(Screen);
- while (Quit == false && State == WEATHER)
- {
- while (SDL_PollEvent(&event))
- {
- SDL_Flip(Screen);
- if (event.type == SDL_QUIT)
- {
- Quit = true;
- }
- if (event.type == SDL_MOUSEMOTION)
- {
- x = event.motion.x;
- y = event.motion.y;
- if ((x > 20 ) && (x < 170) && (y > 280) && (y<300)) ApplySurface(20, 280 , ButtonsR, Screen, &ButtonClips [6]); //Cancel
- else ApplySurface(20, 280 , Buttons, Screen, &ButtonClips [6]);
- }
- if (event.type == SDL_MOUSEBUTTONDOWN)
- {
- if (event.button.button == SDL_BUTTON_LEFT)
- {
- if ((x > 20 ) && (x < 170) && (y > 280) && (y<300))
- {
- SDL_Delay(500);
- State = GAME;
- break;
- }
- }
- }
- }
- }
- }
- }
- CleanUp();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment