' demo use of the keypad scan
' a complete help of Annex32 can be found here
' https://cicciocb.com/annex32help/V1.442/
dim rows(4) = 26, 25, 33, 32
dim cols(4) = 35, 34, 39, 36
dim sym$(16) = "1", "2", "3", "A", "4", "5", "6", "B", "7", "8", "9", "C", "*", "0", "#", "D"

dim frutinhas(80) = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
posicaoPacman = 1
quantidadeFrutinhas = 0

dim fantasma(8) = 0x00,0x0E,0x1F,0x15,0x1F,0x1B,0x11,0x00
dim parede(8) = 3, 23, 52, 53, 35, 36, 37, 41

direcaoPacman = 1
dim pacmanCim(8) = 0x00,0x11,0x1B,0x1B,0x1F,0x0E,0x00,0x00 '0
dim pacmanDir(8) = 0x00,0x0F,0x1E,0x18,0x18,0x1E,0x0F,0x00 '1
dim pacmanBai(8) = 0x00,0x00,0x0E,0x1F,0x1B,0x1B,0x11,0x00 '2
dim pacmanEsq(8) = 0x00,0x1E,0x0F,0x03,0x03,0x0F,0x1E,0x00 '3

score = 0
returnColisao = 0
contadorTempoFantasma = 0
gatilhoTempoFantasma = 15
contadorTempoFruta = 0
posicaoFantasma = 80
finished = 0

for z = 0 to 3
  pin.mode rows(z), output
  pin.mode cols(z), input, pulldown
  pin(rows(z)) = 0
next z

k$ = ""
Kp$ = ""
line$ = space$(16)

I2C.SETUP 13, 12
LCD.INIT &h27, 20, 4

I2C.SETUP 13, 12
LCD.INIT &h27, 20, 4

LCD.print 1, 1, "Crew:  Christyelen K"
LCD.print 8, 2, "Luis K."
LCD.print 8, 3, "Thomas R."
LCD.print 8, 4, "Yuri H."
pause 2000
LCD.CLS

for i = 0 to 3
  quantidadeFrutinhas = quantidadeFrutinhas + 1
  frutinhas(quantidadeFrutinhas) = RND(79) + 1
  'gerar um em cima do outro
next i

sub desenhaParede(posicao) 
    linha = INT(posicao / 20) + 1
    coluna = posicao MOD 20

    if (coluna = 0) then
      linha = linha - 1
      coluna = 20
    endif

    dim b(8) = 0x17,0x00,0x1D,0x1D,0x00,0x17,0x17,0x00
    LCD.CUSTOM 6, b()
    LCD.print coluna, linha, ""
    LCD.WRITE 6
end sub

sub desenharParedes(x)
 for i = 0 to 7
    desenhaParede parede(i)
  next i
end sub

sub desenhaFruta(posicao) 
    linha = INT(posicao / 20) + 1
    coluna = posicao MOD 20

    if (coluna = 0) then
      linha = linha - 1
      coluna = 20
    endif

    dim a(8) = 0x00,0x01,0x03,0x06,0x0A,0x13,0x1B,0x18
    LCD.CUSTOM 5, a()
    LCD.print coluna, linha, ""
    LCD.WRITE 5
end sub

sub desenharFrutinhas(x)
  for i = 0 to quantidadeFrutinhas
    desenhaFruta frutinhas(i)
  next i
end sub

sub desenharFantasma(x)
    linha = INT(posicaoFantasma / 20) + 1
    coluna = posicaoFantasma MOD 20

    if (coluna = 0) then
      linha = linha - 1
      coluna = 20
    endif

    dim a(8) = 0x00,0x01,0x03,0x06,0x0A,0x13,0x1B,0x18
    LCD.CUSTOM 7, fantasma()
    LCD.print coluna, linha, ""
    LCD.WRITE 7
end sub

sub desenharPacman(x)
  linha = INT(posicaoPacman / 20) + 1
  coluna = posicaoPacman MOD 20

  if (coluna = 0) then
    linha = linha - 1
    coluna = 20
  endif

  if (direcaoPacman = 0) then
    LCD.CUSTOM 1, pacmanCim()
    LCD.print coluna, linha, ""
    LCD.WRITE 1
  endif

  if (direcaoPacman = 1) then
    LCD.CUSTOM 2, pacmanDir()
    LCD.print coluna, linha, ""
    LCD.WRITE 2
  endif

  if (direcaoPacman = 2) then
    LCD.CUSTOM 3, pacmanBai()
    LCD.print coluna, linha, ""
    LCD.WRITE 3
  endif

  if (direcaoPacman = 3) then
    LCD.CUSTOM 4, pacmanEsq()
    LCD.print coluna, linha, ""
    LCD.WRITE 4
  endif 
end sub

sub limpar(x)
  linha = INT(x / 20) + 1
  coluna = x MOD 20

  if (coluna = 0) then
    linha = linha - 1
    coluna = 20
  endif
  LCD.print coluna, linha, " "
end sub

sub gatilhoFantasma(x)
  LOCAL aux2
  jaMexeu = 0

  linhaPacman = INT(posicaoPacman / 20) + 1
  colunaPacman = posicaoPacman MOD 20

  if (colunaPacman = 0) then
      linhaPacman = linhaPacman - 1
      colunaPacman = 20
  endif

  linhaFantasma = INT(posicaoFantasma / 20) + 1
  colunaFantasma = posicaoFantasma MOD 20

  limpar posicaoFantasma

  if (colunaFantasma = 0) then
      linhaFantasma = linhaFantasma - 1
      colunaFantasma = 20
  endif

  aux2 = posicaoFantasma

  if (colunaPacman < colunaFantasma) and (jaMexeu = 0) then
    esquerda posicaoFantasma
    if (aux2 <> posicaoFantasma) then
      jaMexeu = 1
    end if

  end if

  if (colunaPacman > colunaFantasma) and (jaMexeu = 0) then
    direita posicaoFantasma
    if (aux2 <> posicaoFantasma) then
      jaMexeu = 1
    end if
  end if

  if (linhaPacman > linhaFantasma) and (jaMexeu = 0) then
    baixo posicaoFantasma
    if (aux2 <> posicaoFantasma) then
      jaMexeu = 1
    end if
  end if

  if (linhaPacman < linhaFantasma) and (jaMexeu = 0) then
    cima posicaoFantasma
    if (aux2 <> posicaoFantasma) then
      jaMexeu = 1
    end if
  end if

  desenharFantasma 1

  for i = 1 to quantidadeFrutinhas
    if (frutinhas(i) = aux2) then
      desenhaFruta frutinhas(i)
    endif
  next i
end sub

sub gatilhoFruta(x)
  if (quantidadeFrutinhas < 20)
    criou_fruta = 0

    while criou_fruta = 0

      nova_posicao_fruta = RND(79) + 1

      conflitoo = 0
      for i = 0 to 7
        if (parede(i) = nova_posicao_fruta) then
          conflitoo = 1
        endif
      next i

      if (conflitoo = 0) then
        criou_fruta = 1
        quantidadeFrutinhas = quantidadeFrutinhas + 1
        frutinhas(quantidadeFrutinhas) = nova_posicao_fruta
        desenhaFruta nova_posicao_fruta
      endif

    wend

  endif
end sub

desenharParedes 1
desenharFrutinhas 1
desenharPacman 1
desenharFantasma 1

do

  if (finished = 0) then

    contadorTempoFantasma = contadorTempoFantasma + 1
    contadorTempoFruta = contadorTempoFruta + 1

    if (contadorTempoFantasma > gatilhoTempoFantasma) then

      if (gatilhoTempoFantasma > 8) then
        gatilhoTempoFantasma = gatilhoTempoFantasma -1
      endif

      contadorTempoFantasma = 0
      gatilhoFantasma 1
    endif

    if (contadorTempoFruta > 50) then
      contadorTempoFruta = 0
      gatilhoFruta 1
    endif

    if (posicaoFantasma = posicaoPacman) then
      finished = 1
      LCD.CLS
      LCD.print 5, 2, "You lose"
    endif

    scan k$
    if (k$ <> kp$) and (k$ <> "") then

      limpar posicaoPacman

      if (k$ = "2") then
        cima posicaoPacman
      endif

      if (k$ = "8") then
        baixo posicaoPacman
      endif

      if (k$ = "6") then
        direita posicaoPacman
      endif

      if (k$ = "4") then
        esquerda posicaoPacman
      endif

      desenharPacman 1

      for i = 1 to 21
        if (posicaoPacman = frutinhas(i)) then

          blabla = frutinhas(quantidadeFrutinhas)
          frutinhas(i) = blabla
          score = score + 1
          frutinhas(quantidadeFrutinhas) = 0
          quantidadeFrutinhas = quantidadeFrutinhas - 1

        endif
      next i

      if (quantidadeFrutinhas = 0) then
            finished = 1
            LCD.CLS
            LCD.print 5, 2, "You win"
            LCD.print 5, 3, "Score: " + str$(score)
      endif

    end if
    kp$ = k$
    'pause 100

  end if
loop

sub scan(key$)
  local r, c, l, code
  code = -1
  for r = 0 to 3
    pin(rows(r)) = 1
    for c = 0 to 3
      l = pin(cols(c))
      if (l=1) then
        code = r*4 + c
        exit for
      end if
    next c
    pin(rows(r)) = 0
    if (code <> -1) then exit for
  next r
  if (code <> -1) then key$ = sym$(code) else key$ = ""
end sub

sub cima(posicaoEntidade)
  aux = posicaoEntidade - 20

  if (aux <= 0) then
    aux = posicaoEntidade + 60
  endif

  checaColisao aux
  if (returnColisao = 0) then 
    posicaoEntidade = aux
  endif

  direcaoPacman = 0
end sub

sub baixo(posicaoEntidade)
  aux = posicaoEntidade + 20
  if (aux >= 80) then
    aux = posicaoEntidade - 60
  endif

  checaColisao aux
  if (returnColisao = 0) then 
    posicaoEntidade = aux
  endif

  direcaoPacman = 2
end sub

sub direita(posicaoEntidade)
  aux = posicaoEntidade + 1
  posicaoEntidade = aux
  teste = 0

  if ((aux MOD 20) = 1) then
    teste = 1
    cima posicaoEntidade

    if (returnColisao = 1) then 
      posicaoEntidade = posicaoEntidade - 1
    endif

  endif

  if (teste = 0) then
    checaColisao aux

    if (returnColisao = 1) then 
      posicaoEntidade = posicaoEntidade - 1
    endif
  endif

  direcaoPacman = 1
end sub

sub esquerda(posicaoEntidade)
  aux = posicaoEntidade - 1
  posicaoEntidade = aux
  teste = 0

  if ((aux MOD 20) = 0) then
    teste = 1
    baixo posicaoEntidade

    if (returnColisao = 1) then 
      posicaoEntidade = posicaoEntidade + 1
    endif
  endif

  if (teste = 0) then
    checaColisao aux

    if (returnColisao = 1) then 
      posicaoEntidade = posicaoEntidade + 1
    endif
  endif

  direcaoPacman = 3
end sub

sub checaColisao(posicao)
  returnColisao = 0

  for i = 0 to 7
    if (parede(i) = posicao) then
      returnColisao = 1
    endif
  next i
end sub