-- Game 1 : Higher or lower
-- 480 x 272

myfont = Font.createProportional()
myfont:setPixelSizes(0,16)
deckimages = loaddeckimages("big",mydeck,myback)
Music.playFile("music/entertnr.xm",true)

oldpad=nil
-- DOBET

function dobet()
-- bet is 1-5
bet=0
bet_done=0
repeat
	clear_screen()
	render()
	screen:fillRect(160,100,120,72,Color.new(0,60,0))
	screen:fillRect(164,104,112,64,Color.new(0,160,0))
	screen:fontPrint(myfont,170,126,"Bet:"..bet,Color.new(140,0,0))
	screen:fontPrint (myfont,160,40 ," To select ",Color.new(255, 0, 0))
	screen:fontPrint (myfont,160,60 ," To Start ",Color.new(255, 0, 0))
	screen:blit(130,26,pad_up)
	screen:blit(146,26,pad_down)
	screen:blit(136,46,cross)
	
	screen.flip()
	screen.waitVblankStart()
	key_push=0
	
	
repeat		
	pad = Controls.read()
	bet_done=0
if not (pad == oldpad) then
	if pad:up() then
		voice = click:play()
	 	bet=bet+1
		key_push=1
		
	elseif pad:down() then
	    voice = click:play() 
		bet=bet-1
		key_push=1
		
	elseif pad:cross() then
	    voice = click:play() 
		
		bet_done=1
		key_push=1
	end
	if bet<0 then bet=0 end
	if bet>5 then bet=5 end
	if bet>credits then bet=credits end
	
	oldpad=pad
	
end
until (key_push==1)
	
until ((bet_done==1) and (bet>0))
--
credits = credits -bet
return bet

end
-- RENDER
function render()

decksize = 8 - math.floor (mycard/6.5)

drawdeck(90,90,decksize,deckimages,canvas)

--x2,y2 = drawdeck(190,90,8 - decksize,deckimages)
x2,y2 = 190,90
if not (mycard ==0) then
for i=1,mycard do
x2 = x2 + 15

drawcard(deck[i],x2,y2,deckimages) end
end

-- if not (mycard ==0) then drawcard_b(deck[mycard],x2,y2,mydeck) end

--g,h = numtocard(deck[mycard])
--screen:print(0,30,h,Color.new(0,0,0))

--if (h>i) then screen:fontPrint (myfont,100,200 ,"Higher",Color.new(255, 0, 0)) end
--if (h<i) then screen:fontPrint (myfont,100,200 ,"Lower",Color.new(255, 0, 0)) end
--if (h==i) then screen:fontPrint (myfont,100,200 ,"Equal",Color.new(255, 0, 0)) end
screen:fillRect(0,245,480,27,Color.new(0,0,0))
screen:fontPrint (myfont,1,260 ,"Credits:"..credits,Color.new(150, 255, 50)) 
screen:fontPrint (myfont,160,260 ,"Bet:"..bet,Color.new(150, 255, 50)) 

end

-- WIN
function win()
addbet = math.floor(bet *0.5)
if bet==1 then addbet=2 end

clear_screen()
render()

screen:fontPrint (myfont,120,40 ,"Won "..bet.."! Keep playing for "..bet+addbet.."?",Color.new(0, 0, 255)) 

screen:fontPrint (myfont,140,60 ," Exit game      Take Cash",Color.new(255, 0, 0))

screen:fontPrint (myfont,180,78 ,"   Keep playing   ",Color.new(255, 0, 0))
screen:blit(126,46,square)
screen:blit(236,46,circle)
screen:blit(178,64,cross)
screen.flip()
screen.waitVblankStart()
answer=0
repeat
pad = Controls.read()
if not (pad == oldpad)	then
	if pad:cross() then answer=1 end
	if pad:circle() then answer=2 end
	if pad:square() then answer=3 end
	oldpad=pad
end

until not(answer==0)
if (answer==1) then
--continue
	bet=bet+ addbet

end
if (answer==2) then
--restart
	mycard=0
	credits=credits+bet
	bet=0

end
if (answer==3) then
--quit
	credits=credits+bet
	bet=0
	quit=1
end
end

-- LOSE
function lose()
clear_screen()
render()

if (credits>0) then
screen:fontPrint (myfont,100,40 ,"Lose... Keep playing ?",Color.new(0, 0, 255)) 
screen:fontPrint (myfont,140,60 ," Exit game  ",Color.new(255, 0, 0))

screen:fontPrint (myfont,180,78 ,"   Retry   ",Color.new(255, 0, 0))
screen:blit(126,46,square)
screen:blit(178,64,cross)
screen.flip()
screen.waitVblankStart()
answer=0
repeat
pad = Controls.read()
	if not (pad == oldpad)	then
	if pad:cross() then answer=2 end
	if pad:square() then answer=3 end
	oldpad=pad
end
until not(answer==0)

if (answer==2) then
--restart
	mycard=0
	bet=0

end
if (answer==3) then
--quit
	quit=1
end
else
-- Lost, 0 credits!
answer=0
screen:fontPrint (myfont,60,40 ,"YOU LOST ALL YOUR MONEY!",Color.new(255, 0, 0))
screen.flip()
screen.waitVblankStart()
repeat
pad = Controls.read()
	if pad:cross() then answer=2 end
	oldpad=pad

until not(answer==0)

quit=1
end
end
-- MAIN

quit=0
clear_screen()
deck = flushdeck()
mycard = 0
refresh=1
bet=0
-- Main loop
while true do
	
	if (refresh==1) then
	clear_screen()
	render()
	screen:fontPrint (myfont,160,40 ," Higher ",Color.new(255, 0, 0))
	screen:fontPrint (myfont,160,60 ," Lower ",Color.new(255, 0, 0))
	screen:blit(146,26,pad_up)
	screen:blit(146,46,pad_down)
	screen.flip()
	refresh=0
	end
	
	while (bet==0) do
		bet=dobet()
		deck = flushdeck()
		mycard=1
		refresh=1
		
	end




-- pad loop
	repeat
	pad = Controls.read()
		if not (pad == oldpad) then
			if pad:up() then
				mycard=mycard+1
				g,h = numtocard(deck[mycard])
				g,i = numtocard(deck[mycard-1])
				voice = click:play()
				if (h>i) then 
					
					win(i,h)
				else
				
					lose(i,h)
				end
				refresh=1
		
			elseif pad:down() then
				mycard=mycard+1
				g,h = numtocard(deck[mycard])
				g,i = numtocard(deck[mycard-1])
				voice = click:play()
				
				if (h<i) then 
					
					win(i,h)
				else
					lose(i,h)
				end
				refresh=1
			end

			if pad:start() then
			
				quit=1
				break
			end
		end
		oldpad=pad
	until refresh==1





screen.waitVblankStart()
if quit==1 then 
Music.stop()
break 
end
end

