figure = Image.load("data/tails.png")
tiles = Image.load("data/stagefour.png")
  level = 4
         mapSource = {
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "mnnnnnnnnnnnnnnbnnnnnnnnnbnnnm",
  "mnnnnnnnnnnnnnnbnonnnnnnnbnonm",
  "mnnnbnnnnnbnnnnbnnnnnnnnnbnnnm",
  "mnnnbnnnnnbnnnnccccccnnnnbcnnm",
  "mnpnbnnnnnbnnnnnnnnnnnnnnnnnnm",
  "mcccbnnnnnbnnnnnnnnnnnnnnnnnnm",
  "mnnnnnnonnbnnnnnnnnnnnnnnnnnnm",
  "mnnnnnnnnnbnnnnnnnnnnnnnnnnnnm",
  "mcccccccccccccccccccccnnnbnnnm",
  "mnonbnnnnnbnnnnnnnnnnbnnnbnnnm",
  "mnnnbnnnnnnnnnnnnbnonbnnnbnnnm",
  "mnnnnnnnbccccbnnnccccbnnnbnnnm",
  "mnnnbnnnbnnnnnnnnnnnnnnnnbnnnm",
  "mnnnbnnnbnonnnnnnbnnnnnnnbnnnm",
  "mnonbnnnbnnnnbnnnbnnnnnnnbnonm",
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
}
board = Image.createEmpty(480, 272)

function drawTile(tile, x, y)
  tile = string.byte(tile, 1) - string.byte("a")
  local tileX = math.mod(tile, 4)
  local tileY = math.floor(tile / 4)
  board:blit(16 * x, 16 * y, tiles, 17 * tileX + 1, 17 * tileY + 1, 16, 16, false)
end

map = {}
for y = 1, 17 do
  line = mapSource[y]
  map[y] = {}
  for x = 1, 30 do
    tile = string.sub(line, x, x)
    map[y][x] = tile
    drawTile(tile, x - 1, y - 1)
  end
end

playerX = 6
playerY = 14

animation = 0

animationDirection = 1

animationSlowDown = 0

speedStep = 0

dx = 0
dy = 0

score = 0


white = Color.new(255, 255, 255)

while true do
  pad = Controls.read()

  if pad:start() then
    break
  end
  
  if pad:select() then
  screen:save("screenie1.png")
  end

  if speedStep == 0 then
    oldPlayerX = playerX
    oldPlayerY = playerY

    playerX = playerX + dx
    playerY = playerY + dy

    newTile = map[playerY][playerX]

    if newTile ~= "n" and newTile ~= "p" and newTile ~= "o" then
      playerX = oldPlayerX
      playerY = oldPlayerY
    end

    if newTile == "p" then score = score + 5 end
    if newTile == "o" then score = score + 1 end
    if newTile == "p" or newTile == "o" then

      map[playerY][playerX] = "n"


      drawTile("n", playerX - 1, playerY - 1)
    end


    dx = 0
    dy = 0
  else

    if pad:up() then
      dy = -1
    elseif pad:down() then
      dy = 1
    elseif pad:left() then
      dx = -1
    elseif pad:right() then
      dx = 1
    end
  end


  speedStep = speedStep + 1
  if speedStep == 5 then
    speedStep = -1
  end


  screen:blit(0, 0, board, 0, 0, board:width(), board:height(), false)




  screen:blit((playerX - 1) * 16 - 4, (playerY - 1) * 16 - 4, figure, 1 + 25 * animation, 1, 24, 24)


  animationSlowDown = animationSlowDown + 1
  if animationSlowDown == 3 then
    animationSlowDown = 0
    animation = animation + animationDirection
    if animation == 4 then
      animationDirection = -1
    elseif animation == 0 then
      animationDirection = 1
    end
  end

  screen:print(10, 260, "Score: " .. score, white)

  screen.waitVblankStart()
  screen:flip()


       if score == 13 then
     screen:clear()
     screen:print(10,130,"Press X",red)
     screen.flip()

           if pad:cross() then
      screen:clear()
 
          level = 5

tiles = Image.load("data/stagefive.png")
mapSource = {
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "mnnnnnbnnonnnbobonbnonnnnnnnnm",
  "mnnpnnbncccncbnbnnbnnnnnnnnnnm",
  "mnnnnnbnnonnnbnbnnbnnbcccccnnm",
  "mnnnnnbncccncbnbnnbnnbnnnnnnnm",
  "mnnnnnbnnnnnnbnbnnbnnbnnnnnnom",
  "mnnnnnbnnnnnnnnnnnnnnbnncccccm",
  "mnnnnnnnnnnnnbnbnnbnnbnnnnnnnm",
  "mcccccccccccccccccccccccccccnm",
  "monnnnbnonbonnnnnbnnnbnnbobnnm",
  "mcccnnbnnnbnnnnnnbnnnnnnbnbnnm",
  "mnnnnnnnnnbnnbnnnbnnnnnnbnbnnm",
  "mnnnnnnnnnbnnbncccnncbnnbnbnnm",
  "mnncccbnnnbnnbnnnnnnnbnnbnbnnm",
  "mnnnnnbnnnnnnbnnnnnnnbnnnnnnnm",
  "mnnnnobnnnnnnbnnnbnnnbnnnnnnnm",
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
}

board = Image.createEmpty(480, 272)

function drawTile(tile, x, y)
  tile = string.byte(tile, 1) - string.byte("a")
  local tileX = math.mod(tile, 4)
  local tileY = math.floor(tile / 4)
  board:blit(16 * x, 16 * y, tiles, 17 * tileX + 1, 17 * tileY + 1, 16, 16, false)
end

map = {}
for y = 1, 17 do
  line = mapSource[y]
  map[y] = {}
  for x = 1, 30 do
    tile = string.sub(line, x, x)
    map[y][x] = tile
    drawTile(tile, x - 1, y - 1)
  end
end

playerX = 6
playerY = 14

animation = 0

animationDirection = 1

animationSlowDown = 0

speedStep = 0

dx = 0
dy = 0

score = 0

white = Color.new(255, 255, 255)

while true do
  pad = Controls.read()

  if pad:start() then
    break
  end
  if speedStep == 0 then
    oldPlayerX = playerX
    oldPlayerY = playerY

    playerX = playerX + dx
    playerY = playerY + dy

    newTile = map[playerY][playerX]

    if newTile ~= "n" and newTile ~= "p" and newTile ~= "o" then
      playerX = oldPlayerX
      playerY = oldPlayerY
    end

    if newTile == "p" then score = score + 5 end
    if newTile == "o" then score = score + 1 end
    if newTile == "p" or newTile == "o" then
      map[playerY][playerX] = "n"

      drawTile("n", playerX - 1, playerY - 1)
    end

    dx = 0
    dy = 0
  else
    if pad:up() then
      dy = -1
    elseif pad:down() then
      dy = 1
    elseif pad:left() then
      dx = -1
    elseif pad:right() then
      dx = 1
    end
  end

  speedStep = speedStep + 1
  if speedStep == 5 then
    speedStep = -1
  end

  screen:blit(0, 0, board, 0, 0, board:width(), board:height(), false)


 
 screen:blit((playerX - 1) * 16 - 4, (playerY - 1) * 16 - 4, figure, 1 + 25 * animation, 1, 24, 24)

  animationSlowDown = animationSlowDown + 1
  if animationSlowDown == 3 then
    animationSlowDown = 0
    animation = animation + animationDirection
    if animation == 4 then
      animationDirection = -1
    elseif animation == 0 then
      animationDirection = 1
    end
  end
  screen:print(10, 260, "Score: " .. score, white)

  screen.waitVblankStart()
  screen:flip()

  if score == 16 then
  screen:clear()
  screen:print(10,130,"Press Start To Finish",red)
  screen.flip()

end
 end
  end
   end
    end
     
      
        
            while not Controls.read():start() do end
