#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../webpage-assets/ce.css" />
#+HTML_HEAD: <link rel="icon" type="image/png" href="http://rachel.likespizza.com/web-assets/images/favicon-moose.png">

# ---------------------------------------------------------------- #

* Lesson 05: Random numbers

** Getting set up

#+ATTR_LATEX: :width 0.75\textwidth
#+ATTR_HTML: :style width:50%; float: right; margin: 20px;
[[file:images/lesson01-ticblank.png]]

You can either edit over your existing code, or start a new program.
If you want to start a new program, press *ESCAPE* until you get to
the main prompt:

Then type in =new lua=, hit *ENTER*, then =save GAMENAME.LUA=, hit
*ENTER*. (Replace "GAMENAME" with whatever you want.)

#+BEGIN_SRC terminal
  > new lua
  new cart has been created

  > save GAMENAME.lua
  cart GAMENAME.lua saved!
#+END_SRC

#+ATTR_HTML: :style clear:both;
--

** Looking at the SFX (Sound Effects) Editor

Press the *F4* button to open up the SFX editor.

[[file:images/lesson-sfx-animation.gif]]

Along the left side is the sound bank. You can click each one, and
then play the piano on the bottom of the screen to hear it (or use
keyboard keys Z through M to play!)

There are three default sounds already provided, but you can play
around with the other channels and the waveforms. We're only going
to do a small amount with sounds, so this will be a place where you
can experiment on your own.

** About If statements

With *if statements* we can ask "yes/no" questions in our code, then
execute certain commands only /if/ the result of that question is
"yes" (or, more specifically, "true").
This is what we use for checking if a button is pressed while the
game is running. In this case, if a button is pressed, play a sound.

If statements take this form: *if CONDITION then COMMAND end*.
They can also take multiple lines as well:

#+BEGIN_QUOTE
*if CONDITION then*

*COMMAND1*

*COMMAND2*

*end*
#+END_QUOTE

** Editing the code

Press the *F1* key to go to the code editor. We're going to add if
statements to check for each button - there are 8 buttons for one
"player" in Tic-80, similar to a game pad on a game console.

[[file:images/tic80-buttons.png]]

#+BEGIN_SRC lua
  function TIC()
    if btn(0) then sfx(1, "C-4", 10) end
    if btn(1) then sfx(1, "D-4", 10) end
    if btn(2) then sfx(1, "E-4", 10) end
    if btn(3) then sfx(1, "F-4", 10) end

    if btn(4) then sfx(1, "G-4", 10) end
    if btn(5) then sfx(1, "A-4", 10) end
    if btn(6) then sfx(1, "B-4", 10) end
    if btn(7) then sfx(1, "C-5", 10) end

    cls(13)
    print("PRESS ARROW KEYS",84,80)
    print("OR Z, X, A, S!",84,88)
  end
#+END_SRC


** Testing

Run the program. The screen doesn't have much interesting to show,
but you can press the *ARROW KEYS* or the *Z, X, A, S* keys to hear
different tones.

[[file:images/lesson-sfx.png]]

** Functions
  - =sfx(id, note, duration, channel, volume, speed)=

    [[https://github.com/nesbox/TIC-80/wiki/sfx]]

    The =sfx= function will play a sound effect. Notes will be in
    the form of "C-4", D-4", "E-4" and so on. You can change the
    number to get a different /octave/, or change the =-= to =#= to
    change it to a /sharp note/. Valid notes are =A= through =G=.

  - =btn(id)=

    [[https://github.com/nesbox/TIC-80/wiki/btn]]

    Checks to see if a button is pressed, with the player 1 buttons
    being 0 through 7.

    #+ATTR_HTML: :style margin: 20px auto;
    | ID |   | Key   |
    |----+---+-------|
    |  0 |   | UP    |
    |  1 |   | DOWN  |
    |  2 |   | LEFT  |
    |  3 |   | RIGHT |
    |  4 |   | Z     |
    |  5 |   | X     |
    |  6 |   | A     |
    |  7 |   | S     |

** Save your work

Make sure to press the *CTRL+S* key, or press *ESC* to go to the
prompt and type =save= to save your work! If you want to access the
folder with your code, type =folder=.

If you have a USB flash drive, make sure to copy your file over so
you don't lose it when the computer resets!

** Back to the course page!

[[../index.org][⬅️ Once you're all done, go back to the course homepage!]]
