shop3load = nil
loadmap1 = nil
map9 = nil
map10 = nil
map11 = nil
map9 = nil
collectgarbage()

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

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

map11,message = loadfile("map2.lua")
if map11 == 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/town3.png")

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

	wall1 = Image.createEmpty(1,80)   -----left hand roof, top
	wall2 = Image.createEmpty(1,80)   -----right hand roof, top
	wall3 = Image.createEmpty(1,10)   -----left hand house wall vertical part1
	wall4 = Image.createEmpty(1,5)    -----left  hand house wall vertical part2
	wall5 = Image.createEmpty(1,10)   -----right hand house wall vertical part1
	wall6 = Image.createEmpty(1,5)    -----right hand house wall vertical part2
	wall7 = Image.createEmpty(40,1)   -----Left hand (before door) house wall horizontal part1
	wall8 = Image.createEmpty(6,15)    -----Left hand (after door) house wall horizontal part2
	wall10 = Image.createEmpty(40,1)  -----Right hand (before door) house wall horizontal part1
	wall11 = Image.createEmpty(6,1)   -----Right hand (after door) house wall horizontal part2
   	wall13 = Image.createEmpty(1,65)  -----Left Wall vertical
	wall14 = Image.createEmpty(1,65)  -----Right Wall vertical
	wall15 = Image.createEmpty(140,1) -----Left Side Horizontal Wall
	wall16 = Image.createEmpty(140,1) -----Right Side Horizontal Wall
	wall17 = Image.createEmpty(1,35)  -----Roof Left side next to wall - vertical
   	wall18 = Image.createEmpty(1,35)  -----Roof Right side next to wall - vertical
	wall19 = Image.createEmpty(3,10)  -----Roof Right side part1
	wall20 = Image.createEmpty(1,1)   -----Roof Right side part2
	wall21 = Image.createEmpty(1,1)   -----Roof Right side part3
	wall22 = Image.createEmpty(1,10)  -----Roof Right Side Pilar
	wall23 = Image.createEmpty(40,1)  -----Roof Middle Horizontal
	wall24 = Image.createEmpty(1,10)  -----Roof Left Side Pilar
	wall25 = Image.createEmpty(3,10)  -----Right side part1
	wall26 = Image.createEmpty(1,1)   -----Right side part2
	wall27 = Image.createEmpty(1,1)   -----Right side part3
	wall28 = Image.createEmpty(1,1)   -----Right Side Part4


------------Walls------

   	wall1 = {x = 151, y = 0, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 333, y = 0, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 130, y = 94, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 125, y = 117, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 355, y = 94, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 363, y = 117, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 83, y = 124, height = wall7:height(), width = wall7:width() }
   	wall8 = {x = 33, y = 110, height = wall8:height(), width = wall8:width() }
   	wall10 = {x = 362, y = 124, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 445, y = 124, height = wall11:height(), width = wall11:width() }
   	wall13 = {x = 18, y = 134, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 469, y = 134, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 20, y = 210, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 317, y = 210, height = wall16:height(), width = wall16:width() }
   	wall17 = {x = 174, y = 176, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 314, y = 176, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 287, y = 156, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 297, y = 169, height = wall20:height(), width = wall20:width() }
   	wall21 = {x = 278, y = 152, height = wall21:height(), width = wall21:width() }
   	wall22 = {x = 270, y = 136, height = wall22:height(), width = wall22:width() }
   	wall23 = {x = 216, y = 148, height = wall23:height(), width = wall23:width() }
   	wall24 = {x = 213, y = 134, height = wall24:height(), width = wall24:width() }
   	wall25 = {x = 192, y = 157, height = wall25:height(), width = wall25:width() }
   	wall26 = {x = 180, y = 170, height = wall26:height(), width = wall26:width() }
   	wall27 = {x = 171, y = 177, height = wall27:height(), width = wall27:width() }
   	wall28 = {x = 202, y = 148, height = wall28:height(), width = wall28: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 >=38 and player.x <=71 and
	player.y >=110 and player.y <=115 then
	player.x = 234                
	player.y = 250
	player.img = player7
	map9()
	end
	
	if player.x >=390 and player.x <=438 and
	player.y >=110 and player.y <=115 then
	player.x = 80                
	player.y = 252
	player.img = player7
	map10()
	end 
	  	
	if player.x >=136 and player.x <=345 and
	player.y >=1 and player.y <=3 then
	player.x = 242
	player.y = 262
	player.img = player7
	map11()
	end
end

oldpad = Controls.read()

----------start main loop--------

	while true do

    maps()
	loose()

-------**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(wall10)
	collisionCheck(wall11)
	collisionCheck(wall13)
	collisionCheck(wall14)
	collisionCheck(wall15)
	collisionCheck(wall16)
	collisionCheck(wall17)
	collisionCheck(wall18)
	collisionCheck(wall19)
	collisionCheck(wall20)
	collisionCheck(wall21)
	collisionCheck(wall22)
	collisionCheck(wall23)
	collisionCheck(wall24)
	collisionCheck(wall25)
	collisionCheck(wall26)
	collisionCheck(wall27)
	collisionCheck(wall28)
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	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()

 	screen.waitVblankStart()  
	oldpad = pad
end

