Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <string>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. const int WINDOW_WIDTH = 1024;
  11. const int WINDOW_HEIGHT = 768;
  12.  
  13. //2d array of strings for the answers. 5 questions, 4 answers each
  14. string answers[8][4];
  15.  
  16. void setAnswers(string q1, string q2, string q3, string q4, int c, int question, int* correct)
  17. {
  18.     answers[question][0] = q1; answers[question][1] = q2; answers[question][2] = q3; answers[question][3] = q4;
  19.     correct[question] = c - 1;
  20. }
  21.  
  22. bool detectCollision(sf::Vector2i& pos, sf::RectangleShape& rect)
  23. {
  24.     int rectLeft = rect.getPosition().x;
  25.     int rectRight = rect.getPosition().x + rect.getSize().x;
  26.     int rectTop = rect.getPosition().y;
  27.     int rectBottom = rect.getPosition().y + rect.getSize().y;
  28.  
  29.     bool colliding = true;
  30.     if (pos.x > rectRight)
  31.         colliding = false;
  32.     if (pos.x < rectLeft)
  33.         colliding = false;
  34.     if (pos.y > rectBottom)
  35.         colliding = false;
  36.     if (pos.y < rectTop)
  37.         colliding = false;
  38.  
  39.     return colliding;
  40. }
  41.  
  42. // tell them their score at the end rather then tell them immediately if the got it correct/wrong
  43. int main()
  44. {
  45.     srand(time(0));
  46.     sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "quiz game");
  47.     window.setFramerateLimit(60);
  48.     int questionNum = 0;
  49.     int correctBox[8] = {};
  50.     string questions[8] = { "How many people approximately live on Earth?",
  51.                            "Which isn't a musical family?",
  52.                            "Who was Canada's first Prime Minister?",
  53.                            "In what year did the Unix epoch time start counting?",
  54.                            "Which language doesn't support multiple inheritance?",
  55.                            "What is Canada's national sport?",
  56.                            "What is measured in ohms?",
  57.                            "Who formed the laws of motion?"};
  58.  
  59.     // answers for question 1, 2, 3...
  60.     setAnswers("6 billion", "4 billion", "7.5 billion", "8.5 billion", 3, 0, correctBox);
  61.     setAnswers("Brass", "Percussion", "String", "Electrical", 4, 1, correctBox);
  62.     setAnswers("John A. Macdonald", "Louis Riel", "George Brown", "George Washington", 1, 2, correctBox);
  63.     setAnswers("1980", "1970", "1972", "1992", 2, 3, correctBox);
  64.     setAnswers("C++", "C#", "Python", "Perl", 2, 4, correctBox);
  65.     setAnswers("Hockey", "Basketball", "Lacrosse", "Badminton", 3, 5, correctBox);
  66.     setAnswers("Resistance", "Voltage", "Current", "Power", 1, 6, correctBox);
  67.     setAnswers("Albert Einstein", "Winston Churchill", "Issac Newton", "Stephen Hawking", 3, 7, correctBox);
  68.  
  69.     vector<sf::RectangleShape*> rects;
  70.  
  71.     for (int i = 0; i < 4; i++)
  72.     {
  73.         sf::RectangleShape* temp = new sf::RectangleShape;
  74.         temp->setSize(sf::Vector2f(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3));
  75.         if (i == 0)
  76.             temp->setFillColor(sf::Color::Red);
  77.         else if (i == 1)
  78.             temp->setFillColor(sf::Color::Blue);
  79.         else if (i == 2)
  80.             temp->setFillColor(sf::Color::Green);
  81.         else if (i == 3)
  82.             temp->setFillColor(sf::Color::Yellow);
  83.  
  84.         if (i <= 1)
  85.             temp->setPosition(i * WINDOW_WIDTH / 2, 250);
  86.         else
  87.         {
  88.             if (i == 2)
  89.                 temp->setPosition(i - i, 500);
  90.             else
  91.                 temp->setPosition(WINDOW_WIDTH / 2, 500);
  92.         }
  93.         rects.push_back(temp);
  94.     }
  95.  
  96.     sf::Font font;
  97.     font.loadFromFile("arial.ttf");
  98.     vector<sf::Text*> texts;
  99.  
  100.     sf::Text question;
  101.     question.setFont(font);
  102.     question.setString("How many people approximately live on Earth?");
  103.     question.setCharacterSize(38);
  104.     question.setFillColor(sf::Color::Black);
  105.    
  106.     // 17 is the approximate offset
  107.     question.setPosition(WINDOW_WIDTH / 2 - question.getString().getSize() / 2 * 17, 100);
  108.  
  109.     for (int i = 0; i < 4; i++)
  110.     {
  111.         sf::Text* text = new sf::Text;
  112.         text->setFont(font);
  113.         text->setString("ligma");
  114.         text->setCharacterSize(30);
  115.         text->setFillColor(sf::Color::Black);
  116.         text->setPosition(rects[i]->getPosition().x + WINDOW_WIDTH / 4 - 50,
  117.             rects[i]->getPosition().y + 100);
  118.         texts.push_back(text);
  119.     }
  120.  
  121.     int score = 0;
  122.     int currentQuestion = 1;
  123.  
  124.     while (window.isOpen())
  125.     {
  126.         sf::Event event;
  127.         while (window.pollEvent(event))
  128.         {
  129.             sf::Vector2i pos;
  130.             pos = sf::Mouse::getPosition(window);
  131.             if (event.type == sf::Event::Closed)
  132.                 window.close();
  133.             if (event.type == sf::Event::MouseButtonPressed)
  134.             {
  135.                 if (event.mouseButton.button == sf::Mouse::Left)
  136.                 {
  137.                     for (int i = 0; i < rects.size(); i++)
  138.                     {
  139.                         if (detectCollision(pos, *rects[i]))
  140.                         {
  141.                             if (correctBox[questionNum] == i)
  142.                                 score++;
  143.                            
  144.                             questionNum++;
  145.                             if (questionNum < 8)
  146.                             {
  147.  
  148.                                 question.setString(questions[questionNum]);
  149.                                 question.setPosition(WINDOW_WIDTH / 2 - question.getString().getSize() / 2 * 17, 100);
  150.                             }
  151.                             else
  152.                             {
  153.                                 question.setString("You scored " + to_string(score) + "/8!");
  154.                             }
  155.                         }
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         window.clear(sf::Color::White);
  161.  
  162.         // renders both the boxes and the texts
  163.         for (int i = 0; i < rects.size(); i++)
  164.         {
  165.             if (questionNum <= 7)
  166.                 texts[i]->setString(answers[questionNum][i]);
  167.             else
  168.                 texts[i]->setString("");
  169.  
  170.             window.draw(*rects[i]);
  171.             window.draw(*texts[i]);
  172.         }
  173.        
  174.         window.draw(question);
  175.  
  176.         window.display();
  177.     }
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement