Advertisement
Guest User

C++ SFML 2.1 Primitive Pong

a guest
Nov 12th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.82 KB | None | 0 0
  1. ==============================================.HPP FILE===========================================================
  2. #pragma once
  3. //C++ Headers
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <ctime>
  8. #include <cstdlib>
  9.  
  10. #include <vector>
  11. #include <cmath>
  12. #include <algorithm>
  13. //Library Headers
  14. #include <SFML\Graphics.hpp>
  15. #include <SFML\Window.hpp>
  16. #include <SFML\Audio.hpp>
  17. #include <SFML\Network.hpp>
  18. #include <SFML\System.hpp>
  19.  
  20. const int Width = 800;
  21. const int Height = 600;
  22.  
  23. using namespace std;
  24.  
  25. ============================================.CPP FILE=============================================================
  26. //Class Headers
  27. #include "Globals.hpp"
  28.  
  29. bool Collided(const sf::Shape & s1, const sf::Shape & s2);
  30.  
  31. void ResetBall(sf::CircleShape & s1, float & a, float & b, sf::Vector2f & Pos);
  32. void ChangeBallDir(sf::CircleShape & s1, const float & speed, float& dirX, float & dirY);
  33.  
  34. void EnemyAi(sf::RectangleShape & r1, sf::CircleShape c1, float & a, const float & speed, int & difficulty);
  35.  
  36. void PlayerMovement(sf::Shape &PlayerShape);
  37.  
  38. void StringChange(sf::Text & t1, int & a);
  39. void StringChangeDif(sf::Text & t1, int & a);
  40. void DrawText(sf::Font &Font, sf::Text &text, int &Size, sf::Vector2f &Pos, const sf::Color &Color, sf::String &str, sf::RenderWindow &rw);
  41.  
  42. void Reset(sf::Shape & s1, sf::Shape & s2, sf::Shape & s3, sf::Vector2f & v1, sf::Vector2f & v2, sf::Vector2f & v3, int & x, int & y);
  43. void Save();
  44.  
  45. //Start of main
  46. int main()
  47. {
  48.         //Window Objects
  49.         sf::RenderWindow Window(sf::VideoMode(Width, Height), "SFML Window");
  50.         Window.setKeyRepeatEnabled(false);
  51.         Window.setFramerateLimit(60);
  52.         //Init
  53.  
  54.         //Misc things
  55.         enum State{MainMenu, Options, Playing, Paused, EndScreen, Quit};
  56.  
  57.         sf::Font Font; if(!Font.loadFromFile("arial.ttf")) cout << "Could not load font from file!\n";
  58.  
  59.         int f = 32;
  60.         int MaxScore = 10;
  61.  
  62.         sf::Text ModifiableText;
  63.  
  64.         //Game "Board" Init
  65.         sf::RectangleShape BoardMid;
  66.         BoardMid.setSize(sf::Vector2f(20, 600));
  67.         BoardMid.setFillColor(sf::Color(220,220,220,255));
  68.         BoardMid.setPosition(Width/2, 0);
  69.  
  70.         sf::CircleShape BoardCircle;
  71.         BoardCircle.setRadius(180);
  72.         BoardCircle.setFillColor(sf::Color::Transparent);
  73.         BoardCircle.setOutlineColor(sf::Color(220,220,220,255));
  74.         BoardCircle.setOutlineThickness(12);
  75.         BoardCircle.setPosition(225, 130);
  76.  
  77.         //Rand
  78.         srand(time(NULL));
  79.  
  80.         //Player Init
  81.         sf::Text pText;
  82.         pText.setFont(Font);
  83.         pText.setCharacterSize(32);
  84.         pText.setColor(sf::Color::Blue);
  85.         pText.setPosition(Width/2 - 20, 20);
  86.  
  87.         sf::RectangleShape PlayerShape;
  88.         sf::Vector2f Pos;
  89.         PlayerShape.setFillColor(sf::Color::Blue);
  90.         PlayerShape.setPosition(Pos.x = 10, Pos.y = Height/2 - 64);
  91.         PlayerShape.setSize(sf::Vector2f(32, 128));
  92.  
  93.         int PlayerScore = 0;
  94.  
  95.         //Enemy Init
  96.         sf::Text eText;
  97.         eText.setFont(Font);
  98.         eText.setCharacterSize(32);
  99.         eText.setColor(sf::Color::Red);
  100.         eText.setPosition(Width/2 + 20, 20);
  101.  
  102.         sf::RectangleShape EnemyShape;
  103.         sf::Vector2f ePos;
  104.         EnemyShape.setFillColor(sf::Color::Red);
  105.         EnemyShape.setPosition(ePos.x = Width - 42, ePos.y = Height/2 - 64);
  106.         EnemyShape.setSize(sf::Vector2f(32, 128));
  107.         float eDirX = 0, eDirY = 1;
  108.  
  109.         int EnemyScore = 0;
  110.  
  111.         //Ball Init
  112.         sf::CircleShape Ball;
  113.         sf::Vector2f bPos;
  114.         Ball.setFillColor(sf::Color::Green);
  115.         Ball.setPosition(bPos.x = Width/2, bPos.y = Height/2);
  116.         Ball.setRadius(16);
  117.  
  118.         float dirX = -1, dirY = 0;
  119.         const float speed = 5;
  120.         float k = 0.5f;
  121.         //End of Init
  122.  
  123.         enum Difficulty {Cake, Easy, Medium, Hard, Impossible};
  124.         State state = MainMenu;
  125.  
  126.         int difficulty = Easy;
  127.  
  128.         int MaxScoreChange = 1;
  129.  
  130.         while (Window.isOpen())
  131.         {
  132.                 while(state == MainMenu)
  133.                 {
  134.                         sf::Vector2i MousePosition = sf::Mouse::getPosition(Window);
  135.  
  136.                         sf::Text Text;
  137.                         sf::Text Text2;
  138.  
  139.                         DrawText(Font, Text, f, sf::Vector2f(Width/2 - Text.getCharacterSize() * Text.getScale().x, Height/2 - Text.getCharacterSize() * Text.getScale().y),
  140.                                 sf::Color::White, sf::String("Play!"), Window);
  141.                         DrawText(Font, Text2, f, sf::Vector2f(Width/2 - Text2.getCharacterSize() * Text2.getScale().x, Height/2 - Text2.getCharacterSize() * Text2.getScale().y + Text.getGlobalBounds().height + 20),
  142.                                 sf::Color::White, sf::String("Options!"), Window);
  143.  
  144.                         //cout << Text.getPosition().x << " " << Text.getPosition().y << endl;
  145.  
  146.                         sf::Event Event;
  147.                         while(Window.pollEvent(Event))
  148.                         {
  149.                                 switch(Event.type)
  150.                                 {
  151.                                 case sf::Event::Closed:
  152.                                         Window.close();
  153.                                         break;
  154.                                 case sf::Event::KeyPressed:
  155.                                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  156.                                                 state = Quit;
  157.                                         break;
  158.                                 }//end of switch Event.type
  159.                         }//end of window.pollEvent
  160.  
  161.                         //start of mouse bounds checking
  162.                         if(MousePosition.x >= Text.getPosition().x - 10 && MousePosition.x <= Text.getPosition().x + Text.getGlobalBounds().width + 10 && //x
  163.                                 MousePosition.y >= Text.getPosition().y - 10 && MousePosition.y <= Text.getPosition().y + Text.getGlobalBounds().height + 5) //y
  164.                         {
  165.                                 Text.setColor(sf::Color::Red);
  166.  
  167.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  168.                                 {
  169.                                         state = Playing;
  170.                                 }//end of if mouse button is pressed
  171.                         }//end of mouse bounds checking
  172.  
  173.                         //start of mouse bounds checking
  174.                         if(MousePosition.x >= Text2.getPosition().x - 10 && MousePosition.x <= Text2.getPosition().x + Text2.getGlobalBounds().width + 10 && //x
  175.                                 MousePosition.y >= Text2.getPosition().y - 10 && MousePosition.y <= Text2.getPosition().y + Text2.getGlobalBounds().height + 10) //y
  176.                         {
  177.                                 Text2.setColor(sf::Color::Red);
  178.  
  179.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  180.                                 {
  181.                                         state = Options;
  182.                                 }//end of if mouse button is pressed
  183.  
  184.                         }//end of mouse bounds checking
  185.  
  186.  
  187.                         Window.clear();
  188.  
  189.                         Window.draw(Text);
  190.                         Window.draw(Text2);
  191.  
  192.                         Window.display();
  193.                 }
  194.                 //end of While state == MainMenu
  195.                 while(state == Options)
  196.                 {
  197.                         sf::Vector2i MousePosition = sf::Mouse::getPosition(Window);
  198.  
  199.                         sf::Text OT1;
  200.                         sf::Text t1, t2, t3, t4;
  201.  
  202.                         t2.setFont(Font);
  203.                         t2.setCharacterSize(32);
  204.                         t2.setPosition(sf::Vector2f(Width/2 + 160, Height - 100));
  205.                         t2.setColor(sf::Color::White);
  206.                         t4.setFont(Font);
  207.                         t4.setCharacterSize(32);
  208.                         t4.setPosition(sf::Vector2f(Width/2 + 140, Height - 150));
  209.                         t4.setColor(sf::Color::White);
  210.  
  211.                         DrawText(Font, OT1, f, sf::Vector2f(Width/2, Height - 50), sf::Color::White, sf::String("Back"), Window);
  212.                         DrawText(Font, t1, f, sf::Vector2f(Width/2, Height - 100), sf::Color::White, sf::String("Max Score: "), Window);
  213.                         DrawText(Font, t3, f, sf::Vector2f(Width/2, Height - 150), sf::Color::White, sf::String("Difficulty: "), Window);
  214.  
  215.                         sf::Event Event;
  216.                         while(Window.pollEvent(Event))
  217.                         {
  218.                                 switch(Event.type)
  219.                                 {
  220.                                 case sf::Event::Closed:
  221.                                         Window.close();
  222.                                         break;
  223.                                 case sf::Event::KeyPressed:
  224.                                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  225.                                                 state = Paused;
  226.                                         break;
  227.                                 }//end of switch Event.type
  228.                         }//end of window.pollEvent
  229.  
  230.                         //start of mouse bounds checking
  231.                         if(MousePosition.x >= OT1.getPosition().x - 10 && MousePosition.x <= OT1.getPosition().x + OT1.getGlobalBounds().width + 10 && //x
  232.                                 MousePosition.y >= OT1.getPosition().y - 10 && MousePosition.y <= OT1.getPosition().y + OT1.getGlobalBounds().height + 10) //y
  233.                         {
  234.                                 OT1.setColor(sf::Color::Red);
  235.  
  236.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  237.                                 {
  238.                                         state = MainMenu;
  239.                                 }//end of if mouse button is pressed
  240.                         }
  241.  
  242.                         if(MousePosition.x >= t1.getPosition().x - 10 && MousePosition.x <= t1.getPosition().x + t1.getGlobalBounds().width + 10 && //x
  243.                                 MousePosition.y >= t1.getPosition().y - 10 && MousePosition.y <= t1.getPosition().y + t1.getGlobalBounds().height + 20) //y
  244.                         {
  245.                                 t1.setColor(sf::Color::Red);
  246.  
  247.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  248.                                 {
  249.                                         if(MaxScoreChange != 20)
  250.                                                 MaxScoreChange++;
  251.  
  252.                                         StringChange(t2, MaxScoreChange);
  253.                                         MaxScore = MaxScoreChange;
  254.  
  255.                                 }//end of if mouse button is pressed
  256.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
  257.                                 {
  258.                                         if(MaxScoreChange != 1)
  259.                                                 MaxScoreChange--;
  260.  
  261.                                         StringChange(t2, MaxScoreChange);
  262.                                         MaxScore = MaxScoreChange;
  263.                                 }//end of if mouse button is pressed
  264.  
  265.                         }
  266.  
  267.                         if(MousePosition.x >= t3.getPosition().x - 10 && MousePosition.x <= t3.getPosition().x + t3.getGlobalBounds().width + 10 && //x
  268.                                 MousePosition.y >= t3.getPosition().y - 10 && MousePosition.y <= t3.getPosition().y + t3.getGlobalBounds().height + 10) //y
  269.                         {
  270.                                 t3.setColor(sf::Color::Red);
  271.  
  272.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  273.                                 {
  274.                                         if(difficulty != 4)
  275.                                                 ++difficulty;
  276.                                 }//end of if mouse button is pressed
  277.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
  278.                                 {
  279.                                         if(difficulty != 0)
  280.                                                 --difficulty;
  281.                                 }//end of if mouse button is pressed
  282.                         }//end of mouse bounds checking
  283.  
  284.                         StringChange(t2, MaxScoreChange);
  285.                         StringChangeDif(t4, difficulty);
  286.  
  287.                         Window.clear();
  288.  
  289.                         Window.draw(OT1);
  290.                         Window.draw(t1);
  291.                         Window.draw(t2);
  292.                         Window.draw(t3);
  293.                         Window.draw(t4);
  294.  
  295.                         Window.display();
  296.                 }
  297.                 //End of while state == Options
  298.                 while(state == Playing)
  299.                 {
  300.                         sf::Event Event;
  301.                         while(Window.pollEvent(Event))
  302.                         {
  303.                                 switch(Event.type)
  304.                                 {
  305.                                 case sf::Event::Closed:
  306.                                         Window.close();
  307.                                         break;
  308.                                 case sf::Event::KeyPressed:
  309.                                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  310.                                                 state = Paused;
  311.                                         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::K))
  312.                                                 ResetBall(Ball, dirX, dirY, bPos);
  313.                                         break;
  314.                                 }//end of switch Event.type
  315.                         }//end of window.pollEvent
  316.  
  317.                         //Update
  318.                         PlayerMovement(PlayerShape);
  319.                         EnemyAi(EnemyShape, Ball, eDirY, speed, difficulty);
  320.                         Ball.move(dirX, dirY);
  321.  
  322.                         //Keeping Images in bounds
  323.                         if(PlayerShape.getPosition().y <= 0)
  324.                                 PlayerShape.setPosition(10, 0);
  325.  
  326.                         if(PlayerShape.getPosition().y >= Height - PlayerShape.getSize().y)
  327.                                 PlayerShape.setPosition(10, Height - PlayerShape.getSize().y);
  328.  
  329.                         if(EnemyShape.getPosition().y <= 0)
  330.                                 EnemyShape.setPosition(Width - 42, 0);
  331.  
  332.                         if(EnemyShape.getPosition().y >= Height - EnemyShape.getSize().y)
  333.                                 EnemyShape.setPosition(Width - 42, Height - EnemyShape.getSize().y);
  334.  
  335.                         if(Ball.getPosition().y <= 0)
  336.                                 dirY = -dirY;
  337.  
  338.                         if(Ball.getPosition().y >= Height - 32)
  339.                                 dirY = -dirY;
  340.  
  341.                         //Collision Detection
  342.                         if(Collided(PlayerShape, Ball))
  343.                         {
  344.                                 dirX = -dirX;
  345.                                 //dirY = -dirY - (float)rand()/(float)RAND_MAX;
  346.  
  347.                                 //dirX = cos(PlayerShape.getPosition().x) * speed + k * PlayerShape.getPosition().x;
  348.                                 //dirX = sin(PlayerShape.getPosition().x) * 2;
  349.                                 //dirX = atan2(PlayerShape.getPosition().y, PlayerShape.getPosition().x) * 3;
  350.  
  351.                                 dirY = cos(PlayerShape.getPosition().y);
  352.                                 //dirY = sin(PlayerShape.getPosition().y);
  353.                                 //dirY = atan2(PlayerShape.getPosition().x, PlayerShape.getPosition().y);
  354.                         }
  355.  
  356.                         else if(Collided(EnemyShape, Ball))
  357.                         {
  358.                                 dirX = -dirX;
  359.                                 //dirY = -dirY - (float)rand()/(float)RAND_MAX;
  360.  
  361.                                 //dirX = cos(-EnemyShape.getPosition().x);
  362.  
  363.                                 //dirX = -cos(EnemyShape.getPosition().x) * 10;
  364.                                 //dirX = -sin(EnemyShape.getPosition().x) * 10;
  365.                                 //dirX = -atan2(EnemyShape.getPosition().y, EnemyShape.getPosition().x) * 10;
  366.  
  367.                                 dirY = -cos(-EnemyShape.getPosition().y);
  368.                                 //dirY = -sin(EnemyShape.getPosition().y);
  369.                                 //dirY = -atan2(EnemyShape.getPosition().y, EnemyShape.getPosition().x); // doesnt work well
  370.                         }
  371.                         //Update
  372.                         StringChange(pText, PlayerScore);
  373.                         StringChange(eText, EnemyScore);
  374.                         EnemyAi(EnemyShape, Ball, eDirY, speed, difficulty);
  375.  
  376.                         //Points
  377.                         if(Ball.getPosition().x < 0 - 32)
  378.                         {
  379.                                 EnemyScore++;
  380.                                 ResetBall(Ball, dirX, dirY, bPos);
  381.                                 ChangeBallDir(Ball, speed, dirX, dirY);
  382.                                 dirX = -dirX + 0.2f;
  383.                                 dirY = -dirY + 0.2f;
  384.                         }
  385.                         else if(Ball.getPosition().x > Width)
  386.                         {
  387.                                 PlayerScore++;
  388.                                 ResetBall(Ball, dirX, dirY, bPos);
  389.                                 ChangeBallDir(Ball, speed, dirX, dirY);
  390.                                 dirX = -dirX + 0.2f;
  391.                                 dirY = -dirY + 0.2f;
  392.                         }
  393.  
  394.                         Ball.move(dirX * speed, dirY * speed);
  395.  
  396.                         if(PlayerScore >= MaxScore)
  397.                                 state = EndScreen;
  398.                         if(EnemyScore >= MaxScore)
  399.                                 state = EndScreen;
  400.  
  401.                         Window.clear();
  402.  
  403.                         Window.draw(pText);
  404.                         Window.draw(eText);
  405.  
  406.                         Window.draw(BoardCircle);
  407.                         Window.draw(BoardMid);
  408.  
  409.                         Window.draw(PlayerShape);
  410.                         Window.draw(EnemyShape);
  411.                         Window.draw(Ball);
  412.  
  413.                         Window.display();
  414.                 }
  415.                 //end of while state == Playing
  416.                 while(state == Paused)
  417.                 {
  418.                         sf::Text ModifiableText;
  419.                         sf::Text ModifiableText2;
  420.                         sf::Text ModifiableText3;
  421.  
  422.                         DrawText(Font, ModifiableText, f, sf::Vector2f(Width/2, Height/2 - 100), sf::Color::White, sf::String("Resume"), Window);
  423.                         DrawText(Font, ModifiableText2, f, sf::Vector2f(Width/2, Height/2 - 50), sf::Color::White, sf::String("Options"), Window);
  424.                         DrawText(Font, ModifiableText3, f, sf::Vector2f(Width/2, Height/2), sf::Color::White, sf::String("Quit"), Window);
  425.  
  426.                         sf::Event Event;
  427.                         while(Window.pollEvent(Event))
  428.                         {
  429.                                 switch(Event.type)
  430.                                 {
  431.                                 case sf::Event::Closed:
  432.                                         Window.close();
  433.                                         break;
  434.                                 case sf::Event::KeyPressed:
  435.                                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  436.                                                 state = Playing;
  437.                                 }//end of switch Event.type
  438.                         }//end of window.pollEvent
  439.  
  440.                         sf::Vector2i MousePosition = sf::Mouse::getPosition(Window);
  441.  
  442.                         //start of mouse bounds checking
  443.                         if(MousePosition.x >= ModifiableText.getPosition().x - 10 && MousePosition.x <= ModifiableText.getPosition().x + ModifiableText.getGlobalBounds().width + 10 && //x
  444.                                 MousePosition.y >= ModifiableText.getPosition().y - 10 && MousePosition.y <= ModifiableText.getPosition().y + ModifiableText.getGlobalBounds().height + 10) //y
  445.                         {
  446.                                 ModifiableText.setColor(sf::Color::Red);
  447.  
  448.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  449.                                 {
  450.                                         state = Playing;
  451.                                 }//end of if mouse button is pressed
  452.  
  453.                         }//end of mouse bounds checking
  454.  
  455.                         //start of mouse bounds checking
  456.                         if(MousePosition.x >= ModifiableText2.getPosition().x - 10 && MousePosition.x <= ModifiableText2.getPosition().x + ModifiableText2.getGlobalBounds().width + 10 && //x
  457.                                 MousePosition.y >= ModifiableText2.getPosition().y - 10 && MousePosition.y <= ModifiableText2.getPosition().y + ModifiableText2.getGlobalBounds().height + 10) //y
  458.                         {
  459.                                 ModifiableText2.setColor(sf::Color::Red);
  460.  
  461.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  462.                                 {
  463.                                         state = Options;
  464.                                 }//end of if mouse button is pressed
  465.  
  466.                         }//end of mouse bounds checking
  467.  
  468.                         if(MousePosition.x >= ModifiableText3.getPosition().x - 10 && MousePosition.x <= ModifiableText3.getPosition().x + ModifiableText3.getGlobalBounds().width + 10 && //x
  469.                                 MousePosition.y >= ModifiableText3.getPosition().y - 10 && MousePosition.y <= ModifiableText3.getPosition().y + ModifiableText3.getGlobalBounds().height + 10) //y
  470.                         {
  471.                                 ModifiableText3.setColor(sf::Color::Red);
  472.  
  473.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  474.                                 {
  475.                                         state = Quit;
  476.                                 }//end of if mouse button is pressed
  477.  
  478.                         }//end of mouse bounds checking
  479.  
  480.                         Window.clear();
  481.  
  482.                         Window.draw(ModifiableText);
  483.                         Window.draw(ModifiableText2);
  484.                         Window.draw(ModifiableText3);
  485.  
  486.                         Window.display();
  487.                 }
  488.                 //end of while state == Paused
  489.                 while (state == EndScreen)
  490.                 {
  491.                         sf::Vector2i MousePosition = sf::Mouse::getPosition(Window);
  492.  
  493.                         sf::Text t1, t2, Restart, Exit;
  494.  
  495.                         if(PlayerScore >= MaxScore)
  496.                                 DrawText(Font, t1, f, sf::Vector2f(100, Height/2), sf::Color::Green, sf::String("PLAYER WINS!"), Window);
  497.                         if(EnemyScore >= MaxScore)
  498.                                 DrawText(Font, t2, f, sf::Vector2f(100, Height/2), sf::Color::Red, sf::String("ENEMY WINS!"), Window);
  499.  
  500.                         DrawText(Font, Restart, f, sf::Vector2f(50, Height-100), sf::Color::White, sf::String("Play Again?"), Window);
  501.                         DrawText(Font, Exit, f, sf::Vector2f(700, Height-100), sf::Color::White, sf::String("Quit!"), Window);
  502.  
  503.                         //start of mouse bounds checking
  504.                         if(MousePosition.x >= Restart.getPosition().x - 10 && MousePosition.x <= Restart.getPosition().x + Restart.getGlobalBounds().width + 10 && //x
  505.                                 MousePosition.y >= Restart.getPosition().y - 10 && MousePosition.y <= Restart.getPosition().y + Restart.getGlobalBounds().height + 20) //y
  506.                         {
  507.                                 Restart.setColor(sf::Color::Red);
  508.  
  509.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  510.                                 {
  511.                                         Reset(PlayerShape, EnemyShape, Ball, Pos, ePos, bPos, PlayerScore, EnemyScore);
  512.                                         state = MainMenu;
  513.                                 }//end of if mouse button is pressed
  514.  
  515.                         }//end of mouse bounds checking
  516.  
  517.                         if(MousePosition.x >= Exit.getPosition().x - 10 && MousePosition.x <= Exit.getPosition().x + Exit.getGlobalBounds().width + 10 && //x
  518.                                 MousePosition.y >= Exit.getPosition().y - 10 && MousePosition.y <= Exit.getPosition().y + Exit.getGlobalBounds().height + 20) //y
  519.                         {
  520.                                 Exit.setColor(sf::Color::Red);
  521.  
  522.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  523.                                 {
  524.                                         state = Quit;
  525.                                 }//end of if mouse button is pressed
  526.  
  527.                         }//end of mouse bounds checking
  528.  
  529.                         Window.clear();
  530.  
  531.                         Window.draw(t1);
  532.                         Window.draw(t2);
  533.  
  534.                         Window.draw(Restart);
  535.                         Window.draw(Exit);
  536.  
  537.                         Window.display();
  538.                 }
  539.                 //end of while state == EndScreen
  540.                 while (state == Quit)
  541.                 {
  542.                         sf::Text EndText;
  543.                         sf::Text Yes;
  544.                         sf::Text No;
  545.  
  546.                         DrawText(Font, EndText, f, sf::Vector2f(Width/2 - 100, Height/2 - 50), sf::Color::White, sf::String("Are you sure you want to quit?!"), Window);
  547.                         DrawText(Font, Yes, f, sf::Vector2f(Width/2 - 100, Height/2), sf::Color::White, sf::String("Yes"), Window);
  548.                         DrawText(Font, No, f, sf::Vector2f(Width/2, Height/2), sf::Color::White, sf::String("No"), Window);
  549.  
  550.                         sf::Event Event;
  551.                         while(Window.pollEvent(Event))
  552.                         {
  553.                                 switch(Event.type)
  554.                                 {
  555.                                 case sf::Event::Closed:
  556.                                         Window.close();
  557.                                         break;
  558.                                 case sf::Event::KeyPressed:
  559.                                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  560.                                                 Window.close();
  561.                                         break;
  562.                                 }//end of switch Event.type
  563.                         }//end of window.pollEvent
  564.                         //Update
  565.  
  566.                         sf::Vector2i MousePosition = sf::Mouse::getPosition(Window);
  567.                         //Start of mouse bounds checking
  568.                         if(MousePosition.x >= Yes.getPosition().x - 10 && MousePosition.x <= Yes.getPosition().x + Yes.getGlobalBounds().width + 10 && //x
  569.                                 MousePosition.y >= Yes.getPosition().y - 10 && MousePosition.y <= Yes.getPosition().y + Yes.getGlobalBounds().height + 20) //y
  570.                         {
  571.                                 Yes.setColor(sf::Color::Red);
  572.  
  573.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  574.                                 {
  575.                                         Window.close();
  576.                                 }//end of if mouse button is pressed
  577.                         }
  578.  
  579.                         if(MousePosition.x >= No.getPosition().x - 10 && MousePosition.x <= No.getPosition().x + No.getGlobalBounds().width + 10 && //x
  580.                                 MousePosition.y >= No.getPosition().y - 10 && MousePosition.y <= No.getPosition().y + No.getGlobalBounds().height + 20) //y
  581.                         {
  582.                                 No.setColor(sf::Color::Red);
  583.  
  584.                                 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  585.                                 {
  586.                                         state = Paused;
  587.                                 }//end of if mouse button is pressed
  588.  
  589.                         }//end of mouse bounds checking
  590.  
  591.                         Window.clear();
  592.  
  593.                         Window.draw(EndText);
  594.                         Window.draw(Yes);
  595.                         Window.draw(No);
  596.  
  597.                         Window.display();
  598.                 }
  599.                 //end of state == Quit
  600.  
  601.         }//end of while Window.isOpen()
  602.         return 0;
  603. }//End of main
  604.  
  605. //Methods/Functions
  606.  
  607. void PlayerMovement(sf::Shape &PlayerShape)
  608. {
  609.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  610.         {
  611.                 PlayerShape.move(0, -5);
  612.         }
  613.         else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  614.         {
  615.                 PlayerShape.move(0, 5);
  616.         }
  617. }
  618.  
  619. bool Collided(const sf::Shape & s1, const sf::Shape & s2)
  620. {
  621.         sf::FloatRect r1 = s1.getGlobalBounds();
  622.         sf::FloatRect r2 = s2.getGlobalBounds();
  623.         return r1.intersects(r2);
  624. }
  625.  
  626. void ResetBall(sf::CircleShape & s1, float & a, float & b, sf::Vector2f & Pos)
  627. {
  628.         s1.setPosition(Pos);
  629.         a = 1, b = 1;
  630. }
  631.  
  632. void ChangeBallDir(sf::CircleShape & s1, const float & speed, float & dirX, float & dirY)
  633. {
  634.         dirX = -dirX;
  635.         dirY = -dirY;
  636.         //s1.move(dirX * speed, dirY * speed);
  637. }
  638.  
  639. void StringChange(sf::Text & t1, int & a)
  640. {
  641.         switch(a)
  642.         {
  643.         case 0:
  644.                 t1.setString("0");
  645.                 break;
  646.         case 1:
  647.                 t1.setString("1");
  648.                 break;
  649.         case 2:
  650.                 t1.setString("2");
  651.                 break;
  652.         case 3:
  653.                 t1.setString("3");
  654.                 break;
  655.         case 4:
  656.                 t1.setString("4");
  657.                 break;
  658.         case 5:
  659.                 t1.setString("5");
  660.                 break;
  661.         case 6:
  662.                 t1.setString("6");
  663.                 break;
  664.         case 7:
  665.                 t1.setString("7");
  666.                 break;
  667.         case 8:
  668.                 t1.setString("8");
  669.                 break;
  670.         case 9:
  671.                 t1.setString("9");
  672.                 break;
  673.         case 10:
  674.                 t1.setString("10");
  675.                 break;
  676.         case 11:
  677.                 t1.setString("11");
  678.                 break;
  679.         case 12:
  680.                 t1.setString("12");
  681.                 break;
  682.         case 13:
  683.                 t1.setString("13");
  684.                 break;
  685.         case 14:
  686.                 t1.setString("14");
  687.                 break;
  688.         case 15:
  689.                 t1.setString("15");
  690.                 break;
  691.         case 16:
  692.                 t1.setString("16");
  693.                 break;
  694.         case 17:
  695.                 t1.setString("17");
  696.                 break;
  697.         case 18:
  698.                 t1.setString("18");
  699.                 break;
  700.         case 19:
  701.                 t1.setString("19");
  702.                 break;
  703.         case 20:
  704.                 t1.setString("20");
  705.                 break;
  706.         }//end of switch
  707.  
  708. }//end of void SwitchString
  709. void StringChangeDif(sf::Text & t1, int & a)
  710. {
  711.         switch(a)
  712.         {
  713.         case 0:
  714.                 t1.setString("Cake");
  715.                 break;
  716.         case 1:
  717.                 t1.setString("Easy");
  718.                 break;
  719.         case 2:
  720.                 t1.setString("Medium");
  721.                 break;
  722.         case 3:
  723.                 t1.setString("Hard");
  724.                 break;
  725.         case 4:
  726.                 t1.setString("Impossible");
  727.                 break;
  728.         }
  729. }
  730.  
  731. void EnemyAi(sf::RectangleShape & r1, sf::CircleShape c1, float & a, const float & speed, int & difficulty)
  732. {
  733.         if(difficulty == 0)
  734.         {
  735.                 a = 0.2f;
  736.         }
  737.         else if(difficulty == 1)
  738.         {
  739.                 a = 0.4f;
  740.         }
  741.         else if(difficulty == 2)
  742.         {
  743.                 a = 0.5f;
  744.         }
  745.         else if(difficulty == 3)
  746.         {
  747.                 a = 0.6f;
  748.         }
  749.         else if(difficulty == 3)
  750.         {
  751.                 a = 1.0f;
  752.         }
  753.         if(c1.getPosition().y < r1.getPosition().y)
  754.         {
  755.                 r1.move(0, -a * speed);
  756.         }
  757.         else if(c1.getPosition().y > r1.getPosition().y)
  758.         {  
  759.                 r1.move(0, a * speed);
  760.         }
  761.         //so if the ball goes lower than the enemy can the enemy wont be all buggy and weird about moving in and out of the bounds its allowed
  762.         else if(c1.getPosition().y > Height - 96)
  763.         {
  764.                 r1.move(0,-1);
  765.         }
  766. }
  767.  
  768. void DrawText(sf::Font &Font, sf::Text &text, int &Size, sf::Vector2f &Pos, const sf::Color &Color, sf::String &str, sf::RenderWindow &rw)
  769. {
  770.         text.setFont(Font);
  771.         text.setCharacterSize(Size);
  772.         text.setPosition(Pos.x, Pos.y);
  773.         text.setColor(Color);
  774.         text.setString(str);
  775.  
  776.         rw.draw(text);
  777. }
  778.  
  779. void Reset(sf::Shape & s1, sf::Shape & s2, sf::Shape & s3, sf::Vector2f & v1, sf::Vector2f & v2, sf::Vector2f & v3, int & x, int & y)
  780. {
  781.         s1.setPosition(v1);
  782.         s2.setPosition(v2);
  783.         s3.setPosition(v3);
  784.  
  785.         x = 0;
  786.         y = 0;
  787. }
  788.  
  789. void Save()
  790. {
  791.  
  792. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement