Source code for Torpedo Boat Commander 1.1

    0 print"use , . space":wait198,1:y=1063:c=54272:poke53280,2:poke53281,0:print"{brn}{clr}":poke214,20
    1 geta$:l=5:r=37:print:poke2023,86:a$(0)="    ":a$(1)="Z   ":a$(2)="ZZ  ":a$(3)="ZZZ "
    2 a=3:f=c+24:pokec+5,3:pokec+6,68:pokec+1,20:c$=chr$(13):p=1053:z=rnd(-ti):m=.2:n=.8
    3 printc$"   {down}{left}{left}{left}    {down}{up}{up}"c$a$(a)c$s$spc(l-e)"{brn}VV"spc(k)"{gry1}W{brn}"spc(r-l-3-k)"VV{up}{up}{wht}";
    4 pokec+4,129:pokec+4,128:ifpeek(p)<>32thenprint"{home}{down}{down}{down}{wht}*** you dead ***":end
    5 pokep,88:pokep+c,1:geta$:ifa$=","ora$="."thenp=p-1-2*(a$="."):p=p+(p>y)
    6 ifa$=" "andathena=a-1:fori=1to13:pokef,i*9:t=p+40*i:ifpeek(t)<>86thenpoket,90:poket-40,32:next
    7 pokef,15:g=rnd(1):l=l-((l>5)*(g<m+l/200))+((l<30)*(g>n)):ifr-l<9thenr=r+1:goto9
    8 h=rnd(1):r=r-((r>8)*(h<m+r/200))+((r<37)*(h>n))
    9 k=(r-l-3)*rnd(1):s=s+m:v=int(s):s$=str$(v):e=len(s$):a=a-(a<3)*.01:goto3

Commented source code:    

# Line 0-2: Print instructions, wait for the player to press a key, initalize game     

    0 
        print "use , . space"   # Print instructions
        wait 198,1              # Wait for a keypress
        y = 1063                # Constant: Rightmost position where ship can go
        c = 54272               # Constant for two purposes: 
                                #     (A) Start of soundchip registers, 
                                #     (B) Start of colour memory - start of screen memory
        poke 53280,2            # Set border colour = red
        poke 53281,0            # Set background colour = black
        print "{brn}{clr}"      # Set character colour = brown, then clear screen. This fills the colour memory with all brown.
        poke 214,20             # Place cursor on line 20 (does not take effect until a newline is printed)

    1 
        get a$                  # Consume the character that the player pressed to start the game
        l = 5                   # Set the left river bank to start in column 5
        r = 37                  # Set the right river bank to start in column 37
        print                   # Print a newline to make "poke214,0" take effect 
        poke 2023,86            # Put a river bank character in the bottom right corner so the ship can't sail to 
                                #     the right of the river
        a$(0) = "    "          # Set the strings to show 0, 1, 2 and 3 torpedoes left 
        a$(1) = "Z   "
        a$(2) = "ZZ  "
        a$(3) = "ZZZ "
        
    2 
        a = 3                   # Set the number of torpedoes left to 3
        f = c + 24              # Set f to the address of the volume register of the sound chip
        poke c + 5,3            # Set Attack/Decay for sound channel 1
        poke c + 6,68           # Set Sustain/Release for sound channel 1
        poke c + 1,20           # Set the highbyte of the frequency for sound channel 1
        c$ = chr$(13)           # Set c$ to hold a newline character 
        p = 1053                # Set the ship to start at screen address 1053 (29 columns from the top left corner)
        z = rnd(-ti)            # Init pseudo random number generator
        m = .2                  # Constant used in randomizing the track
        n = .8                  # Constant used in randomizing the track

# Line 3: Print loaded torpedo symbols, score, and a line with river banks and a mine.
        
    3 
        print c$                # Print a newline character
            "   {down}{left}{left}{left}    {down}{up}{up}" 
                                # (continued) Print spaces to erase torpedo symbols and current score
                                #     By erasing them before the screen scrolls, they won't scroll 
                                #     up and appear on the line above.
            c$ a$(a) c$ s$      # (continued) Print the number of torpedoes left and the score
            spc(l - e)          # (continued) Move the cursor right to where the left river bank is
            "{brn}VV"           # (continued) Print the left river bank
            spc(k)              # (continued) Move the cursor right to where the mine is 
            "{gry1}W{brn}"      # (continued) Print the mine 
            spc(r - l - 3 - k)  # (continued) Move the cursor right to where the right river bank is
            "VV{up}{up}{wht}";  # (continued) Print the right river bank, then place the cursor where it needs to
                                #     be to print the torpedo symbols and score next round.

# Line 4: Play a sound effect and check if the ship crashes into something, and if so, end the game.

    4 
        poke c + 4,129          # Start playing a sound in channel 1 (128 = noise waveform, 1 = start sound)
        poke c + 4,128          # Start fading out the sound in channel 1
        if peek(p) <> 32 then   # Is there an obstacle where the ship should be drawn?
            print"{home}{down}{down}{down}{wht}*** you dead ***"     # Print death message
            end                 # End the program

# Line 5: Draw the player's ship. Check for player input. Calculate the new ship position.

    5 
        poke p,88               # Draw the ship
        poke p + c,1            # Set the colour of the ship to white
        geta$                   # Read player input
        if a$ = "," or a$ = "." then     # Did the player type "," or "."?
            p = p - 1 - 2 * ( a$ = ".")  # Move the ship one step to the left or the right
            p = p + (p > y)     # If the new player position is outside the righthand side of the screen, decrease it by 1

# Line 6: Fire a torpedo, if the player requested it and one is loaded 

    6 
        if a$ = " " and a then  # Did the player press space and is there a torpedo available?
            a = a - 1           # Decrease the number of torpedoes
            for i = 1 to 13     # Send the torpedo 13 positions ahead from the ship
                poke f,i*9      # Alter the volume register in big steps to create a sound effect
                t = p + 40 * i  # Set the memory position where the torpedo should go next
                if peek(t) <> 86 then # Does the new torpedo postion NOT contain a river bank?
                    poke t,90   # Draw the torpedo
                    poke t-40,32# Erase the torpedo (or the ship!) from where it was before
                    next        # Note that the "next" is only executed until a river bank is reached.

# Line 7: Randomize the position of the left river bank.

    7 
        poke f,15               # Set the volume register to its normal value
        g = rnd(1)              # Randomize whether the left bank is moving left, staying, or moving right 
        l = l -                 # Set the new position of the left river bank 
            ((l > 5) * (g < m + l / 200))    # (continued) If the left river bank is in column 6+, it may move the left
                                             # Additionally, the chance is higher the further to the right it is
            + ((l < 30) * (g > n))           # (continued) Unless the left river bank is in column 30+, it may move to the right
        if r - l < 9 then       # Is the river less than 9 columns wide? 
            r = r + 1           # Move the right bank one column to the right
            goto 9              # Skip the line where the position of the right river bank may be changed

# Line 8: Randomize the position of the right river bank.

    8 
        h = rnd(1)              # Randomize whether the right bank is moving left, staying, or moving right 
        r = r - ((r > 8) * (h < m + r / 200)) + ((r < 37) * (h > n))
                                # Analogous to line 7, but this is for moving the right river bank

# Line 9: Randomize the position of the mine, increase score, reload torpedo, go back to start of game loop.

    9 
        k = (r - l - 3) * rnd(1)# Randomize the postion of the mine 
        s = s + m               # Increase score
        v = int(s)              # Get the integer part of the score, for display
        s$ = str$(v)            # Convert the score to a string 
        e = len(s$)             # Keep track of the length of the score string (needed on line 3)
        a = a - (a < 3) * .01   # Partially reload a torpedo, if there are less than three torpedoes loaded
        goto 3                  # Go back to the start of the game loop