Skip to content

Instantly share code, notes, and snippets.

@pixelpusher
Created March 9, 2012 14:05
Show Gist options
  • Save pixelpusher/2006635 to your computer and use it in GitHub Desktop.
Save pixelpusher/2006635 to your computer and use it in GitHub Desktop.
QuizGameModified
/**
* 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
{
String question;
int answer;
}
int numberOfQuestions = 5; //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(80, 255, 80); //green
color mediumTimeColor = color(220, 180, 20); // orange?
color endTimeColor = color(255, 0, 0);
// questions are text (String)
// answers are a number (1 or 2)
// create Question objects to hold questions & answers
Question[] questions;
// keeps track of whether key was pressed last frame
boolean wasKeyPressed = false;
// 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;
// elapsed time for current question
int timeDiff = 0;
int startTime = 0; // the time we first started displaying the current question
int score = 0; // how many they got right
void setup()
{
size(640, 480);
questions = new Question[ numberOfQuestions ];
for (int i=0; i < numberOfQuestions; 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? \n1) 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 coolest? 1) Josh 2) Evan";
questions[3].answer = 1;
questions[4].question = "Am I getting tires of saying the word question 1) Yes 2) Definitely";
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;
// QUESTION 1
if ( currentQuestion < numberOfQuestions)
{
Question q = questions[ currentQuestion ];
doQuestion( q );
}
else
{
background( #B9B538 );
textSize(24);
text("FINAL SCORE! =" + score, 5, 40);
}
}
// 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 the key as not having been pressed
wasKeyPressed = false;
}
void checkAnswer( int correctAnswer )
{
// if a key is pressed, check for the correct answer:
if (keyPressed && !wasKeyPressed)
{
wasKeyPressed = 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++;
}
else
{
//kill game
//exit();
score--;
}
currentQuestion++;
startTime = millis();
// end keyPressed
}
// end checkAnswer()
}
void doQuestion( Question q )
{
drawBackground(timeDiff);
text(q.question, 5, 26);
checkAnswer( q.answer );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment