PiQuiz – An easy quiz for kids with LEDs.

This tutorial will show you how to design a quiz for two players. You are asked seven questions and the first to answer seven correctly wins! The game makes use of LEDs so you know how many questions you have answered correctly. If you do not have LEDs connected there is an option not to use the gpio.
The answers are kept in a seperate file. You can have many files of differing levels and call them into the script.

Difficulty = Intermediate

Requirements:

  • Raspberry Pi
  • LEDs/Breadboard/resistors/wires (if used with gpio)

The first thing to do is make a text file with the list of questions and answers. The game will open the file, read each line and return the question and answer. Each line must contain the question and the answer seperated by a colon (:). For example:

What animal purrs and drinks milk:cat
What is 5+5?:10

This needs to be saved into a file called questions.txt. I recommend that you have at least 50 questions so that there is no chance of a question being used more than once. Now that we have the questions sorted we can move onto scripting the game.

Firstly we need to ask the user if any LEDs are connected. We will use the variable throughout the game to ensure that the gpio is used/not used. Start a new file by typing the following into the terminal:

sudo nano piquiz.sh

Now add the following code:

hardware ()
{
  ## Ask the players if we need to use the gpio 
  ## and place the answer into the variable "$gpioa"...
  read -p "Do you have LEDs connected? [y/n] : " gpioa
}

Next, we need to setup some functions. The first is a function is to set the relevant gpio pins as outputs.
Copy and paste the following into the file:

## Functions...
setup ()
{
  ## Set the gpio's to outputs...
  echo Setup
  for ia in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio mode $ia out ; done
}

We are using wiringPi gpio pins 0-6 for player one and wiringPi pins 8-14 for player two. The above function will set them all to outputs so that we can turn on the LEDs as needed.

Now we will include a function to introduce the players to the game:

intro ()
 {
  clear
  echo "welcome to piquiz.  The quiz for all ages with optional LEDs"
  sleep 4
  echo "To play the game, just answer the questions correctly.  The first to 7 points wins."
  sleep 4
 }

Next we will run a little test of the LEDs (just for fun). This will flash the LEDs on/off ten times. Add the following to the file:

testled ()
{
  echo "Testing the game board... Please wait..."
  f=1
  ## Use a while loop to repeat ten times.
  while [ $f -lt 10 ]
  do
    ## Flash the LEDs on and off quickly (every millisecond) and repeat 10 times...
    for ib in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio write $ib 1 ; done
    sleep 0.1
    for ic in 0 1 2 3 4 5 6 8 9 10 11 12 13 14 ; do gpio write $ic 0 ; done
    sleep 0.1
    f=$(($f+1))
  done
}

The next function will ask the players for their name. This will make it easier to know whos turn it is during the game. We use the ‘read’ function to get input from the player and place it into a variable called ‘$pone’ & ‘$ptwo’:

playerone ()
{
  ## Ask player one to input their name...
  read -p "What is your name Player 1 : " pone

  ## Create a variable to store the score for player 1...
  ponescore=0

  ## Define the gpio pin to start...
  ponegpio=0
}
playertwo ()
{
  ## Ask player two to input their name...
  read -p "What is your name Player 2 : " ptwo

  ## Create a variable to store the score for player 2...
  ptwoscore=0

  ## Define the gpio pin to start...
  ptwogpio=8
}

Now for the good stuff. The following function will open our questions text file, assign a random number to each line and pull the question out for use in the game. It will then split the line using the colon (:) as delimiter and assign the question & answer to two seperate variables. The question will be displayed on-screen and the answer hidden for checking.

askQuestion ()
{
  ## define the file to be used for the questions...
  QUOTES_FILE=questions.txt

  ## extract each line from the file...
  numLines=`wc -l $QUOTES_FILE | cut -d" " -f 1`

  ## Set a random number to shuffle the questions...
  random=$RANDOM

  ## Assign the random number to each line question...
  selectedLineNumber=$(($random - $(($random/$numLines)) * $numLines + 1))

  ## Extract a question from the file...
  selectedLine=`head -n $selectedLineNumber $QUOTES_FILE | tail -n 1`

  ## Split the question/answer for use in the game 
  ##and assign to variables '$question' / '$answer'
  question=$(echo "$selectedLine" | cut -d':' -f 1)
  answer=$(echo "$selectedLine" | cut -d':' -f 2)
}

This next function asks the questions and plays the game. It will light our LEDs (if connected) and keep score.

playthegame ()
 {
  ## Clear the screen...
  clear
  ## Set the variable to calculate who's turn it is...
  i=1
  ## Start a while loop to check score 
  ## and play the game...
  while true
  do
   ## If the score is 7 then let us know which player has won...
   if [ $ponescore -eq 7 ]
   then
    echo "WOO HOO $pone !!! YOU ARE THE WINNER"
    sleep 5
    exit
   elif [ $ptwoscore -eq 7 ]
   then
    echo "WOO HOO $ptwo !!! YOU ARE THE WINNER"
    sleep 5
    exit
   ## Else play the game...
   else
	## Use an IF statement to ask each player a question...
    if [[ $i -eq 1 ]]
    then
      ## Enter player one question...
      askQuestion
      echo "$pone : $question"
      read aone
	   ## Check if the answer is correct...
       if [ "$aone" = "$answer" ];
       then
        ## If using LEDs then light up according to current score...
        if [ "$gpioa" = "y" ];
        then
          gpio write $ponegpio 1
        else
          echo ""
        fi
        echo "Well done $pone! Correct answer. Your score is $ponescore..."
		## Change to player two...
        i=2
		## Update the gpio and score...
        ponegpio=$(($ponegpio + 1))
        ponescore=$(($ponescore + 1))
        sleep 5
		## Clear the screen
        clear
        continue
	   ## Check if the answer is incorrect...
       else
        echo "Oops! Wrong answer..."
        sleep 5
		## Clear the screen...
        clear
		## Keep the current LED on...
        ponegpio="$ponegpio"
		## Change to player two...
        i=2
        continue
       fi
    fi
	## As above, but this is for player two...
    if [[ $i -eq 2 ]]
    then
      ## Enter player two question...
      askQuestion
      echo "$ptwo : $question"
      read answertwo
       if [ "$answertwo" = "$answer" ];
       then
        if [ "$gpioa" = "y" ];
        then
          gpio write $ptwogpio 1
        else
          echo ""
        fi
        echo "Well done $ptwo! Correct answer. Your score is $ptwoscore..."
        i=1
        ptwogpio=$(($ptwogpio + 1))
        ptwoscore=$(($ptwoscore + 1))
        sleep 5
        clear
        continue
       else
        echo "Oops! Wrong answer..."
        ptwogpio="$ptwogpio"
        sleep 5
        clear
        i=1
        continue
       fi
    fi
   fi
  done
 }

Now we just call all of our functions and play the game:

## Run the game (Instigate our functions)...
while true
do
 ## Run the setup function...
 setup
 ## Run the intro function...
 intro
 ## Ask if we have gpio's connected...
 hardware
 ## If using gpio (LEDs) then test the LEDS...
 if [ "$gpioa" = "y" ];
 then
  testled
 ## Otherwise ingnore the gpio bit...
 else
  echo ""
 fi
 sleep 5
 ## Ask the players names...
 playerone
 playertwo
 ## Ask the questions...
 playthegame
done

All of this needs to be saved to a file called piquiz.sh, CHMOD’d and can be run from a terminal.

Happy quizzing 😀

This entry was posted in bash, GPIO, LED, raspberry pi, Scripting, Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *