red = Color.new(255,0,0)
green = Color.new(0,255,0)
blue = Color.new(0,0,255)
Info = Image.load("data/info1.png")
zero = Image.load("data/zero.png")
one = Image.load("data/one.png")
two = Image.load("data/two.png")
three = Image.load("data/three.png")
four = Image.load("data/four.png")
five = Image.load("data/five.png")
background = Image.load("data/background.png")
infotwo = Image.load("data/info2.png")
loading = Image.load("data/loading.png")
ready = Image.load("data/ready.png")


screen:clear()
screen:blit(0,0, zero)
screen:print (150, 200,"Loading... 0%", red)
screen.flip()
screen.waitVblankStart(60)

screen:clear()
screen:blit(0,0, one)
screen:print (150, 200,"Loading... 23%", red)
screen.flip()
screen.waitVblankStart(60)

screen:clear()
screen:blit(0,0, two)
screen:print (150, 200,"Loading... 53%", red)
screen.flip()
screen.waitVblankStart(60)

screen:clear()
screen:blit(0,0, three)
screen:print (150, 200,"Loading... 78%", red)
screen.flip()
screen.waitVblankStart(60)

screen:clear()
screen:blit(0,0, four)
screen:print (150, 200,"Loading... 91%", red)
screen.flip()
screen.waitVblankStart(60)

screen:clear()
screen:blit(0,0, five)
screen:print (150, 200,"Loading... 100%!!!", red)
screen.flip()
screen.waitVblankStart(60)

bgmusic = Music.playFile("data/bgmusic.xm", true)

option = 1
while true do
screen:clear()
screen:blit(0,0, background)
            input = Controls.read()
            if input:select() then
            screen:save("screenie.png")
            end
      if input:up() then
         if option ~= 1 then
            option = option -1
            screen.waitVblankStart(10)
         end
      elseif input:down() then
         if option ~= 5 then
            option = option + 1
            screen.waitVblankStart(10)
         end
  end

      screen:print(240,90,"Start lvs. 1-3",blue)
      screen:print(240,110,"Start lvs. 4-5",blue)
      screen:print(240,130,"Info",blue)
      screen:print(240,150,"Tails Chao mode",blue)
      screen:print(240,170,"Exit",blue)

      if option == 1 then
         screen:print(240,90,"Start lvs. 1-3",red)
      elseif option == 2 then
         screen:print(240,110,"Start lvs. 4-5",red)
      elseif option == 3 then
         screen:print(240,130,"Info",red)
      elseif option == 4 then
         screen:print(240,150,"Tails Chao mode",red)
      elseif option == 5 then
         screen:print(240,170,"Exit",red)
      end

      if input:cross() then
         if option == 1 then
            screen:clear()
            screen:blit(0,0, loading)
            screen.flip()
            screen.waitVblankStart(60)

            figure = Image.load("data/player.png")
            tiles = Image.load("data/mapimgs.png")

            screen:clear()
            screen:blit(0,0, ready)
            screen.flip()
            screen.waitVblankStart(60)

        level = 1
            mapSource = {
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "mmeaaaaaaaaafaaaaaaaaaaaaaafmm",
  "mmdpoooooooonooooooooooooopbmm",
  "mmdokccccclnnkccccccccccclobmm",
  "mmdojaaaaadnnbmmmmmmmmmmmdobmm",
  "mmdooooooodnnbmmmmmmmmmmmdobmm",
  "mmdokccclodnnbmmmmmmmmmmmdobmm",
  "mmdobmmmdodnnbmmmmmmmmmmmdobmm",
  "mmdobmmmdodnnjaaaaaaaaaafdobmm",
  "mmdobmmmdpdnnooooooooooobdobmm",
  "mmdobmmmhcgnhccccccccclobdobmm",
  "mmdojaaaaainjaaaaaaaaaiojiobmm",
  "mmdpoooooooooooooooooooooopbmm",
  "mmhccccccccccccccccccccccccgmm",
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "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 == 108 then
     screen:clear()
     screen:print(10,130,"Press X",red)
     screen.flip()

      if pad:cross() then
      screen:clear()

             level = 2
      tiles = Image.load("data/stagetwo.png")
          mapSource = {
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "meaaaaaaaaaaaaaaaaaaaafaaaaafm",
  "mdnnnnbnnnnnnnnnnnnonndnnnpnbm",
  "mdnonnbnnnnnbnnnnnnnnndnnnnnbm",
  "mdnnnnnnnbnnbnnccccccchccccnnm",
  "mecccccccgnnnnnnnnnnnndnbnnnbm",
  "mdnnnnnnnnnnnnnnnnnnnndnbnnnbm",
  "mdnnnnnnnnnnnnnnnnbnnndccnnnbm",
  "mdnnecccccccccccnnbnnndnnnnnbm",
  "mdnnbnnnnnbnnnnbnnbnnndnnnnnbm",
  "mdnnbnnonnbnnonbnnbnnndnnnnnbm",
  "mdnnbnnnnnbnnnnbnnbnnndnnnccbm",
  "mdnnbnnccccnnnnbnnbnnndnnnnnbm",
  "mdnnbnnnnnnnnnnnnnbnnnnnnnnnbm",
  "mdnnbnnnnnnnnnnnnnbnnnnnnnnnbm",
  "mhccgcccccccccccccgcccccccccgm",
  "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 == 9 then
     screen:clear()
     screen:print(10,130,"Press X",red)
     screen.flip()

           if pad:cross() then
      screen:clear()

             level = 3
          tiles = Image.load("data/stagethree.png")    
mapSource = {
  "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
  "monnnnnnnnnnnbnnnnnbnnnnnnnnom",
  "mnnnnnnnnnnnnbnnnnnbnnnnnnnnnm",
  "mnnnnnccccbnnnnnnnnnnncccccccm",
  "mnnbnnnnnnbnnnnnnnnnnnnnnnnnnm",
  "mnnbccnnccbnnbnncccbcccccccnnm",
  "mnnbnnnnnnbnnbnnnnnbnnnnnnnnnm",
  "monbnnnnnobnnbnnnnnbnnnnnnnnnm",
  "mccccccccccnnbnnnnnbnncccccccm",
  "mnnbnnnnnnnnnbnnnnnbnnnnnnnnnm",
  "mnnbnnnnnnnnnbnnnnobnnnnnnnnnm",
  "mnnnnnnnnnnnnbcccccbccccnncccm",
  "mnnnnnnnnnnnnnnnnnnbnnnnnnnnnm",
  "mnnbnnbnnnnnnnnnnnnbccccnncccm",
  "mnnbnnbnnnnnnnnnnnnbnnnnnnnnnm",
  "monbonbnnnnnnnnnnnnbnnnnnnnnpm",
  "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 == 12 then
  screen:clear()
  screen:print(10,130,"Press Start To Finish With BETA",red)
  screen.flip()

end
 end
  end
   end
    end
     end
      end
        end
            while not Controls.read():start() do end
         elseif option == 2 then
             dofile("new.lua")
while not Controls.read():start() do end
         elseif option == 3 then
               screen:clear()
               screen:blit(0,0, Info)
              screen.flip()
               screen.waitVblankStart(600)
              screen:clear() 
             screen:blit(0,0, infotwo)
             screen.flip()     
              screen.waitVblankStart()            
             while not Controls.read():start() do end
while not Controls.read():start() do end  
       elseif option == 4 then 
       dofile("scrip.lua")
      while not Controls.read():start() do end
       elseif option == 5 then
       screen:print(0,0,"Bye",red)
         bgmusic = Music.stop()  
        break     
  end
 end
screen.flip()
 end


