News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Math Quiz

Started by kevin, April 25, 2010, 08:48:52 AM

Previous topic - Next topic

kevin

 Math Quiz


 This example is a mini math quiz. The game asks the player a number of multiple choice questions, the player has to answer them as quickly as possible.  At the end  the game tells you how long it took to complete the quiz.  While it's not much of game, the example helps demonstrate how arrays can be used to store and iterate though a list of questions..  



PlayBASIC Code: [Select]
    Dim Question$(5)         ; the list of the questions

Dim AnswerOptions$(5,3) ; Give the player a choice of 3 answers

DIm CorrectAnswer(5) ;


// Init the Questions and answer arrays

// Question #1
Question$(1) ="What's 1 + 1 ?"
AnswerOptions$(1,1) ="11"
AnswerOptions$(1,2) ="2"
AnswerOptions$(1,3) ="50"
CorrectAnswer(1) =2

// Question #2
Question$(2) ="What's 20 / 2 ?"
AnswerOptions$(2,1) ="10"
AnswerOptions$(2,2) ="18"
AnswerOptions$(2,3) ="5"
CorrectAnswer(2) =1


// Question #3
Question$(3) ="What's 100 * 6 ?"
AnswerOptions$(3,1) ="106"
AnswerOptions$(3,2) ="6000"
AnswerOptions$(3,3) ="600"
CorrectAnswer(3) =3


// Question #4
Question$(4) ="What's 97 - 8?"
AnswerOptions$(4,1) ="91"
AnswerOptions$(4,2) ="90"
AnswerOptions$(4,3) ="89"
CorrectAnswer(4) =3


// Question #5
Question$(5) ="What's 1234 + 123?"
AnswerOptions$(5,1) ="1234123"
AnswerOptions$(5,2) ="1357"
AnswerOptions$(5,3) ="1349"
CorrectAnswer(5) =2




LoadFont "Arial",1,48,0

Setfps 30
TotalTime=0

For CurrentQuestion=1 to 5

;
q$=Question$(CurrentQuestion)
Correct=false

StartOfQuestion=Timer()
While Correct=False and q$<>""

; draw the backdrop
c1= rgb(20,40,80)
c2= rgb(120,40,00)
shadebox 0,0,800,600,c1,c1,c2,c2


ink rgb(255,255,255)
CenterText 400,15, "Math Quiz Speed Test"


ink rgb(255,255,255)
CenterText 400,120, "Question: " +q$

ink rgb(255,0,255)
For options=1 to 3
Xpos=200+(options-1)*200
s$="("+mid$("ABC",Options)+") "+AnswerOptions$(CurrentQuestion,options)
CenterText Xpos,200,s$
next


Key$=lower$(inkey$())
if Key$<>""
Answer=0
If key$="a" then Answer=1
If key$="b" then Answer=2
If key$="c" then Answer=3
if Answer<>0
ink rgb(200,200,200)
if CorrectAnswer(CurrentQuestion)=Answer
TotalTime+=Timer()-StartOfQuestion
Correct=true
centertext 400,400,"Correct !"
else
s$="Sorry "
s$+=AnswerOptions$(CurrentQuestion,Answer)
s$+=" Is incorrect"
centertext 400,400,s$
centertext 400,450,"Try Again !"
endif
Sync
wait 1000
endif
endif

Sync
EndWhile

Flushkeys
next


; Show the game over screen.
Cls rgb(0,55,0)
ink rgb(200,200,200)
Centertext 400,100,"Game Over"

Centertext 400,200,"You Completed The Quiz In"

centertext 400,300,str$(TotalTime/1000.0)+" seconds"
Sync
waitkey

end