Source Code: 3 Digit Combination Lock

Description

This code demonstrates the principles behind creating a simple text-based 3-Digit combination lock.

Instructions

  • Use left / right arrow keys to change selected number
  • Use up / down arrow keys to change selected number's value
  • Use the Enter Key to enter the code

Source Code

LoadFont "Arial", 2, 18, 0
SetFont 2

lockCode = 665
lockClosed = True
numberSelected = 1
Dim lock(3)

For i = 1 to 3
    lock(i) = RndRange( 1, 9 )
Next

SetFPS 60

Do

    If lockClosed

        If RightKey() Then numberSelected = numberSelected + 1
        If numberSelected > 3 Then numberSelected = 3
        If LeftKey() Then numberSelected = numberSelected - 1
        If numberSelected < 1 Then numberSelected = 1

        If UpKey() Then lock(numberSelected) = lock(numberSelected) + 1
        If lock(numberSelected) > 9 Then lock(numberSelected) = 9
        If DownKey() Then lock(numberSelected) = lock(numberSelected) - 1
        If lock(numberSelected) < 1 Then lock(numberSelected) = 1

        If EnterKey()
            If Str$(lockCode) = Str$(lock(1)) + Str$(lock(2)) + Str$(lock(3))
                lockClosed = False
            EndIf
        EndIf

        FlushKeys

    EndIf

    Cls 0

        If lockClosed

            For i = 1 to 3

                If numberSelected = i
                    Ink $FFFF00; switch drawing color to yellow
                Else
                    Ink $FFFFFF ; switch drawing color to white
                EndIf            

                Text 10*i, 10, lock(i)

            Next

        Else

            Ink $00FF00; green

            For i = 1 to 3

                Text 10*i, 10, lock(i)
                Text 10,30, "Lock Solved!"
            Next

        EndIf
    Sync
Loop

Related Pages

Categories: Input : Interfaces : Source Code