Advertisement
gorgoroth666

SFML2 - 13 - Game Structure

Jul 19th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.83 KB | None | 0 0
  1. #ifdef _MSC_VER  
  2.     #ifdef _WIN32
  3.         #ifndef _DEBUG
  4.             #pragma comment( lib, "sfml-system.lib" )      
  5.             #pragma comment( lib, "sfml-graphics.lib" )        
  6.             #pragma comment( lib, "sfml-window.lib" )  
  7.         #else
  8.             #pragma comment( lib, "sfml-system-d.lib" )    
  9.             #pragma comment( lib, "sfml-graphics-d.lib" )          
  10.             #pragma comment( lib, "sfml-window-d.lib" )
  11.         #endif
  12.     #endif
  13. #endif
  14.  
  15. #include <SFML/Graphics.hpp>
  16. #include <algorithm>
  17. #include <sstream>
  18.  
  19. using namespace sf;
  20.  
  21. bool intersects (const RectangleShape & rect1,const RectangleShape & rect2)
  22. {
  23.     FloatRect r1=rect1.getGlobalBounds();
  24.     FloatRect r2=rect2.getGlobalBounds();
  25.     return r1.intersects (r2);
  26. }
  27.  
  28. int clamp (const int x, const int a, const int b)
  29. {
  30.     return std::min(std::max(a,x),b);
  31. }
  32.  
  33. class Game
  34. {
  35. private:
  36.     static const int width = 640;
  37.     static const int height= 480;
  38.     static const int borderSize= 12;
  39.     static const int margin = 50;
  40.     static const int moveDistance = 5;
  41.  
  42.     RenderWindow window;
  43.     Font font;
  44.  
  45.     RectangleShape top;
  46.     RectangleShape left;
  47.     RectangleShape right;
  48.     RectangleShape bottom; 
  49.  
  50.     RectangleShape ball;
  51.  
  52.     Vector2f ballSpeed;
  53.  
  54.     RectangleShape player1;
  55.     RectangleShape player2;
  56.  
  57.     RectangleShape middleLine;
  58.  
  59.     Text title;
  60.     Text start;
  61.     Text won;
  62.     Text lost;
  63.     Text score;
  64.     Text fps;
  65.  
  66.     Clock clock;
  67.     Time time;
  68.  
  69.     unsigned int p1Score, p2Score;
  70.  
  71.     enum states {INTRO, PLAYING, P1WON, P1LOST};
  72.    
  73.     int gameState;
  74.    
  75. public:
  76.     Game()
  77.     {
  78.     }
  79.  
  80.     bool init()
  81.     {
  82.         VideoMode videoMode(width, height);
  83.         window.create(videoMode,"Pong Structure");
  84.         window.setVerticalSyncEnabled(true);
  85.         window.setFramerateLimit(60);
  86.  
  87.         if (!font.loadFromFile("tomb.ttf"))
  88.             return false;  
  89.    
  90.         setup();
  91.         return true;
  92.     }
  93.  
  94.     int exec()
  95.     {
  96.         while (window.isOpen())
  97.         {
  98.             time=clock.getElapsedTime();
  99.             float fFps=1000000/time.asMicroseconds();
  100.             std::stringstream s;
  101.             s<<fFps;
  102.             fps.setString(s.str());
  103.             clock.restart();
  104.  
  105.             display();
  106.  
  107.             Event event;
  108.             while (window.pollEvent(event))
  109.             {
  110.                 if ( (event.type == Event::Closed) ||
  111.                 ((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Escape)) )
  112.                     window.close();
  113.                 else
  114.                 if ((event.type == Event::KeyPressed) && (gameState == INTRO))
  115.                     gameState=PLAYING;
  116.             }
  117.        
  118.             if (gameState!=PLAYING)
  119.                 continue;
  120.            
  121.             update();
  122.  
  123.         }
  124.        
  125.         return EXIT_SUCCESS;
  126.     }
  127.  
  128. private:
  129.  
  130.     void update()
  131.     {
  132.         updatePlayer1();
  133.         updatePlayer2();
  134.         checkCollisions();
  135.         updateBall();
  136.  
  137.         // detect if game is over
  138.         if (p1Score >=11 && p1Score >= p2Score +2)
  139.             gameState=P1WON;
  140.         if (p2Score >=11 && p2Score >= p1Score +2)
  141.             gameState=P1LOST;
  142.     }
  143.  
  144.     void setup()
  145.     {
  146.         top.setPosition(0, 0);
  147.         top.setSize(Vector2f(width, borderSize));
  148.  
  149.         left.setPosition(-borderSize, 0);
  150.         left.setSize(Vector2f(borderSize, height));
  151.  
  152.         right.setPosition(width, 0);
  153.         right.setSize(Vector2f(borderSize, height));
  154.  
  155.         bottom.setPosition(0, height-borderSize);
  156.         bottom.setSize(Vector2f(width, borderSize));
  157.            
  158.         top.setFillColor(Color(100,100,100));
  159.         top.setOutlineColor(Color::Blue);
  160.         top.setOutlineThickness(3);
  161.  
  162.         left.setFillColor(Color(100,100,100));
  163.         left.setOutlineColor(Color::Blue);
  164.         left.setOutlineThickness(3);
  165.  
  166.         right.setFillColor(Color(100,100,100));
  167.         right.setOutlineColor(Color::Blue);
  168.         right.setOutlineThickness(3);
  169.  
  170.         bottom.setFillColor(Color(100,100,100));
  171.         bottom.setOutlineColor(Color::Blue);
  172.         bottom.setOutlineThickness(3);
  173.        
  174.         ball.setPosition(width/2, height/2);
  175.         ball.setSize(Vector2f(20, 20));
  176.         ball.setFillColor(Color::Yellow);
  177.         ball.setOutlineColor(Color::Red);
  178.         ball.setOutlineThickness(2);
  179.  
  180.         player1.setSize(Vector2f(borderSize, 90));
  181.         player1.setPosition(margin-borderSize, height/2-25);
  182.         player1.setFillColor(Color(0,122,245));
  183.         player1.setOutlineColor(Color::Red);
  184.         player1.setOutlineThickness(3);
  185.        
  186.         player2.setSize(Vector2f(borderSize, 90));
  187.         player2.setPosition(width-margin, height/2-25);
  188.         player2.setFillColor(Color(0,122,245));
  189.         player2.setOutlineColor(Color::Red);
  190.         player2.setOutlineThickness(3);
  191.            
  192.         middleLine.setFillColor(Color(100,100,100,30));
  193.         middleLine.setOutlineColor(Color(0,0,100,30));
  194.         middleLine.setOutlineThickness(2);
  195.         middleLine.setPosition(width/2, 0);
  196.         middleLine.setSize(Vector2f(0, height));
  197.  
  198.         title.setString("Pong SFML 2");
  199.         title.setFont(font);
  200.         title.setCharacterSize(50);
  201.         title.setPosition(width/2-title.getGlobalBounds().width/2,100);
  202.         title.setColor(Color::Blue);
  203.    
  204.         start.setString("Press any key to start");
  205.         start.setFont(font);
  206.         start.setCharacterSize(30);
  207.         start.setPosition(width/2-start.getGlobalBounds().width/2,400);
  208.         start.setColor(Color::Red);
  209.  
  210.         won.setString("You have won this game.\n\n Congratulations !");
  211.         won.setFont(font);
  212.         won.setCharacterSize(20);
  213.         won.setPosition(width/2-won.getGlobalBounds().width/2,height/2-won.getGlobalBounds().height/2);
  214.         won.setColor(Color::Green);
  215.  
  216.         lost.setString("You have lost this game, \n better luck next time!");
  217.         lost.setFont(font);
  218.         lost.setCharacterSize(20);
  219.         lost.setPosition(width/2-lost.getGlobalBounds().width/2,height/2-lost.getGlobalBounds().height/2);
  220.         lost.setColor(Color::Red);
  221.  
  222.         score.setString("0   0");
  223.         score.setFont(font);
  224.         score.setCharacterSize(50);
  225.         score.setPosition(width/2-score.getGlobalBounds().width/2,40);
  226.         score.setColor(Color(0,0,100,50));
  227.  
  228.         fps.setString("0");
  229.         fps.setFont(font);
  230.         fps.setCharacterSize(30);
  231.         fps.setPosition(fps.getGlobalBounds().width/2,40);
  232.         fps.setColor(Color(52,0,100,50));
  233.        
  234.         ballSpeed=Vector2f(0.1,0.1);
  235.         p1Score=0;
  236.         p2Score=0;
  237.         gameState=INTRO;
  238.  
  239.     }
  240.  
  241.     void display()
  242.     {      
  243.         window.clear(Color::White);
  244.         switch(gameState)
  245.         {
  246.             case INTRO:
  247.                 window.draw(title);
  248.                 window.draw(start);
  249.                 break;
  250.             case PLAYING:
  251.                 window.draw(middleLine);
  252.                 window.draw(left);
  253.                 window.draw(right);        
  254.                 window.draw(player1);  
  255.                 window.draw(player2);  
  256.                 window.draw(ball); 
  257.                 window.draw(score);
  258.                 window.draw(top);
  259.                 window.draw(bottom);       
  260.                 window.draw(fps);  
  261.                 break;
  262.             case P1WON:
  263.                 window.draw(won);
  264.                 break;
  265.             case P1LOST:
  266.                 window.draw(lost);
  267.                 break;
  268.         }
  269.         window.display();
  270.     }
  271.  
  272.     void updatePlayer1()
  273.     {
  274.         // move player 1 pad
  275.         if (Keyboard::isKeyPressed(Keyboard::Up))
  276.         {
  277.             player1.move(0,-moveDistance*time.asMilliseconds()/50.0);
  278.         }else
  279.         if (Keyboard::isKeyPressed(Keyboard::Down))
  280.         {
  281.             player1.move(0,moveDistance*time.asMilliseconds()/50.0);
  282.         }
  283.     }
  284.  
  285.     void updatePlayer2()
  286.     {
  287.         // auto move player2 pad
  288.         if (ball.getPosition().y < player2.getPosition().y)
  289.             player2.move(0, -moveDistance*time.asMilliseconds()/40.0);
  290.         else if(ball.getPosition().y+ball.getSize().y > player2.getPosition().y+player2.getSize().y)
  291.             player2.move(0, moveDistance*time.asMilliseconds()/40.0);
  292.     }
  293.  
  294.     void updateBall()
  295.     {
  296.         ball.move(ballSpeed.x*time.asMilliseconds(),ballSpeed.y*time.asMilliseconds());
  297.     }
  298.  
  299.     void checkCollisions()
  300.     {
  301.                        
  302.         // block players pad inside the play area
  303.         if ( intersects(player1,bottom) || intersects(player1,top) )
  304.         {
  305.             FloatRect t=top.getGlobalBounds();
  306.             FloatRect b=bottom.getGlobalBounds();
  307.             Vector2f p=player1.getPosition();
  308.             p.y=clamp(p.y,t.top+t.height+5,b.top-player1.getSize().y-5);
  309.             player1.setPosition(p);
  310.         }
  311.         if ( intersects(player2,bottom) || intersects(player2,top) )
  312.         {
  313.             FloatRect t=top.getGlobalBounds();
  314.             FloatRect b=bottom.getGlobalBounds();
  315.             Vector2f p=player2.getPosition();
  316.             p.y=clamp(p.y,t.top+t.height+5,b.top-player2.getSize().y-5);
  317.             player2.setPosition(p);
  318.         }
  319.         // ball collides with top and bottom
  320.         if (intersects(ball,top))
  321.         {
  322.             FloatRect t=top.getGlobalBounds();
  323.             FloatRect b=ball.getGlobalBounds();
  324.             ballSpeed.y=-ballSpeed.y;
  325.             int u = t.top + t.height - b.top;
  326.             ball.move(0,2*u);            
  327.         }
  328.         if ( intersects(ball,bottom) )
  329.         {
  330.             FloatRect bot= bottom.getGlobalBounds();
  331.             FloatRect b= ball.getGlobalBounds();
  332.             ballSpeed.y=-ballSpeed.y;
  333.             int u = bot.top - b.height - b.top;
  334.             ball.move(0,2*u);  
  335.         }
  336.         // ball collides with player1 and player2
  337.         if (intersects(ball,player1))
  338.         {
  339.             FloatRect p= player1.getGlobalBounds();
  340.             FloatRect b= ball.getGlobalBounds();
  341.             ballSpeed.x= -ballSpeed.x;
  342.             ballSpeed.y= (b.top+b.height/2 - p.top - p.height/2) / 100;
  343.             int u = p.left +  p.width - b.left;
  344.             b.left = p.left +  p.width + u;
  345.             ball.setPosition(b.left,b.top);
  346.             //increase ball speed by 2-3%
  347.             ballSpeed.x*=1.03f;
  348.             ballSpeed.y*=1.02f;
  349.         }
  350.         if ( intersects(ball,player2) )
  351.         {
  352.             FloatRect p=player2.getGlobalBounds();
  353.             FloatRect b=ball.getGlobalBounds();
  354.             ballSpeed.x=-ballSpeed.x;
  355.             ballSpeed.y= (b.top+b.height/2 - p.top - p.height/2) / 100;
  356.             int u = b.left + b.width - p.left;
  357.             b.left = p.left -  b.width - u;
  358.             ball.setPosition(b.left,b.top);
  359.             //increase ball speed by 2-3%
  360.             ballSpeed.x*=1.03f;
  361.             ballSpeed.y*=1.02f;
  362.         }                      
  363.        
  364.         // check for scoring
  365.         if (intersects(ball,left))
  366.         {
  367.             p2Score++;
  368.             std::stringstream str;
  369.             str << p1Score << "   " << p2Score;
  370.             score.setString(str.str());
  371.             score.setPosition(width/2-score.getGlobalBounds().width/2,40);
  372.             FloatRect p=player2.getGlobalBounds();
  373.             FloatRect b=ball.getGlobalBounds();
  374.             ball.setPosition(p.left-b.width-5, height/2);
  375.             ballSpeed.x=-0.1f;
  376.             ballSpeed.y=0.1f;
  377.  
  378.         }
  379.         if (intersects(ball,right))
  380.         {
  381.             p1Score++;
  382.             std::stringstream str;
  383.             str << p1Score << "   " << p2Score;
  384.             score.setString(str.str());
  385.             score.setPosition(width/2-score.getGlobalBounds().width/2,40);
  386.             FloatRect p=player1.getGlobalBounds();
  387.             FloatRect b=ball.getGlobalBounds();
  388.             ball.setPosition(p.left+p.width+5, height/2);
  389.             ballSpeed.x=0.1f;
  390.             ballSpeed.y=0.1f;
  391.         }
  392.  
  393.     }
  394.  
  395.    
  396.  
  397. };
  398.  
  399.  
  400. int main()
  401. {
  402.     Game game;
  403.     if (!game.init())
  404.         return EXIT_FAILURE;
  405.     return game.exec();
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement