Created
March 9, 2012 10:40
-
-
Save pixelpusher/2006033 to your computer and use it in GitHub Desktop.
QuizGameUsingObjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple quiz game. | |
* | |
* by Evan Raskob | |
* UCA CGA, Farnham | |
* | |
* The Arena, 2012. Week 5+ | |
* | |
* Your first task is to add the code to make questions 2 and 3 work. Have a look | |
* at how question 1 is implemented. Draw a diagram of the program to help yourself visualise it. | |
* | |
* Your second task is to use arrays for the questions - an array for the String | |
* containing the questions themselves, and an array for the answers. You will | |
* probably need to write a few methods to help you out - think of how you can make | |
* this code more efficient (less typing for you!) | |
* | |
* Your 3rd task, and the toughest, is to write an object that represents a question. | |
* Think about all the essential data is a question - the text, the correct answer, etc. | |
* Draw a picture (or diagram) showing these essential properties of the question. | |
* Use your object to re-write this code more efficiently. | |
* | |
*/ | |
class Question | |
{ | |
int answer; | |
String question; | |
} | |
int numberOfQuestions = 3; //total questions in this game | |
int currentQuestion = 0; // questions are numbered from 0 to 4 | |
// these are the keys we press to select an answer | |
char answer1Key = '1'; | |
char answer2Key = '2'; | |
color easyTimeColor = color(0, 255, 0); //green | |
color mediumTimeColor = color(220, 180, 20); // orange? | |
color endTimeColor = color(255, 0, 0); | |
// this is for testing if the key is held down | |
boolean keyWasPressed = false; | |
// questions are text (String) | |
// answers are a number (1 or 2) | |
Question[] questions; | |
// These are the time limits for each stage of a question. | |
// You could have different points for each time level. | |
int easyTime = 2000; // all of these are in milliseconds (1000th of a second) | |
int mediumTime = easyTime + 2000; | |
int endTime = mediumTime + 2000; | |
int startTime = 0; // the time we first started displaying the current question | |
int timeDiff = 0; | |
int score = 0; // how many they got right | |
int numQuestions = 5; | |
void setup() | |
{ | |
size(640, 480); | |
// create questions (Objects) | |
questions = new Question[numQuestions]; | |
for (int i=0; i < numQuestions; i++) | |
{ | |
questions[i] = new Question(); | |
} | |
questions[0].question = "What county is Farnham based in? 1) Surrey 2) London"; | |
questions[0].answer = 1; | |
questions[1].question = "Who is the Prime Minister? 1) David Cameron 2) Optimus Prime"; | |
questions[1].answer = 1; | |
questions[2].question = "What is the longest river? 1) Amazon 2) Nile"; | |
questions[2].answer = 2; | |
questions[3].question = "Who is the Riddler? 1) ??, 2) Jim Carrey, 3) Me"; | |
questions[3].answer = 2; | |
questions[4].question = "How tall is Steve Littman? 1) 9ft 2) 2.1m "; | |
questions[4].answer = 2; | |
currentQuestion = 0; | |
score = 0; | |
startTime = millis(); // millis() gives us the current time th sketch has been running for | |
} | |
void draw() | |
{ | |
textSize(16); | |
int currentTime = millis(); | |
timeDiff = currentTime - startTime; | |
// Have we shown all the questions? | |
if ( currentQuestion < numQuestions) | |
{ | |
Question q = questions[ currentQuestion ]; | |
doQuestion(q); | |
} | |
else | |
{ | |
// this is if it is not in any question | |
// currentQuestion > 2 | |
background(255); | |
textSize(24); | |
text("FINAL QUIZ RESULT! SCORE IS:" + score, 5, 26); | |
//exit(); | |
} | |
// end draw() | |
} | |
void doQuestion( Question q ) | |
{ | |
drawBackground(timeDiff); | |
text(q.question, 5, 26); | |
checkAnswer(q.answer); | |
} | |
void checkAnswer( int correctAnswer ) | |
{ | |
// if a key is pressed, check for the correct answer: | |
if (keyPressed) | |
{ | |
if (keyWasPressed) | |
{ | |
// do nothing! key is held down | |
} | |
else | |
{ | |
// mark key as being held down from now on | |
keyWasPressed = true; | |
// this will store the player's guess (answer) | |
int answer = 0; | |
// answers can be only either 1 or 2 | |
if (key == answer1Key) | |
{ | |
answer = 1; | |
} | |
else if (key == answer2Key) | |
{ | |
answer = 2; | |
} | |
// is this the correct answer? | |
if (answer == correctAnswer) | |
{ | |
// CORRECT! Move on to the next question and restart the timer. | |
score++; | |
} | |
currentQuestion++; | |
startTime = millis(); | |
} | |
// end keyPressed | |
} | |
// end checkCorrectAnswer() | |
} | |
// This is the same for all questions. | |
// This method take the time difference between the current time and when the player first | |
// started answering the question, and draws the background the appropriate colour. | |
// If too much time has elapsed, it moves on to the next question. | |
void drawBackground(int timeDiff) | |
{ | |
fill(0); | |
if (timeDiff < easyTime) | |
{ | |
background(easyTimeColor ); | |
} | |
else if (timeDiff < mediumTime) | |
{ | |
background(mediumTimeColor ); | |
} | |
else if (timeDiff < endTime) | |
{ | |
background(endTimeColor ); | |
} | |
else | |
{ | |
// OUT OF TIME! | |
// Move on to the next question and restart the timer. | |
currentQuestion++; | |
startTime = millis(); | |
} | |
text("score:"+score, 5, 80); | |
} | |
void keyReleased() | |
{ | |
// mark key as not held down anymore | |
keyWasPressed = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment