Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int WINDOW_WIDTH = 1024;
- const int WINDOW_HEIGHT = 768;
- //2d array of strings for the answers. 5 questions, 4 answers each
- string answers[8][4];
- void setAnswers(string q1, string q2, string q3, string q4, int c, int question, int* correct)
- {
- answers[question][0] = q1; answers[question][1] = q2; answers[question][2] = q3; answers[question][3] = q4;
- correct[question] = c - 1;
- }
- bool detectCollision(sf::Vector2i& pos, sf::RectangleShape& rect)
- {
- int rectLeft = rect.getPosition().x;
- int rectRight = rect.getPosition().x + rect.getSize().x;
- int rectTop = rect.getPosition().y;
- int rectBottom = rect.getPosition().y + rect.getSize().y;
- bool colliding = true;
- if (pos.x > rectRight)
- colliding = false;
- if (pos.x < rectLeft)
- colliding = false;
- if (pos.y > rectBottom)
- colliding = false;
- if (pos.y < rectTop)
- colliding = false;
- return colliding;
- }
- // tell them their score at the end rather then tell them immediately if the got it correct/wrong
- int main()
- {
- srand(time(0));
- sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "quiz game");
- window.setFramerateLimit(60);
- int questionNum = 0;
- int correctBox[8] = {};
- string questions[8] = { "How many people approximately live on Earth?",
- "Which isn't a musical family?",
- "Who was Canada's first Prime Minister?",
- "In what year did the Unix epoch time start counting?",
- "Which language doesn't support multiple inheritance?",
- "What is Canada's national sport?",
- "What is measured in ohms?",
- "Who formed the laws of motion?"};
- // answers for question 1, 2, 3...
- setAnswers("6 billion", "4 billion", "7.5 billion", "8.5 billion", 3, 0, correctBox);
- setAnswers("Brass", "Percussion", "String", "Electrical", 4, 1, correctBox);
- setAnswers("John A. Macdonald", "Louis Riel", "George Brown", "George Washington", 1, 2, correctBox);
- setAnswers("1980", "1970", "1972", "1992", 2, 3, correctBox);
- setAnswers("C++", "C#", "Python", "Perl", 2, 4, correctBox);
- setAnswers("Hockey", "Basketball", "Lacrosse", "Badminton", 3, 5, correctBox);
- setAnswers("Resistance", "Voltage", "Current", "Power", 1, 6, correctBox);
- setAnswers("Albert Einstein", "Winston Churchill", "Issac Newton", "Stephen Hawking", 3, 7, correctBox);
- vector<sf::RectangleShape*> rects;
- for (int i = 0; i < 4; i++)
- {
- sf::RectangleShape* temp = new sf::RectangleShape;
- temp->setSize(sf::Vector2f(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3));
- if (i == 0)
- temp->setFillColor(sf::Color::Red);
- else if (i == 1)
- temp->setFillColor(sf::Color::Blue);
- else if (i == 2)
- temp->setFillColor(sf::Color::Green);
- else if (i == 3)
- temp->setFillColor(sf::Color::Yellow);
- if (i <= 1)
- temp->setPosition(i * WINDOW_WIDTH / 2, 250);
- else
- {
- if (i == 2)
- temp->setPosition(i - i, 500);
- else
- temp->setPosition(WINDOW_WIDTH / 2, 500);
- }
- rects.push_back(temp);
- }
- sf::Font font;
- font.loadFromFile("arial.ttf");
- vector<sf::Text*> texts;
- sf::Text question;
- question.setFont(font);
- question.setString("How many people approximately live on Earth?");
- question.setCharacterSize(38);
- question.setFillColor(sf::Color::Black);
- // 17 is the approximate offset
- question.setPosition(WINDOW_WIDTH / 2 - question.getString().getSize() / 2 * 17, 100);
- for (int i = 0; i < 4; i++)
- {
- sf::Text* text = new sf::Text;
- text->setFont(font);
- text->setString("ligma");
- text->setCharacterSize(30);
- text->setFillColor(sf::Color::Black);
- text->setPosition(rects[i]->getPosition().x + WINDOW_WIDTH / 4 - 50,
- rects[i]->getPosition().y + 100);
- texts.push_back(text);
- }
- int score = 0;
- int currentQuestion = 1;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- sf::Vector2i pos;
- pos = sf::Mouse::getPosition(window);
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::MouseButtonPressed)
- {
- if (event.mouseButton.button == sf::Mouse::Left)
- {
- for (int i = 0; i < rects.size(); i++)
- {
- if (detectCollision(pos, *rects[i]))
- {
- if (correctBox[questionNum] == i)
- score++;
- questionNum++;
- if (questionNum < 8)
- {
- question.setString(questions[questionNum]);
- question.setPosition(WINDOW_WIDTH / 2 - question.getString().getSize() / 2 * 17, 100);
- }
- else
- {
- question.setString("You scored " + to_string(score) + "/8!");
- }
- }
- }
- }
- }
- }
- window.clear(sf::Color::White);
- // renders both the boxes and the texts
- for (int i = 0; i < rects.size(); i++)
- {
- if (questionNum <= 7)
- texts[i]->setString(answers[questionNum][i]);
- else
- texts[i]->setString("");
- window.draw(*rects[i]);
- window.draw(*texts[i]);
- }
- window.draw(question);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement