StartMenuSelection = 1
ShowMultiStats = false
CanUseFifty = true
CanUsePhone = true
CanUseAudience = true
Turn = 1
PlayerLifeLines = { }

startscreenimage = Image.load("Images/titlescreen.png")
startCustom = Image.load("Images/Custom.png")
startReset = Image.load("Images/Reset.png")
startTwo = Image.load("Images/Two.png")
startSingle = Image.load("Images/single.png")
startSelector = Image.load("Images/menuselection.png")
Timer = Timer.new(0)
Timer:stop()
Timer:reset()
gosound = Sound.load("Sound/gameover.wav")
gosound:play()

Millionaire = {}
Stats100 = {}
Stats200 = {}
Stats300 = {}
Stats500 = {}
Stats1000 = {}
Stats2000 = {}
Stats4000 = {}
Stats8000 = {}
Stats16000 = {}
Stats32000 = {}
Stats64000 = {}
Stats125000 = {}
Stats250000 = {}
Stats500000 = {}
Stats1000000 = {}

math.randomseed(os.time())

--Set up Font
font = Image.load("Images/font.png")
LetterPixelsWide = 8
LetterPixelsTall = 9
InputString = [[ !"#$%&'()* ,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
function char2font(char_input) 
	
	IndexNumber = 0
	for i = 1, string.len(InputString) do
		if string.sub(InputString, i,i) == char_input then
			return IndexNumber
		end -- end if
		IndexNumber = IndexNumber + LetterPixelsWide
	end -- end if
	return 0
end
function blitChar(target, char, posx, posy)
	target:blit(posx,posy, font,char2font(char),0,LetterPixelsWide,LetterPixelsTall,true)
end
function blitString(myTarget, possx, possy, thestring)
	incX = 0
	for w = 1, string.len(thestring) do
	blitChar(myTarget, string.upper(string.sub(thestring, w,w)), possx + incX, possy)
		incX = incX + LetterPixelsWide
	end -- end for
end

--Load Images
black = Color.new(0,0,0)
poll = Image.load("Images/poll.png")
background = Image.load("Images/1.png")
diamond = Image.load("Images/diamond.png")
playeranswera = Image.load("Images/playeranswera.png")
playeranswerb = Image.load("Images/playeranswerb.png")
playeranswerc = Image.load("Images/playeranswerc.png")
playeranswerd = Image.load("Images/playeranswerd.png")
rightanswera = Image.load("Images/rightanswera.png")
rightanswerb = Image.load("Images/rightanswerb.png")
rightanswerc = Image.load("Images/rightanswerc.png")
walkyes = Image.load("Images/walkyes.png")
walkno = Image.load("Images/walkno.png")
rightanswerd = Image.load("Images/rightanswerd.png")
check = Image.load("Images/check.png")
xmark = Image.load("Images/x.png")
audience = Image.load("Images/audience.png")
fifty = Image.load("Images/5050.png")
phone = Image.load("Images/phone.png")
lifemenuyes = Image.load("Images/lifelineyes.png")
lifemenuno = Image.load("Images/lifelineno.png")
solidback = Image.load("Images/background.png")
moneypic = Image.load("Images/2.png")
selectorcolor = "grey"
yellowselector = Image.load("Images/yellow.png")
greyselector = Image.load("Images/grey.png")
pad = Controls.read()
oldpad = Controls.read()

--sounds
hbsound = Sound.load("Sound/background1.wav",true)
hbsound2 = nil
hbsound3 =  Sound.load("Sound/background3.wav",true)
finalsound = nil
local sound = hbsound
nextq = Sound.load("Sound/next.wav")
voice = sound:play()
fiftysound = Sound.load("Sound/50.wav")
phonesound = Sound.load("Sound/phone.wav")
correctsound = Sound.load("Sound/correct.wav")
nextq1 = nil
wrongsound = Sound.load("Sound/wrong.wav")
audipoll = Sound.load("Sound/poll.wav",true)


-- Variables
VarsSet = "false"
GameOver = "false"
restartcheck = false
-- dofile("./questionvars.lua") 
startscreen = true


function SetVariables()
if StartMenuSelection == 1 then
PlayerLifeLines[1] = { audience = 1, phone = 1, fifty = 1 }
end
ClearStatus = false
FirstResponse = false
answerselect = "a"
gocheck = false
pscheck = false
wscheck = false
cscheck = false
PollTotal = 0
Audience = {}
Audience[1] = 0
Audience[2] = 0
Audience[3] = 0
Audience[4] = 0
WonAMillion = false
ChooseLife = "audience"
fiftychoices = 0 -- number of choices used so far for 50/50
fiftyrandom = 0
lastfifty = 0
FriendAnswer = ""
RanPhoneAnswer = false
PhonePercent = 0
PerformingLifeLine = false
LifeMenuOn = false -- Is menu on? true or false
LifeYes = true 
LifeNo = false
WalkYes = false
WalkNo = true
WalkMenuOn = false
menutimer = os.time()
PickLifeLine = false
AudienceCalculation = false
AnswerNum = 1
winamount = "0"
Ranstopper = true --to get random number once for answer response
checkx = -500 -- check image x position
score = { 0,0 } -- Player[1] and Player[2] scores
selectorx = 375
selectory = { 266,266 }
printscore = { "0", "0" } -- current score with comma
printnext = { "100", "100" } -- next score with comma

timercheck = 0
GotMoney = "false"
RandNum = 0 -- Random Number to get a random question
Answer = "" -- Answer to Question
timer = os.time() -- Timer for Keeping buttons from being pressed too fast
PlayerGuess = "" -- Player's answer
GuessStatus = ""
CanGuess = "true"
Difficulty = { 100, 100 }
GetQuestion = "false" -- True if Ready to Get a New Question

end

-- Functions

function MultiVarSet()
ChooseLife = "audience"
fiftychoices = 0 -- number of choices used so far for 50/50
fiftyrandom = 0
lastfifty = 0
FriendAnswer = ""
RanPhoneAnswer = false
PhonePercent = 0
PerformingLifeLine = false
LifeMenuOn = false -- Is menu on? true or false
LifeYes = true 
LifeNo = false
WalkYes = false
WalkNo = true
WalkMenuOn = false
menutimer = os.time()
PickLifeLine = false
AudienceCalculation = false
ShowMultiStats = true
GetQuestion = "true"
PerformingLifeLine = false
PickLifeLine = false
AudienceCalculation = false
PollTotal = 0
Audience[1] = 0
Audience[2] = 0
Audience[3] = 0
Audience[4] = 0
end

function NextQuestionSounds()
if RandNum ~= 0 then
if nextq1 == nil then nextq1 = Sound.load("Sound/correct1.wav") end
if score[Turn] < 1000  then
voice:stop()
local sound = nextq1
voice = sound:play()
end
if score[Turn] >= 1000 then
voice:stop()
local sound = nextq
voice = sound:play()
end
end
end
--end
function buttoninput()
	pad = Controls.read()
end

function SelectorInfo()
if score[Turn] == 500 or score[Turn] == 16000 or score[Turn] == 500000 then 
selectorcolor = "yellow"
else
selectorcolor = "grey"
end
end


function DrawLifeLineMenu()
if LifeMenuOn == true and LifeYes == true then screen:blit(20,10,lifemenuyes) end
if LifeMenuOn == true and LifeNo == true then screen:blit(20,10,lifemenuno) end
if WalkMenuOn == true and WalkYes == true then screen:blit(20,10,walkyes) end
if WalkMenuOn == true and WalkNo == true then screen:blit(20,10,walkno) end
if PickLifeLine == true and ChooseLife == "audience" then screen:blit(20,10,audience) end
if PickLifeLine == true and ChooseLife == "fifty" then screen:blit(20,10,fifty) end
if PickLifeLine == true and ChooseLife == "phone" then screen:blit(20,10,phone) end
if PickLifeLine == true and CanUseFifty == false  then  screen:blit(224,126,xmark) end
if PickLifeLine == true and CanUsePhone == false  then  screen:blit(272,126,xmark) end
if PickLifeLine == true and CanUseAudience == false  then  screen:blit(179,126,xmark) end
if PickLifeLine == true and PlayerLifeLines[Turn].fifty == 0 then screen:blit(224,126,xmark) end
if PickLifeLine == true and PlayerLifeLines[Turn].phone == 0 then screen:blit(272,126,xmark) end
if PickLifeLine == true and PlayerLifeLines[Turn].audience == 0 then screen:blit(179,126,xmark) end
end

function RandomCalcs()
if StartMenuSelection == 1 or StartMenuSelection == 2 then file = io.open("Qstats.lua", "w") end
if StartMenuSelection == 4 then file = io.open("Custom/Qstats.lua", "w") end
for x = 1,Value1 do
if Stats100[x] == 1 then
if StartMenuSelection == 1 or StartMenuSelection == 2 then file:write("Stats100[" .. x .. "]  = 1 \n") end
if StartMenuSelection == 4 then file:write("Stats100[" .. x .. "]  = 1 \n") end
end
end
for x = 1,Value2 do
if Stats200[x] == 1 then
file:write("Stats200[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value3 do
if Stats300[x] == 1 then
file:write("Stats300[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value4 do
if Stats500[x] == 1 then
file:write("Stats500[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value5 do
if Stats1000[x] == 1 then
file:write("Stats1000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value6 do
if Stats2000[x] == 1 then
file:write("Stats2000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value7 do
if Stats4000[x] == 1 then
file:write("Stats4000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value8 do
if Stats8000[x] == 1 then
file:write("Stats8000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value9 do
if Stats16000[x] == 1 then
file:write("Stats16000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value10 do
if Stats32000[x] == 1 then
file:write("Stats32000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value11 do
if Stats64000[x] == 1 then
file:write("Stats64000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value12 do
if Stats125000[x] == 1 then
file:write("Stats125000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value13 do
if Stats250000[x] == 1 then
file:write("Stats250000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value14 do
if Stats500000[x] == 1 then
file:write("Stats500000[" .. x .. "]  = 1 \n")
end
end
for x = 1,Value15 do
if Stats1000000[x] == 1 then
file:write("Stats1000000[" .. x .. "]  = 1 \n")
end
end
file:close()
end

-- Show the multiplayer stats screen
function MultiStats()
screen:blit(0,0,MultiScreen)
for a = 1,2 do
if score[a] == 1000 or score[a] == 32000 or score[a] == 1000000  then
 if score[a] > 0 then screen:blit(StartSelector[a].x,StartSelector[a].y,yellowselector) end
else
if score[a] > 0 then screen:blit(StartSelector[a].x,StartSelector[a].y,greyselector) end
 end

end
screen:blit(0,0,MultiScores)
--Player 1 Life Line Info
blitString(screen,80,50, PlayerLifeLines[1].audience)
blitString(screen,113,50, PlayerLifeLines[1].fifty)
blitString(screen,147,50, PlayerLifeLines[1].phone)

--Player 2 Life Line Info
blitString(screen,320,50, PlayerLifeLines[2].audience)
blitString(screen,354,50, PlayerLifeLines[2].fifty)
blitString(screen,390,50, PlayerLifeLines[2].phone)
if Timer:time() < 500 then
if Turn == 1 then blitString(screen,208,210, "Player 1") else blitString(screen,196,210, "Player 2") end
blitString(screen,178,230, "Press X To Play")
end
if Timer:time() > 1000 then 
Timer:stop()
Timer:reset()
Timer:start()
end
end

function GetRandQuestion()
if StartMenuSelection == 4 then dofile("./Custom/questionvars.lua") end
if StartMenuSelection == 1 or StartMenuSelection == 2 then 
CanUseFifty = true
CanUsePhone = true
CanUseAudience = true
dofile("./questionvars.lua") end
cscheck = false
wscheck = false
if Difficulty[Turn] == 100 then 
if Counts100 == Value1 then
for x = 1,Value1 do
Stats100[x]  = 0
end
Counts100 = 0
RandomCalcs()
end
 if StartMenuSelection == 1 or StartMenuSelection == 2 then
dofile("./Questions/100.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection == 4 then
dofile("./Custom/Questions/100.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value1) 
if Stats100[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats100[RandNum] = 1
Counts100 = Counts100 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 200 then 
if Counts200 == Value2 then
for x = 1,Value2 do
Stats200[x]  = 0
end
Counts200 = 0
RandomCalcs()
end
  if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/200.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/200.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value2) 
if Stats200[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats200[RandNum] = 1
Counts200 = Counts200 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 300 then 
if Counts300 == Value3 then
for x = 1,Value3 do
Stats300[x]  = 0
end
Counts300 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/300.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/300.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value3) 
if Stats300[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats300[RandNum] = 1
Counts300 = Counts300 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 500 then 
if Counts500 == Value4 then
for x = 1,Value4 do
Stats500[x]  = 0
end
Counts500 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/500.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/500.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value4) 
if Stats500[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats500[RandNum] = 1
Counts500 = Counts500 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 1000 then 
if Counts1000 == Value5 then
for x = 1,Value5 do
Stats1000[x]  = 0
end
Counts1000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/1000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/1000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value5) 
if Stats1000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats1000[RandNum] = 1
Counts1000 = Counts1000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 2000 then 
if Counts2000 == Value6 then
for x = 1,Value6 do
Stats2000[x]  = 0
end
Counts2000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/2000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/2000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value6) 
if Stats2000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats2000[RandNum] = 1
Counts2000 = Counts2000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 4000 then 
if Counts4000 == Value7 then
for x = 1,Value7 do
Stats4000[x]  = 0
end
Counts4000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/4000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/4000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value7) 
if Stats4000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats4000[RandNum] = 1
Counts4000 = Counts4000 + 1
RandomCalcs()
end
 end
if Difficulty[Turn] == 8000 then 
if Counts8000 == Value8 then
for x = 1,Value8 do
Stats8000[x]  = 0
end
Counts8000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/8000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/8000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value8) 
if Stats8000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats8000[RandNum] = 1
Counts8000 = Counts8000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 16000 then 
if Counts160000 == Value9 then
for x = 1,Value9 do
Stats16000[x]  = 0
end
Counts16000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/16000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/16000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value9) 
if Stats16000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats16000[RandNum] = 1
Counts16000 = Counts16000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 32000 then 
if Counts32000 == Value10 then
for x = 1,Value10 do
Stats32000[x]  = 0
end
Counts32000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/32000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/32000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value10) 
if Stats32000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats32000[RandNum] = 1
Counts32000 = Counts32000 + 1
RandomCalcs()
end
 end
if Difficulty[Turn] == 64000 then 
 if Counts64000 == Value11 then
for x = 1,Value11 do
Stats64000[x]  = 0
end
Counts64000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/64000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/64000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value11) 
if Stats64000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats64000[RandNum] = 1
Counts64000 = Counts64000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 125000 then 
if Counts125000 == Value12 then
for x = 1,Value12 do
Stats125000[x]  = 0
end
Counts125000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/125000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/125000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value12) 
if Stats125000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats125000[RandNum] = 1
Counts125000 = Counts125000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 250000 then 
if Counts250000 == Value13 then
for x = 1,Value13 do
Stats250000[x]  = 0
end
Counts250000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/250000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/250000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value13) 
if Stats250000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats250000[RandNum] = 1
Counts250000 = Counts250000 + 1
RandomCalcs()
end
end
if Difficulty[Turn] == 500000 then 
if Counts500000 == Value14 then
for x = 1,Value14 do
Stats500000[x]  = 0
end
Counts500000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/500000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/500000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value14) 
if Stats500000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats500000[RandNum] = 1
Counts500000 = Counts500000 + 1
RandomCalcs()
end
 end
if Difficulty[Turn] == 1000000 then 
if Counts1000000 == Value15 then
for x = 1,Value15 do
Stats1000000[x]  = 0
end
Counts1000000 = 0
RandomCalcs()
end
 if StartMenuSelection ==1 or StartMenuSelection == 2 then
dofile("./Questions/1000000.lua")
 dofile("./Qstats.lua")
end
 if StartMenuSelection ==4 then
dofile("./Custom/Questions/1000000.lua")
 dofile("./Custom/Qstats.lua")
end
RandNum = math.random(1,Value15) 
if Stats1000000[RandNum] == 1 then RandNum = 0 end
if RandNum ~= 0 then
Stats1000000[RandNum] = 1
Counts1000000 = Counts1000000 + 1
RandomCalcs()
end
end
if StartMenuSelection == 1 or StartMenuSelection == 2 then file = io.open("Counts.lua", "w")  end
if StartMenuSelection == 4 then file = io.open("Custom/Counts.lua", "w")  end
file:write("Counts100 = " .. Counts100 .. "\n")
file:write("Counts200 = " .. Counts200 .. "\n")
file:write("Counts300 = " .. Counts300 .. "\n")
file:write("Counts500 = " .. Counts500 .. "\n")
file:write("Counts1000 = " .. Counts1000 .. "\n")
file:write("Counts2000 = " .. Counts2000 .. "\n")
file:write("Counts4000 = " .. Counts4000 .. "\n")
file:write("Counts8000 = " .. Counts8000 .. "\n")
file:write("Counts16000 = " .. Counts16000 .. "\n")
file:write("Counts32000 = " .. Counts32000 .. "\n")
file:write("Counts64000 = " .. Counts64000 .. "\n")
file:write("Counts125000 = " .. Counts125000 .. "\n")
file:write("Counts250000 = " .. Counts250000 .. "\n")
file:write("Counts500000 = " .. Counts500000 .. "\n")
file:write("Counts1000000 = " .. Counts1000000 .. "\n")
file:write("TotalMillion  = " .. TotalMillion .. "\n")
file:close()
if StartMenuSelection == 1 then
NextQuestionSounds()
end
if RandNum ~= 0 then
ta = string.lower(Millionaire[RandNum].a)
tb = string.lower(Millionaire[RandNum].b)
tc = string.lower(Millionaire[RandNum].c)
td = string.lower(Millionaire[RandNum].d)

tr = math.random(1,5)

if tr == 1 then
  Millionaire[RandNum].a = td
  Millionaire[RandNum].b = tc
  Millionaire[RandNum].c = tb
  Millionaire[RandNum].d = ta
end
if tr == 2 then
  Millionaire[RandNum].a = tb
  Millionaire[RandNum].b = ta
  Millionaire[RandNum].c = td
  Millionaire[RandNum].d = tc
end
if tr == 3 then
  Millionaire[RandNum].a = tc
  Millionaire[RandNum].b = tb
  Millionaire[RandNum].c = ta
  Millionaire[RandNum].d = td
end
if tr == 4 then
  Millionaire[RandNum].a = tc
  Millionaire[RandNum].b = td
  Millionaire[RandNum].c = tb
  Millionaire[RandNum].d = ta
end
end

if RandNum ~= 0 then
if score[Turn] == 0 then selectory[Turn] = 266 end
if score[Turn] == 100 then selectory[Turn] = 253 end
if score[Turn] == 200 then selectory[Turn] = 240 end
if score[Turn] == 300 then selectory[Turn] = 227 end
if score[Turn] == 500 then selectory[Turn] = 214 end
if score[Turn] == 1000 then selectory[Turn] = 201 end
if score[Turn] == 2000 then selectory[Turn] = 188 end
if score[Turn] == 4000 then selectory[Turn] = 175 end
if score[Turn] == 8000 then selectory[Turn] = 162 end
if score[Turn] == 16000 then selectory[Turn] = 149 end
if score[Turn] == 32000 then selectory[Turn] = 136 end
if score[Turn] == 64000 then selectory[Turn] = 123 end
if score[Turn] == 125000 then selectory[Turn] = 112 end
if score[Turn] == 250000 then selectory[Turn] = 99 end
if score[Turn] == 500000 then selectory[Turn] = 84 end
if score[Turn] == 1000000 then selectory[Turn] = 73 end
selectory[Turn] = selectory[Turn] - 12
SelectorInfo()
GetQuestion = "false" 
GotMoney = "false"
GotDissed = "true"
RanStopper = true
answerselect = "a"
FirstResponse = false
PlayerGuess =  string.lower(Millionaire[RandNum].a)
end
end



function PrintQuestion()
if LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false  then 
if answerselect == "a" then 
screen:blit(0,0,playeranswera)
end
if answerselect == "c" then 
screen:blit(0,0,playeranswerc)
end
if answerselect == "b" then 
screen:blit(0,0,playeranswerb)
end
if answerselect == "d" then 
screen:blit(0,0,playeranswerd)
end
end
blitString(screen,22,100,Millionaire[RandNum].questiona)
blitString(screen,22,110,Millionaire[RandNum].questionb)
blitString(screen,22,120,Millionaire[RandNum].questionc)
blitString(screen,13,173, Millionaire[RandNum].a)
blitString(screen,13,213, Millionaire[RandNum].b)
blitString(screen,198,173,Millionaire[RandNum].c)
blitString(screen,198,213,Millionaire[RandNum].d)
if StartMenuSelection == 2 then blitString(screen,10,40, "Player " .. Turn) end
Answer = string.lower(Millionaire[RandNum].answer)
end

function DrawScreen()
screen:blit(0,0,background)
if selectorcolor == "grey" then screen:blit(selectorx,selectory[Turn],greyselector)  end
if selectorcolor == "yellow" then screen:blit(selectorx,selectory[Turn],yellowselector)  end

screen:blit(0,0,moneypic,true)
if score[Turn] > 0 then screen:blit(0,0,diamond) end
if score[Turn] > 100 then screen:blit(0,0-13,diamond) end
if score[Turn] > 200 then screen:blit(0,0-13*2,diamond) end
if score[Turn] > 300 then screen:blit(0,0-13*3,diamond) end
if score[Turn] > 500 then screen:blit(0,0-13*4,diamond) end
if score[Turn] > 1000 then screen:blit(0,0-13*5,diamond) end
if score[Turn] > 2000 then screen:blit(0,0-13*6,diamond) end
if score[Turn] > 4000 then screen:blit(0,0-13*7,diamond) end
if score[Turn] > 8000 then screen:blit(0,0-13*8,diamond) end
if score[Turn] > 16000 then screen:blit(0,0-13*9,diamond) end
if score[Turn] > 32000 then screen:blit(0,0-13*10,diamond) end
if score[Turn] > 64000 then screen:blit(0,0-13*11,diamond) end
if score[Turn] > 125000 then screen:blit(0,0-13*12,diamond) end
if score[Turn] > 250000 then screen:blit(0,0-13*13,diamond) end
if score[Turn] > 500000 then screen:blit(0,0-13*14,diamond) end
if PlayerLifeLines[Turn].fifty == 0 or CanUseFifty == false then screen:blit(414,45,xmark) end
if PlayerLifeLines[Turn].phone == 0 or CanUsePhone == false then screen:blit(448,45,xmark) end
if PlayerLifeLines[Turn].audience == 0 or CanUseAudience == false then screen:blit(381,45,xmark) end
end

function StartMenu()
screen:blit(0,0,startscreenimage) 
if StartMenuSelection == 1 or StartMenuSelection == 2 then 
screen:blit(135,180,startSingle) 
end
if StartMenuSelection == 2 then 
screen:blit(135,180,startTwo) 
end
if StartMenuSelection == 3 then 
screen:blit(135,180,startReset) 
end
if StartMenuSelection == 4 then 
screen:blit(135,180,startCustom) 
end
end

function PerformLifeLine()
if PlayerLifeLines[Turn].fifty == 0 and ChooseLife == "fifty" then PerformingLifeLine = false end
if PlayerLifeLines[Turn].phone == 0 and ChooseLife == "phone" then PerformingLifeLine = false end
if PlayerLifeLines[Turn].audience == 0 and ChooseLife == "audience" then PerformingLifeLine = false end
if ChooseLife == "fifty" and CanUseFifty == false then PerformingLifeLine = false end
if ChooseLife == "fifty" and PlayerLifeLines[Turn].fifty ~= 0 and CanUseFifty == true then
if menuchecker < 2 and PlayerLifeLines[Turn].fifty ~= 0 and ChooseLife == "fifty" and PerformingLifeLine == true then blitString(screen,20,70, "Let's take away two wrong answers.") end
if ChooseLife == "fifty"  and PlayerLifeLines[Turn].fifty ~= 0 and menuchecker > 2 and PerformingLifeLine == true then
if fiftychoices < 2 then 
fiftyrandom = math.random(1,4) 
if fiftyrandom == 1 and lastfifty ~= fiftyrandom and string.lower(Millionaire[RandNum].a) ~= Answer and PerformingLifeLine == true then 
 fiftysound:play()
Millionaire[RandNum].a = "" 
fiftychoices = fiftychoices +1
lastfifty = fiftyrandom
end
if fiftyrandom == 2 and lastfifty ~=  fiftyrandom and string.lower(Millionaire[RandNum].b) ~= Answer and PerformingLifeLine == true then 
 fiftysound:play()
Millionaire[RandNum].b = ""  
fiftychoices = fiftychoices +1
lastfifty = fiftyrandom
end
if fiftyrandom == 3 and lastfifty ~=  fiftyrandom and string.lower(Millionaire[RandNum].c) ~= Answer and PerformingLifeLine == true then 
 fiftysound:play()
Millionaire[RandNum].c = ""  
fiftychoices = fiftychoices +1
lastfifty = fiftyrandom
end
if fiftyrandom == 4 and lastfifty ~=  fiftyrandom and string.lower(Millionaire[RandNum].d) ~= Answer and PerformingLifeLine == true  then 
 fiftysound:play()
Millionaire[RandNum].d = ""  
fiftychoices = fiftychoices +1
lastfifty = fiftyrandom
end
end
end

if fiftychoices == 2 then 
PerformingLifeLine = false
PlayerLifeLines[Turn].fifty = PlayerLifeLines[Turn].fifty - 1
CanUseFifty = false
lastfifty = 0
fiftychoices = 0 
answerselect = "a"
PlayerGuess =  string.lower(Millionaire[RandNum].a)
end
end

-- Phone A Friend
if PerformingLifeLine == true and ChooseLife == "phone"  then
if CanUsePhone == false then PerformingLifeLine = false end
if ChooseLife == "phone" and RanPhoneAnswer ~= true  and CanUsePhone == true then
menutimer = os.time()
math.randomseed(os.time())
tempnum = math.random(2,2)
if tempnum == 1  then FriendAnswer = Millionaire[RandNum].answer end
if tempnum == 2  then 
anothernum = math.random(1,8)
if anothernum == 1  then FriendAnswer = Millionaire[RandNum].a end
if anothernum == 2  then FriendAnswer = Millionaire[RandNum].b end
if anothernum == 3  then FriendAnswer = Millionaire[RandNum].c end
if anothernum == 4  then FriendAnswer = Millionaire[RandNum].d end
if anothernum == 5  then FriendAnswer = Millionaire[RandNum].answer end
if anothernum == 6  then FriendAnswer = Millionaire[RandNum].answer end
if anothernum == 7  then FriendAnswer = Millionaire[RandNum].answer end
if anothernum == 8  then FriendAnswer = Millionaire[RandNum].answer end
end
if FriendAnswer ~= "" then RanPhoneAnswer = true end
end

if menuchecker < 3 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Let's call up your friend for help!") 
end
if menuchecker == 2 and pscheck == false then 
phonesound:play() 
pscheck = true
end
if menuchecker > 3 and menuchecker <= 6 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Friend: Hello?") 
end

if menuchecker > 6 and menuchecker <= 10 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Host: Hi there. We need your help.") 
end

if menuchecker > 10 and menuchecker <= 14 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Player: Do you think you know the answer?") 
end

if menuchecker > 14 and menuchecker <= 18 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Friend: I'm gonna go with ".. FriendAnswer .. ".") 
end

if menuchecker > 18 and menuchecker <= 22 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Player: OK, thanks. ") 
end

if menuchecker > 22 and menuchecker <= 26 and PlayerLifeLines[Turn].phone ~= 0 and ChooseLife == "phone" then 
blitString(screen,20,70, "Host: Well he thinks it's "..FriendAnswer.. ".") 
end
if menuchecker > 26 then
CanUsePhone = false
 PerformingLifeLine = false 
PlayerLifeLines[Turn].phone = PlayerLifeLines[Turn].phone - 1
end
end
if PerformingLifeLine == true then
if ChooseLife == "audience" and CanUseAudience == false then PerformingLifeLine = false end
if ChooseLife == "audience" and AudienceCalculation == false and CanUseAudience == true then
voice:stop()
local sound = audipoll
voice = sound:play()
menutimer = os.time()
MathRandom = math.random(1,10)
if MathRandom <= 7  then 
if Answer == Millionaire[RandNum].a and Millionaire[RandNum].a ~= "" then 
Audience[1] = 50 
AudienceCalculation = true
PollTotal = PollTotal + 50
end
if Answer == Millionaire[RandNum].b and Millionaire[RandNum].b ~= "" then 
Audience[2] = 50
AudienceCalculation = true
PollTotal = PollTotal + 50
end
if Answer == Millionaire[RandNum].c and Millionaire[RandNum].c ~= "" then
Audience[3] = 50
AudienceCalculation = true
PollTotal = PollTotal + 50
end
if Answer == Millionaire[RandNum].d and Millionaire[RandNum].d ~= "" then 
Audience[4] = 50
AudienceCalculation = true
PollTotal = PollTotal + 50
end
end
AudienceCalculation = true
end
if PollTotal < 100 and AudienceCalculation == true then
MathRandom = math.random(1,4)
if MathRandom ==1 and Millionaire[RandNum].a ~= "" then 
Audience[1] = Audience[1] + 2
PollTotal = PollTotal + 2
end
if MathRandom ==2 and Millionaire[RandNum].b ~= "" then 
Audience[2] = Audience[2] + 2
PollTotal = PollTotal + 2
end
if MathRandom ==3 and Millionaire[RandNum].c ~= "" then 
Audience[3] = Audience[3] + 2 
PollTotal = PollTotal + 2
end
if MathRandom ==4 and Millionaire[RandNum].d ~= "" then 
Audience[4] = Audience[4] + 2
PollTotal = PollTotal + 2
end
end
end
if  PlayerLifeLines[Turn].audience ~= 0 and ChooseLife == "audience" and PollTotal < 100  then
screen:blit(0,0,poll)
blitString(screen,130,120,Millionaire[RandNum].a..": ".."Waiting...")
blitString(screen,130,130,Millionaire[RandNum].b..": ".."Waiting...")
blitString(screen,130,140,Millionaire[RandNum].c..": ".."Waiting...")
blitString(screen,130,150,Millionaire[RandNum].d..": ".."Waiting...")
end
if  PlayerLifeLines[Turn].audience ~= 0 and ChooseLife == "audience" and PollTotal == 100  then
screen:blit(0,0,poll)
blitString(screen,130,120,Millionaire[RandNum].a..": "..Audience[1].."%")
blitString(screen,130,130,Millionaire[RandNum].b..": "..Audience[2].."%")
blitString(screen,130,140,Millionaire[RandNum].c..": "..Audience[3].."%")
blitString(screen,130,150,Millionaire[RandNum].d..": "..Audience[4].."%")
end
if PollTotal == 100 and pad:cross()   and ChooseLife == "audience" and oldpad:cross() ~= pad:cross() then 
voice:stop()
menutimer = os.time()
CanUseAudience = false
PlayerLifeLines[Turn].audience = PlayerLifeLines[Turn].audience - 1
PerformingLifeLine = false
end

end

function EndScoreCalcs()
if score[Turn] < 1000 then
score[Turn] = 0 
Difficulty[Turn] = 100
printscore[Turn] = "0"
end
if score[Turn] > 1000 and score[Turn] < 32000 then 
score[Turn] = 1000 
Difficulty[Turn] = 2000
printscore[Turn] = "1,000"
end
if score[Turn] > 32000 then 
score[Turn] = 32000 
Difficulty[Turn] = 64000
printscore[Turn] = "32,000"
end
end

function AnswerCheck()
if finalsound == nil then finalsound = Sound.load("Sound/final.wav") end
if PlayerGuess == string.lower(Millionaire[RandNum].answer) and GotMoney == "false"  and timercheck > 2 then
GotMoney = "true"
if score[Turn] == 500000 then 
score[Turn] = 1000000
printscore[Turn] = "1,000,000"
printnext[Turn] = "1,000,000"
TotalMillion = TotalMillion + 1
end
if score[Turn] == 250000 then 
Difficulty[Turn] = 1000000
score[Turn] = 500000
printscore[Turn] = "500,000"
printnext[Turn] = "1,000,000"
end
if score[Turn] == 125000 then 
Difficulty[Turn] = 500000
score[Turn] = 250000
printscore[Turn] = "250,000"
printnext[Turn] = "500,000"
 end
if score[Turn] == 64000 then 
Difficulty[Turn] = 250000
score[Turn] = 125000
printscore[Turn] = "125,000"
printnext[Turn] = "250,000"
end
if score[Turn] == 32000 then 
Difficulty[Turn] = 125000
score[Turn] = 64000
printscore[Turn] = "64,000"
printnext[Turn] = "125,000"
end
if score[Turn] == 16000 then 
Difficulty[Turn] = 64000
score[Turn] = 32000
printscore[Turn] = "32,000"
printnext[Turn] = "64,000"
end
if score[Turn] == 8000 then 
Difficulty[Turn] = 32000
score[Turn] = 16000
printscore[Turn] = "16,000"
printnext[Turn] = "32,000"
end
if score[Turn] == 4000 then 
Difficulty[Turn] = 16000
score[Turn] = 8000
printscore[Turn] = "8,000"
printnext[Turn] = "16,000"
end
if score[Turn] == 2000 then 
Difficulty[Turn] = 8000
score[Turn] = 4000
printscore[Turn] = "4,000"
printnext[Turn] = "8,000"
 end
if score[Turn] == 1000 then 
Difficulty[Turn] = 4000
score[Turn] = 2000
printscore[Turn] = "2,000"
printnext[Turn] = "4,000"
end
if score[Turn] == 500 then 
Difficulty[Turn] = 2000
score[Turn] = 1000
printscore[Turn] = "1,000"
printnext[Turn] = "2,000"
if hbsound2 == nil then hbsound2 = Sound.load("Sound/background2.wav",true) end
if hbsound3 == nil then hbsound3 = Sound.load("Sound/background3.wav",true) end
voice:stop()
end
if score[Turn] == 300 then 
Difficulty[Turn] = 1000
score[Turn] = 500
printscore[Turn] = "500"
printnext[Turn] = "1,000"
end
if score[Turn] == 200 then
Difficulty[Turn] = 500
 score[Turn] = 300
printscore[Turn] = "300"
printnext[Turn] = "500"
end
if score[Turn] == 100 then 
Difficulty[Turn] = 300
score[Turn] = 200
printscore[Turn] = "200"
printnext[Turn] = "300"
end
if score[Turn] == 0 then 
Difficulty[Turn] = 200
score[Turn] = 100
printscore[Turn] = "100"
printnext[Turn] = "200"
end
dofile("./CorrectAnswer.lua")
end

--If answer is correct get a response for the answer

if FirstResponse == false then
dofile("./AnswerResponse.lua")
 AnswerNum = math.random(1,5) 
if score[Turn] > 500 then finalsound:play() end
FirstResponse = true
end
if timercheck <= 2 then blitString(screen,20,70,AnswerResponse[AnswerNum]) end
if PlayerGuess == string.lower(Millionaire[RandNum].answer) and timercheck > 2 and timercheck <= 4 then
if RanStopper == true  then 
math.randomseed(os.time())
AnswerNum = math.random(1,7)
RanStopper = false
 end
if score[Turn] < 1000000 then 
blitString(screen,20,70,CorrectAnswer[AnswerNum]) 
if cscheck == false then 
correctsound:play() 
cscheck = true
end
end
if score[Turn] == 1000000 then 
blitString(screen,20,70,"Yes!! You got it!!") 
if cscheck == false then 
correctsound:play() 
cscheck = true
end
end
end

-- If answer is wrong get a response and get final score 
if PlayerGuess ~= string.lower(Millionaire[RandNum].answer) and timercheck > 2 and  timercheck <= 4 then
dofile("./WrongAnswer.lua")
if RanStopper == true  then 
math.randomseed(os.time())
AnswerNum = math.random(1,5)
RanStopper = false
 end
if StartMenuSelection == 1 then blitString(screen,20,70,WrongAnswer[AnswerNum]) end
if StartMenuSelection == 2 then blitString(screen,20,70,"No, the answer was ".. Answer .. ".") end
if wscheck == false then
wrongsound:play()
wscheck = true
end
if GotDissed == "true" then
GotDissed = "false"
--EndScoreCalcs()
end
ChooseLife = ""
end

if PlayerGuess ~= string.lower(Millionaire[RandNum].answer) and timercheck > 4 and timercheck <= 6 then
if StartMenuSelection == 1 then blitString(screen,20,70,"The answer was ".. Answer .. ".") end
if StartMenuSelection == 2 then 
if Turn == 1 then blitString(screen,20,70,"Player 2's turn" )  end
if Turn == 2 then blitString(screen,20,70,"Player 1's turn" )  end
end
end

if StartMenuSelection == 2 and  PlayerGuess ~= string.lower(Millionaire[RandNum].answer) then
if timercheck == 7 then
EndScoreCalcs()
if score[Turn] == 100 then StartSelector[Turn].y = 249 end
if score[Turn] == 200 then StartSelector[Turn].y = 236 end
if score[Turn] == 300 then StartSelector[Turn].y = 223 end
if score[Turn] == 500 then StartSelector[Turn].y = 210 end
if score[Turn] == 1000 then StartSelector[Turn].y = 197 end
if score[Turn] == 2000 then StartSelector[Turn].y = 184 end
if score[Turn] == 4000 then StartSelector[Turn].y = 171 end
if score[Turn] == 8000 then StartSelector[Turn].y = 158 end
if score[Turn] == 16000 then StartSelector[Turn].y = 145 end
if score[Turn] == 32000 then StartSelector[Turn].y = 132 end
if score[Turn] == 64000 then StartSelector[Turn].y = 119 end
if score[Turn] == 125000 then StartSelector[Turn].y = 106 end
if score[Turn] == 250000 then StartSelector[Turn].y = 93 end
if score[Turn] == 500000 then StartSelector[Turn].y = 80 end
if score[Turn] == 1000000 then StartSelector[Turn].y = 67 end

if Turn == 1 then Turn = 2 else Turn = 1 end 
MultiVarSet()
end
end

if StartMenuSelection == 1 then
if PlayerGuess ~= string.lower(Millionaire[RandNum].answer) and timercheck == 7  then 
EndScoreCalcs()
GameOver = "true"
 end
--end
end

if score[Turn] == 0 then winamount = "0" end
if score[Turn] == 100 then winamount = "One hundred and 0/100" end
if score[Turn] == 200 then winamount = "Two hundred and 0/100" end
if score[Turn] == 300 then winamount = "Three hundred and 0/100" end
if score[Turn] == 500 then winamount = "Five hundred and 0/100" end
if score[Turn] == 1000 then winamount = "One thousand and 0/100" end
if score[Turn] == 2000 then winamount = "Two thousand and 0/100" end
if score[Turn] == 4000 then winamount = "Four thousand and 0/100" end
if score[Turn] == 8000 then winamount = "Eight thousand and 0/100" end
if score[Turn] == 16000 then winamount = "Sixteen thousand and 0/100" end
if score[Turn] == 32000 then winamount = "Thirty-Two thousand and 0/100" end
if score[Turn] == 64000 then winamount = "Sixty-four thousand and 0/100" end
if score[Turn] == 125000 then winamount = "One hundred twenty-five thousands and 0/100" end
if score[Turn] == 250000 then winamount = "Two hundred fifty thousand and 0/100" end
if score[Turn] == 500000 then winamount = "Five hundred thousand and 0/100" end
if score[Turn] == 1000000 then winamount = "One Million and 0/100" end

if PlayerGuess == string.lower(Millionaire[RandNum].answer) and timercheck > 4 and score[Turn] < 1000000 and timercheck <= 8 then
if StartMenuSelection == 1 then blitString(screen,20,70, "Going for $" .. printnext[Turn]) end
if StartMenuSelection == 2 then 
if Turn == 1 then blitString(screen,20,70, "Player 2's turn") end 
if Turn == 2 then blitString(screen,20,70, "Player 1's turn") end
end
end
if PlayerGuess == string.lower(Millionaire[RandNum].answer) and timercheck > 4 and score[Turn] == 1000000 and timercheck <= 8 then
blitString(screen,20,70, "Just Won A Million Dollars!!!")
end
-- if StartMenuSelection == 1 then
if score[Turn] == 1000000 and timercheck == 6 then
WonAMillion = true
end
-- end

blitString(screen,22,100,Millionaire[RandNum].questiona)
blitString(screen,22,110,Millionaire[RandNum].questionb)
blitString(screen,22,120,Millionaire[RandNum].questionc)
if PlayerGuess == string.lower(Millionaire[RandNum].a) then 
screen:blit(0,0,playeranswera) 
end
if PlayerGuess == string.lower(Millionaire[RandNum].b)
then screen:blit(0,0,playeranswerb)
 end
if PlayerGuess == string.lower(Millionaire[RandNum].c) then 
screen:blit(0,0,playeranswerc) 
end
if PlayerGuess == string.lower(Millionaire[RandNum].d) then 
screen:blit(0,0,playeranswerd) 
end

if timercheck > 2 and timercheck <= 6 then
if string.lower(Millionaire[RandNum].answer) == string.lower(Millionaire[RandNum].a) then 
screen:blit(0,0,rightanswera) 
end
if string.lower(Millionaire[RandNum].answer) == string.lower(Millionaire[RandNum].b) then 
screen:blit(0,0,rightanswerb) 
end
if string.lower(Millionaire[RandNum].answer) == string.lower(Millionaire[RandNum].c) then 
screen:blit(0,0,rightanswerc) 
end
if string.lower(Millionaire[RandNum].answer) == string.lower(Millionaire[RandNum].d) then 
screen:blit(0,0,rightanswerd) 
end
end
blitString(screen,13,173, Millionaire[RandNum].a) 
blitString(screen,13,213, Millionaire[RandNum].b) 
blitString(screen,198,173,Millionaire[RandNum].c) 
blitString(screen,198,213,Millionaire[RandNum].d) 
if StartMenuSelection == 1 then
if timercheck > 6 then  GetQuestion = "true"  end
end
-- end
if StartMenuSelection == 2 and  PlayerGuess == string.lower(Millionaire[RandNum].answer) then
if timercheck == 7 then
if score[Turn] == 100 then StartSelector[Turn].y = 249 end
if score[Turn] == 200 then StartSelector[Turn].y = 236 end
if score[Turn] == 300 then StartSelector[Turn].y = 223 end
if score[Turn] == 500 then StartSelector[Turn].y = 210 end
if score[Turn] == 1000 then StartSelector[Turn].y = 197 end
if score[Turn] == 2000 then StartSelector[Turn].y = 184 end
if score[Turn] == 4000 then StartSelector[Turn].y = 171 end
if score[Turn] == 8000 then StartSelector[Turn].y = 158 end
if score[Turn] == 16000 then StartSelector[Turn].y = 145 end
if score[Turn] == 32000 then StartSelector[Turn].y = 132 end
if score[Turn] == 64000 then StartSelector[Turn].y = 119 end
if score[Turn] == 125000 then StartSelector[Turn].y = 106 end
if score[Turn] == 250000 then StartSelector[Turn].y = 93 end
if score[Turn] == 500000 then StartSelector[Turn].y = 80 end
if score[Turn] == 1000000 then StartSelector[Turn].y = 67 end

if Turn == 1 then Turn = 2 else Turn = 1 end  
MultiVarSet()
ShowMultiStats = true
GetQuestion = "true"
end
end
end
-- Main Loop
while true do

buttoninput()

menuchecker = os.difftime(os.time(),menutimer)

if pad:cross() and restartcheck == false  and LifeMenuOn == false and PickLifeLine == false and oldpad:cross() ~= pad:cross() and PerformingLifeLine == false and PlayerGuess ~= ""  and CanGuess == "true" and WalkMenuOn == false and startscreen == false and ShowMultiStats == false then 
timer = os.time()
CanGuess = "false"
elseif pad:cross() and restartcheck == true and oldpad:cross() ~= pad:cross() then
timercheck = os.difftime(os.time(),timer)
if timercheck > 2 then restartcheck = false end
end


if GameOver == "false"   then
if VarsSet == "false" then
SetVariables()
VarsSet = "true"
end

menuchecker = os.difftime(os.time(),menutimer)

screen:clear() 

if not voice:playing()   and GameOver == "false"  and startscreen == false and wscheck == false  then
if score[Turn] < 1000 then
local sound = hbsound
voice = sound:play()
end
if score[Turn] >= 1000 and score[Turn] < 32000 then
local sound = hbsound2
voice = sound:play()
end
if score[Turn] >= 32000  then
local sound = hbsound3
voice = sound:play()
end
end

-- Draw Screen Images
if GameOver == "false" and startscreen == false  and ShowMultiStats == false  then DrawScreen()  end

if  startscreen == false and ShowMultiStats == true  then 
MultiStats()
end

if startscreen == true and ShowMultiStats == false then StartMenu() end

-- Get a Random Question if Needed
if GetQuestion == "true" then GetRandQuestion() end

-- Print the Question and Choices to the Screen
if GameOver == "false" and startscreen == false and RandNum ~= 0 and ShowMultiStats == false then PrintQuestion()  end

-- Draw LifeLine Menu
if PickLifeLine == true or WalkMenuOn == true or LifeMenuOn == true then DrawLifeLineMenu() end

--Do Lifeline stuff if needed
if PerformingLifeLine == true then
PerformLifeLine()
end



if StartMenuSelection == 2 then 
if pad:cross() and startscreen == false   and ShowMultiStats == true and oldpad:cross() ~= pad:cross() then
ShowMultiStats = false 
NextQuestionSounds()
end
end


--Start menu selections
if pad:up() and startscreen == true and oldpad:up() ~= pad:up()  then 
menutimer = os.time()
if  StartMenuSelection == 2 then StartMenuSelection = 1 end
if StartMenuSelection == 3 then StartMenuSelection = 2 end
if StartMenuSelection == 4 then StartMenuSelection = 3 end
end

if pad:down() and startscreen == true and oldpad:down() ~= pad:down() then
menutimer = os.time()
if StartMenuSelection == 3 then StartMenuSelection = 4 end
if StartMenuSelection == 2 then StartMenuSelection = 3 end
if StartMenuSelection == 1 then StartMenuSelection = 2 end
end

if pad:cross() and startscreen == true and oldpad:cross() ~= pad:cross() then 
-- Single Player
if StartMenuSelection == 1 then 
dofile("./Counts.lua") 
GetQuestion = "true"
menutimer = os.time()
startscreen = false
end
if StartMenuSelection == 2 then 
-- Multi Player
StartSelector = { }
StartSelector[1] = { x = 65, y = 262 }
StartSelector[2] = { x = 305, y = 262 }
PlayerLifeLines[1] = { audience = 2, phone = 2, fifty = 2 }
PlayerLifeLines[2] = { audience = 2, phone = 2, fifty = 2 }
MultiScreen = Image.load("Images/multistats.png")
MultiScores = Image.load("Images/multiscores.png")
ShowMultiStats = true
dofile("./Counts.lua") 
GetQuestion = "true"
menutimer = os.time()
startscreen = false
Timer:start()
end

if StartMenuSelection == 4 then 
-- Custom Game
dofile("./Custom/Counts.lua") 
dofile("./Custom/questionvars.lua") 
if Value1 > 0 and Value2 > 0 and Value3 > 0 and Value4 > 0 and Value5 > 0 and Value6 > 0 and Value7 > 0 and Value8 > 0 and Value9 > 0 and Value10 > 0 and Value11 > 0 and Value12 > 0 and Value13 > 0 and Value14 > 0 and Value15 > 0 then
GetQuestion = "true"
menutimer = os.time()
startscreen = false
end
end
if StartMenuSelection == 3 and ClearStatus == false then
-- Clear Questions
dofile("./Counts.lua") 
file = io.open("Counts.lua", "w") 

file:write("Counts100 = 0 \n")
file:write("Counts200 = 0 \n")
file:write("Counts300 = 0 \n")
file:write("Counts500 = 0 \n")
file:write("Counts1000 = 0 \n" )
file:write("Counts2000 = 0 \n")
file:write("Counts4000 = 0 \n")
file:write("Counts8000 = 0 \n" )
file:write("Counts16000 = 0 \n" )
file:write("Counts32000 = 0 \n" )
file:write("Counts64000 = 0 \n" )
file:write("Counts125000 = 0 \n" )
file:write("Counts250000 = 0 \n")
file:write("Counts500000 = 0 \n")
file:write("Counts1000000 = 0 \n")
file:write("TotalMillion  = " .. TotalMillion .. "\n")
file:close()
file = io.open("Qstats.lua","w")
file:write("")
file:close()
ClearStatus = true
fiftysound:play()
end
 end

if CanGuess == "true"  and GetQuestion == "false" and startscreen == false then

if pad:l() and LifeMenuOn ==  false  and PerformingLifeLine == false and ShowMultiStats == false then
LifeMenuOn = true
end

if pad:left() and LifeMenuOn == true and oldpad:left() ~= pad:left() then
LifeYes = true
LifeNo = false
end

if pad:left() and PickLifeLine == true and oldpad:left() ~= pad:left() then
if ChooseLife == "fifty"  then ChooseLife = "audience" end
if ChooseLife == "phone" then ChooseLife = "fifty" end
menutimer = os.time()
end

if pad:right() and PickLifeLine == true and oldpad:right() ~= pad:right() then
if ChooseLife == "fifty"  then ChooseLife = "phone" end
if ChooseLife == "audience" then ChooseLife = "fifty" end
menutimer = os.time()
end

if pad:right() and LifeMenuOn == true then
LifeNo = true
LifeYes = false
end

if pad:right() and WalkMenuOn == true then
WalkNo = true
WalkYes = false
end

if pad:left() and WalkMenuOn == true then
WalkNo =false
WalkYes = true
end

if pad:cross() and PickLifeLine == true and oldpad:cross() ~= pad:cross()  then
menutimer = os.time()
PickLifeLine = false
PerformingLifeLine = true
end


if pad:cross() and LifeMenuOn == true and oldpad:cross() ~= pad:cross() then
if LifeNo == true then 
menutimer = os.time()
LifeMenuOn = false  
end
if LifeYes == true then 
PickLifeLine = true
LifeMenuOn = false
menutimer = os.time()
end
end

if pad:cross() and WalkMenuOn == true and oldpad:cross() ~= pad:cross() then
if WalkNo == true then
menutimer = os.time()
WalkMenuOn = false
end
if WalkYes == true then
menutimer = os.time()
WalkMenuOn = false
GameOver = "true"
CanGuess = "false"
end
end

if pad:r() and LifeMenuOn ==  false  and PerformingLifeLine == false and StartMenuSelection == 1 then
WalkMenuOn = true
end

--Select Answers

if pad:right() and LifeMenuOn == false and PickLifeLine == false and  PerformingLifeLine == false and answerselect == "a"  and WalkMenuOn == false then 
answerselect = "c"
PlayerGuess = string.lower(Millionaire[RandNum].c)
end

if pad:right() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "b"  and WalkMenuOn == false then 
answerselect = "d"
PlayerGuess = string.lower(Millionaire[RandNum].d)
end

if pad:left() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "c"  and WalkMenuOn == false then 
answerselect = "a"
PlayerGuess = string.lower(Millionaire[RandNum].a)
end

if pad:left() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "d"  and WalkMenuOn == false then 
answerselect = "b"
PlayerGuess = string.lower(Millionaire[RandNum].b)
end

if pad:down() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "a"  and WalkMenuOn == false then 
answerselect = "b"
PlayerGuess = string.lower(Millionaire[RandNum].b)
end

if pad:down() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "c"  and WalkMenuOn == false then 
answerselect = "d"
PlayerGuess = string.lower(Millionaire[RandNum].d)
end

if pad:up() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "b"  and WalkMenuOn == false then 
answerselect = "a"
PlayerGuess = string.lower(Millionaire[RandNum].a)
end

if pad:up() and LifeMenuOn == false and PickLifeLine == false  and PerformingLifeLine == false and answerselect == "d" and WalkMenuOn == false  then 
answerselect = "c"
PlayerGuess = string.lower(Millionaire[RandNum].c)
end
end


if CanGuess == "false" then
timercheck = os.difftime(os.time(),timer)
if timercheck >6 then CanGuess = "true" end
AnswerCheck()
 end
end


--GameOver
if GameOver == "true" then
if gocheck == false then
voice:stop()
local sound = gosound
voice = sound:play()
gocheck = true
end
screen:blit(0,0,solidback)
if checkx < 5 then checkx = checkx +5 end
screen:blit(checkx,20,check,true)
if StartMenuSelection == 1 then
if TotalMillion == 1 then blitString(screen,140,35,"Won a Million " .. TotalMillion .. " time. ") end
if TotalMillion ~= 1 then blitString(screen,140,35,"Won a Million " .. TotalMillion .. " times. ") end
end
if StartMenuSelection == 2 then
blitString(screen,140,35,"Player " .. Turn .. " won a million!!!")
end
screen:print(checkx+360,133,printscore[Turn],black)
screen:print(checkx+60,155,winamount,black)
if pad:cross() and checkx == 5 then
voice:stop()
WonAMillion = false
VarsSet = "false"
GameOver = "false"
restartcheck = true
menutimer = os.time()
if StartMenuSelection == 2 then 
StartSelector = { }
StartSelector[1] = { x = 65, y = 262 }
StartSelector[2] = { x = 305, y = 262 }
PlayerLifeLines[1] = { audience = 2, phone = 2, fifty = 2 }
PlayerLifeLines[2] = { audience = 2, phone = 2, fifty = 2 }
MultiVarSet() 
StartMenuSelection = 1
ShowMultiStats = false
Turn = 1
end
startscreen = true
voice = sound:play()
end
end

-- Won A Million
if WonAMillion == true then
if timercheck > 2 then
GameOver = "true"
end
end
oldpad = pad
screen.waitVblankStart()
screen.flip()
end