fc64js

fc64js - Tutorial - Snake (js)

Playing sound effects

We can further improve our rom by adding some simple sound effects

The beep function offers a convenient means of triggering sounds - it requires frequency, duration, and force arguments to be provided (the first two being integers and the last being a boolean). The frequency parameter is the beep’s frequency (measured in hertz), the duration parameter is how long the beep should play for (measured in ticks), and force determines whether or not the beep should override any currently playing beep or not

A short and relatively high pitched beep suits the positive event of eating a piece of fruit, and a longer relatively low pitched beep suits the negative event of losing

We’ll simply add calls to beep (with appropriate arguments set) at the relevant points in romLoop:

function romLoop() {
      ...
      if (snake.isDead()) {
        beep(280, 15, true);
        changeState(states.gameOver);
      }
      if (snake.body[0].equals(fruit)) {
        beep(1000, 5, true);
        ...

Full code at this point

Continue to the next step


Jump to step: Introduction · Project setup · Creating the snake · Drawing the snake · Moving the snake · Throttling the speed · Input handling · Placing the fruit · Eating the fruit · Losing the game · Managing state · Playing sound effects · Bug fixing · Distribution