user30141: I have some background music. If I put it at the top of the void loop(), it'll first play the music and then execute the game code afterwards. 
How do I have the code run while the background music is playing?

user30005: Run the music through a time Interrupt.

user30146: It all depends on the architecture of your game. The first games probably interleaved their music and gameplay, as the comment above says. This is probably the level of complexity you are looking for, on an Arduino.
First you need to pick some "framerate" that the game advances with. You could, for example, play 4 frames of music for every video frame, or something like that. It also depends how you are playing music. If you are just playing notes with the analog out, this approach would work. Here is some pseudocode that updates the PWM output 4 times faster than the game code:
set n = 0
set quit = false

// main game loop
while !quit
    // set music output pwm here
    if n % 4 == 0
        // advance game here
    endif
    n++
endwhile

If you are playing music from something like an SD card, you are constrained by the bit rate of the music.
Also, here is an article on Arduino protothreading that might interest you. https://create.arduino.cc/projecthub/reanimationxp/how-to-multithread-an-arduino-protothreading-tutorial-dd2c37. My suggestion is to look at how the old game systems implemented their state machines.

30141: <p>I have some background music. If I put it at the top of the <code>void loop()</code>, it'll first play the music and then execute the game code afterwards. </p>

<p>How do I have the code run while the background music is playing?</p>
 30005: <p>Run the music through a time Interrupt.</p>
 30146: <p>It all depends on the architecture of your game. The first games probably interleaved their music and gameplay, as the comment above says. This is probably the level of complexity you are looking for, on an Arduino.</p>

<p>First you need to pick some "framerate" that the game advances with. You could, for example, play 4 frames of music for every video frame, or something like that. It also depends how you are playing music. If you are just playing notes with the analog out, this approach would work. Here is some pseudocode that updates the PWM output 4 times faster than the game code:</p>

<pre><code>set n = 0
set quit = false

// main game loop
while !quit
    // set music output pwm here
    if n % 4 == 0
        // advance game here
    endif
    n++
endwhile
</code></pre>

<p>If you are playing music from something like an SD card, you are constrained by the bit rate of the music.</p>

<p>Also, here is an article on Arduino <strong>protothreading</strong> that might interest you. <a href="https://create.arduino.cc/projecthub/reanimationxp/how-to-multithread-an-arduino-protothreading-tutorial-dd2c37" rel="nofollow noreferrer">https://create.arduino.cc/projecthub/reanimationxp/how-to-multithread-an-arduino-protothreading-tutorial-dd2c37</a>. My suggestion is to look at how the old game systems implemented their state machines.</p>
