map4 = nil
shop1res = nil
collectgarbage()

shop1res,message = loadfile("shop3.lua")
if shop1res == nil then
screen:clear()
screen:print(0,0,"LOAD FAILED",Color.new(255,0,0))
screen:print(0,10,message,Color.new(255,255,255))
screen.flip()
while true do
screen.waitVblankStart()
end
end

bombcounter = 0
   
   backgroundCache = Image.load("backgrounds/shopmenu.png")
   cursor = Image.load("backgrounds/spcursor.png") ------load cursor
   beep = Sound.load("music/beep.wav", false)
   
   selected = 1
   maxselected = 6 
   oldpad = Controls.read() 

while true do

   if selected > maxselected then
   selected = 1
   elseif selected <= 0 then
   selected = maxselected
   end

   padmenu = Controls.read()

   screen.waitVblankStart(6)

   screen:blit(0, 0, backgroundCache, false)

   if padmenu:up() and oldpad:up() ~= padmenu:up() then
   selected = selected - 1
   beep:play()
   end

   if padmenu:down() and oldpad:down() ~= padmenu:down() then
   selected = selected + 1
   beep:play()
   end


   if selected == 1 then
    screen:blit(70, 15, cursor)
end

   if selected == 2 then
   screen:blit(92, 15, cursor)
end


   if selected == 3 then
   screen:blit(113, 15, cursor)
end


   if selected == 4 then
   screen:blit(135, 15, cursor)
end


   if selected == 5 then
   screen:blit(70, 32, cursor)
end

   if selected == 6 then
   screen:blit(90, 32, cursor)
end


if padmenu:square() and oldpad:square() ~= pad:square() and selected == 1 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "You don't have enough Rupees....", black)
	screen:print(100, 240, "This item costs 8 Rupees", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

	
	if padmenu:square() and oldpad:square() ~= pad:square() and selected == 1 and
	RupeeCounter == 8 then
	bombcounter = bombcounter + 1
	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	tresure:play()
	screen:print(100, 220, "You have bought a bomb", black)
	screen:print(100, 240, "press square to use it", black)
  	screen.flip()
	screen.waitVblankStart(250)
	player.y = oldy
	player.x = oldx
	shop1res()	
	end

if padmenu:square() and oldpad:square() ~= pad:square() and selected == 2 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "This item is currently", black)
	screen:print(100, 240, "out of stock", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

if padmenu:square() and oldpad:square() ~= pad:square() and selected == 3 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "You don't have enough Rupees....", black)
	screen:print(100, 240, "This item costs 150 Rupees", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

if padmenu:square() and oldpad:square() ~= pad:square() and selected == 4 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "You don't have enough Rupees....", black)
	screen:print(100, 240, "This item costs 80 Rupees", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

	if padmenu:square() and oldpad:square() ~= pad:square() and selected == 5 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "This item is also out", black)
	screen:print(100, 240, "of stock", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

if padmenu:square() and oldpad:square() ~= pad:square() and selected == 6 and
RupeeCounter == 0 then
screen:blit(0, 199, box1) 
screen:blit(390,199, linkenp)
	screen:blit(0,199, shopp)
	screen:print(100, 220, "You need a bigger heart container", black)
	screen:print(100, 240, "before you can buy this", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	player.y = oldy
	player.x = oldx
	shop1res()
	end

if padmenu:start() then 
player.y = oldy
player.x = oldx
shop1res()
end

screen.flip()
oldpad = padmenu
end