map9 = nil
map10 = nil
map11 = nil
map12 = nil
loadmap1 = nil
secret = nil
resumemap = nil
collectgarbage()

map12,message = loadfile("map2.lua")
if map12 == 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()
end

secret,message = loadfile("secret.lua")
if secret == 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()
end

	status = "game"

--------load map-------

	backgroundCache = Image.load("backgrounds/town4.png")

-----------**Walls**---------

	wall1 = Image.createEmpty(200,1)  -----Right Hand house horizontal 
	wall2 = Image.createEmpty(1,1)    -----Right hand house vetical
	wall3 = Image.createEmpty(98,1)   -----Middle Wall top
	wall4 = Image.createEmpty(1,2)    -----left house roof/wall vertical
	wall5 = Image.createEmpty(1,2)    -----left house wall vertical
	wall6 = Image.createEmpty(60,1)   -----left house wall, horizontal
	wall7 = Image.createEmpty(1,300)  -----Left Wall - vertical
	wall8 = Image.createEmpty(250,1)  -----Bottom Wall - Horizontal
	wall9 = Image.createEmpty(1,10)   -----Roof bottom Side Pilar
	wall10 = Image.createEmpty(20,1)  -----Roof bottom block1
	wall11 = Image.createEmpty(1,1)   -----Roof bottom block2
   	wall12 = Image.createEmpty(1,1)   -----Roof bottom block3
	wall13 = Image.createEmpty(250,1) -----Roof Wall bottom
             
------------Walls------

   	wall1 = {x = 285, y = 84, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 284, y = 82, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 163, y = 70, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 140, y = 76, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 127, y = 96, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 42, y = 96, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 39, y = 97, height = wall7:height(), width = wall7:width() }
   	wall8 = {x = 45, y = 254, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 292, y = 216, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 268, y = 247, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 281, y = 233, height = wall11:height(), width = wall11:width() }
	wall12 = {x = 257, y = 257, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 299, y = 229, height = wall13:height(), width = wall13:width() }
   	  
-----------------**Collision Check**---------

function collisionCheck(test)

	if (player.x + playerWidth > test.x) and (player.x < test.x + test.width) and
	(player.y + playerHeight > test.y) and (player.y < test.y + test.height) then

	player.x = oldx
	player.y = oldy
	end
end

--------------**map loading**------

function maps()   ------House

	if player.x >=58 and player.x <=93 and
	player.y >=0 and player.y <=112 and secretroom == true and bombcounter == 1 and
	pad:cross() then
	secretroom = false
	screen:blit(68,105, bomb)
	screen.flip()
	screen.waitVblankStart(50)
	explode:play()
    player.x = 240
	player.y = 250
	player.img = player7
	screen.waitVblankStart(100)
    secret()
	end 

	if player.x >=468 and player.x <=480 and
	player.y >=0 and player.y <=272 then
	player.x = 25
	player.y = oldy
	player.img = player1
    map12()
	end
end
     
----------player text---------

     	function playertext()
       
---------character conversation-----

	if player.x >=36 and player.x <=47 and
	player.y >=145 and player.y <=203 and
	pad:cross() then
	select:play()
 	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:print(100, 220, "Sorry you may not pass", black)
	screen.flip()
	screen.waitVblankStart(250)
   	end
end

oldpad = Controls.read()
     
----------start main loop--------

	while true do

    maps()

-------**store players position at beginning of each loop**----

	oldx = player.x
	oldy = player.y
	screen:clear()

-------init controls---------

	pad = Controls.read()
	
	attackingr()
	attackingl()
	attackingu()
	attackingd()

---------init functions-----

	if pad:right() then    -----Right Movement
	moveRight()
	player.x = player.x + 2
 	end

	if pad:left() then    -----Left Movement
	moveLeft()
	player.x = player.x - 2
  	end

	if pad:up() then      -----Up Movement
	moveUp()                                                   
 	player.y = player.y - 2
	end

	if pad:down() then   -----Down Movement
	moveDown()
 	player.y = player.y + 2
	end

------**check collision for wall**-----

	collisionCheck(wall1)
	collisionCheck(wall2)
	collisionCheck(wall3)
	collisionCheck(wall4)
	collisionCheck(wall5)
	collisionCheck(wall6)
	collisionCheck(wall7)
	collisionCheck(wall8)
	collisionCheck(wall9)
	collisionCheck(wall10)
	collisionCheck(wall11)
	collisionCheck(wall12)
	collisionCheck(wall13)

	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(25, 146, hylian1)
	screen:blit(25, 177, hylian2)
	screen:blit(400, 10, container)
 	screen:blit(417, 13, triangle)
 	screen:blit(432, 30, circle)
   	screen:blit(403, 30, square)
   	screen:blit(417, 45, cross)
 	screen:blit(404, 32, sword)
 	screen:blit(420, 15, bomb)
 	screen:blit(420, 49, maja)
	screen:blit(0, 0, heart1)
	screen:blit(30, 0, heart2)
	screen:blit(60, 0, heart3)
 	screen:blit(240, 0, rupeess) 
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()
   	
   	playertext()

 	screen.waitVblankStart()  
	oldpad = pad
end


