''SUPER MARIO QB v.05  - a very quick port to FreeBASIC
''-----------------------------------------------------------------------
''If you ever wanted a QBasic mario game, well, this is it! Using a
''sophisticated pure QB scrolling engine and sound blaster compatibility
''this game is the first in a new series of games by Dark Flare Software.
''Hope you have fun playing it!
''-----------------------------------------------------------------------
''email:spheresfantasy@yahoo.ca
''-----------------------------------------------------------------------
'' Change log:
'' Converted to FreeBASIC in 2005 (who did the conversion?)
''          sorry, all the low-level SoundBlaster routines were stripped 
''          due to Win32 incompatibilities!
'' Added sounds and music, top scores and linux conversion in 2005 by GOK
''          Uses origional wav files for background music + new wav files
''          for sound effects
'' Converted to 32 bit colors and scaled x 2 in 2008 by GOK
''-----------------------------------------------------------------------


#include once "fbgfx.bi"
#include once "dynsound.bi"               '' dynamically load fmod

'' Declares for all subs and functions
declare sub INTRO ()                 
declare sub DELAY ()                   
declare sub LEVELFINISH ()
declare sub ENDING ()                  
declare sub DRAWCOINS ()
declare sub DRAWMARIO ()
declare sub GETFONT (byval y as integer)
declare sub LOADLEVEL ()
declare sub LOADGPH (n as string)      
declare sub GETGPH ()                  
declare sub DRAWTILES ()
declare sub CONTROL ()
declare sub SETGAME ()
declare sub DRAWENEMY ()
declare sub NEWGAME ()           
declare sub LOSELIFE ()                
declare sub PUTFONT (fontnum as string, byval xstart as integer)
declare sub TIDYUP ()
declare sub Box (byval X1 as integer, byval Y1 as integer, byval X2 as integer, _
        byval Y2 as integer, Title as string)
declare sub SAVESCORES(pname() as string, pscore() as integer)
declare function LOADSCORES(pname() as string, pscore() as integer) as integer

'' Global variables
dim shared as integer  world         '' world number, always 1
dim shared as integer  level         '' level number
dim shared as integer  score         '' players score
dim shared as integer  numcoins      '' number of coins collected
dim shared as integer  lives         '' number of lives left

'' die = 0 alive, rspond to keyboard and draw mario
'' positive = stop drawing mario and increment die, dead controls
'' 50 = restart level or end depending on nextlevel flag
dim shared as integer  die               
dim shared as integer  nextlevel     '' flag to indicate next level expected, otherwise game ends


dim shared as integer  xlen          '' Scroll length of level (count of horizontal tiles - 21)
dim shared as integer  dirn          '' Direction mario is facing 0 (right) or 4 (left)
dim shared as integer  move          '' move = 1 if SC_RIGHT or SC_LEFT key preses, otherwise 0
dim shared as integer  ymove         '' 0 to 3, increments/decrements ytile
dim shared as integer  ytile         '' vertical position of mario

dim shared as integer  drop          '' if drop = 1 then mario is droping
dim shared as integer  jump          '' Mario is jumping, incremented each loop up to 14, then jump = 0 and drop = 1
dim shared as integer  onground      '' Mario is on the ground

dim shared as integer  frcount       '' Frame count for animations 0, 1, 2, 3
dim shared as integer  cframe        '' Frame to draw coins
dim shared as integer  cfrdir        '' Coin frame direction of spin flag 1 or -1
dim shared as integer  spframe       '' Frame to draw of mario walking 0 or 1 (0 also used for not moving)
dim shared as integer  eframe        '' Frame to draw of enemy walking 0 or 1

dim shared as integer  n1            '' 0 to 15, pixel offset of mario, increments/decrements xpl
dim shared as integer  xpl           '' Number of tiles scrolled
dim shared as integer  px, py        '' x,y position of mario on screen (x will stop changing when frames scroll)

dim shared as single   rrate = 1/40  '' loop speed limiter
dim shared as integer  running       '' Flag to exit game loop if 0
dim shared as integer  fastexit      '' finish game without credits

dim shared as byte ptr sprites(0 to 120)        '' Sprite data in "garphics.bmp" bitmap
dim shared as byte ptr font(0 to 9)             '' Font data in "garphics.bmp" bitmap
dim shared as short    map(-1 to 400, 0 to 14)  '' Data file "levelX.txt"
dim shared as short    coins(0 to 100, 0 to 2)  '' Data file "coinsX.txt"
dim shared as integer  enemy(0 to 40, 0 to 5)   '' Data in read statements

'' Sound support handles
dim shared as integer sounddie, soundm1,  soundm2,  soundm3, soundgmo
dim shared as integer soundlgh, soundcon, soundspl, soundbep

    '' Start of main
    dim as integer ptr r

    r = init_dyn_fmod()                                                '' Initialise fmod
    sounddie = Load_dyn_Sound(exepath + "/data/die.wav")              '' load sound
    soundm1  = Load_dyn_Sound(exepath + "/data/music1.wav", 2)        '' load sound (looped)
    soundm2  = Load_dyn_Sound(exepath + "/data/music2.wav", 2)        '' load sound (looped)
    soundm3  = Load_dyn_Sound(exepath + "/data/music3.wav", 2)        '' load sound (looped)
    soundgmo = Load_dyn_Sound(exepath + "/data/gameover.wav")         '' load sound
    soundcon = Load_dyn_Sound(exepath + "/data/coin.wav")             '' load sound
    soundspl = Load_dyn_Sound(exepath + "/data/splash.wav")           '' load sound
    soundbep = Load_dyn_Sound(exepath + "/data/beep1.wav")            '' load sound
    soundlgh = Load_dyn_Sound(exepath + "/data/laugh.wav")            '' load sound

    ''==============================================================================
    ''dim as integer frame
    dim as integer m_x, m_y   '' mouse position
    dim as integer pause      '' used in loop timer
    dim as double tm          '' loop timer
        
    cfrdir = 1

    screenres 640, 400, 32, 2, 0
    SETMOUSE ,, 0

    INTRO()

    screenset 1, 0
    Play_dyn_Sound(soundm1)             '' Background music
    LOADGPH ("graphics")
    GETGPH ()

    NEWGAME ()

    running  = not 0
    fastexit = not 0
    do
        SETGAME ()

        tm = timer()
        
        do
            '' fast exit
            if(multikey(FB.SC_ESCAPE)) then fastexit = 0
   
            '' turn on and off mouse
            getmouse m_x, m_y
            if m_x = -1 and m_y = -1 then
                SETMOUSE ,, 1
            else
                SETMOUSE ,, 0
            end if
   
            '' draw all tiles
            DRAWTILES ()

            '' Play control
            if die = 0 then CONTROL ()
            
            '' display drawn graphics
            screencopy 1, 0

            '' Pixel scrolling
            if n1 > 15 then
                n1 = n1 - 16
                xpl = xpl - 1
            elseif n1 < 0 then
                n1 = 16 + n1
                xpl = xpl + 1
            end if
            
            '' Loop timer - Keelings way
            do while (timer - tm) < rrate
                sleep 10
                pause = pause + 1
                if pause > 10 then exit do
            loop
            pause = 0
            tm = timer
            
            '' Frame count for animation
            frcount = frcount + 1
        
            if frcount = 3 then
                frcount = 0
                if eframe = 1 then
                    eframe = 0
                else
                    eframe = 1
                end if
                if move = 1 then
                    if spframe = 1 then
                        spframe = 0
                    else
                        spframe = 1
                    end if
                else
                    spframe = 0
                end if
            end if
        
            ''frame = frame + 1
        
            '' levels 2 and 3 are hard coded to finish when you reach cetrain points
            if (xpl = 370 and level = 2) or (xpl = 334 and level = 3) then LEVELFINISH
            
            '' level 4 does not exist
            if level = 4 then running = 0 
            if not fastexit then running = 0
            if not running then exit do, do
                        
            '' die < 50 sets the delay after dying before return to start of level
        loop while die < 50 and nextlevel = 0
        if die > 0 then LOSELIFE ()
    loop
        
    if fastexit then ENDING ()
    TIDYUP ()
    
    
    '' enemy position
    '' minx, maxx, y
    level1:

    data 240,320,110, 560,608,78
    data 720,816,110, 832,928,110, 1392,1488,62
    data 1552,1744,142, 2336,2448,126, 2688,2704,30
    data 3008,3200,94, 3440,3648,126, 3744,3872,94, 3744,3920,142
    data 4320,4384,126, 4400,4464,126, 4320,4368,30, 4144,4224,126
    data 999

    level2:

    data 576,640,142, 960,992,94, 864,1072,158
    data 1488,1504,142, 1808,1856,30, 1856,1952,94
    data 2560,2576,94, 2592,2608,94
    data 2864,3008,158, 3024,3168,158, 3360,3392,78, 3680,3760,94
    data 4432,4560,94, 4752,4832,94, 4848,4912,94, 5040,5056,142
    data 5104,5120,110, 5296,5328,78, 5392,5424,78
    data 999

    level3:

    data 416,528,126, 448,560,126, 480,592,126, 832,880,94
    data 1344,1536,142, 1424,1520,110, 1968,2032,126, 2224,2256,110
    data 2320,2352,78, 2592,2608,62, 2672,2688,94, 2816,3008,126
    data 3088,3104,78, 3168,3264,62, 3488,3520,32
    data 999

''==============================================================================

sub CONTROL ()
    
    dim as integer xmin, xmax, value, x, y
    dim as integer side
    static as integer xst, xnd    '' pixel colision

    if not multikey(FB.SC_LEFT) or _
            not multikey(FB.SC_RIGHT) or _
            not multikey(FB.SC_DOWN) or _
            not multikey(FB.SC_SPACE) then
        move = 0
    end if


    if multikey(FB.SC_Q) then LEVELFINISH ()
    if multikey(FB.SC_S) then close_dyn_fmod()
        
    if multikey(FB.SC_DOWN) then
        if level = 1 then
            if xpl * 16 + 15 >= 4543 and _
                    xpl * 16 <= 4559 and _
                    ytile = 1 and _
                    jump = 0 and _
                    drop = 0 then
                    
                LEVELFINISH ()
            end if
        end if
    end if

    if multikey(FB.SC_LEFT) then
        dirn = 4
        value = 0
    
        if ymove = 3 then
            y = 0
        else
            y = 1
        end if
    
        select case map(xpl + xst, ytile + y)
        case 0, 1, 20 to 28, 40, 88 to 90, 107 to 110, 91, 92, 111, 112
            value = 1: move = 1
        end select
        if xpl > 0 and px = 112 and value > 0 then
            n1 = n1 + 2
        elseif value > 0 then
            if px > 0 then px = px - 2
        end if
    end if

    if multikey(FB.SC_RIGHT) then
        dirn = 0
        value = 0
        if ymove = 3 then
            y = 0
        else
            y = 1
        end if
        select case map(xpl + xnd, ytile + y)
        case 0, 1, 8, 20 to 28, 40, 88 to 90, 107 to 110, 91, 92, 111, 112
            value = 1: move = 1
        end select
        if xpl <= xlen and px = 112 and value > 0 then
            n1 = n1 - 2
        elseif value > 0 then
            if px < 304 then px = px + 2
        end if
    end if

    if multikey(FB.SC_SPACE) then
        if jump = 0 and drop = 0 and onground = 1 then
            jump = 1
            onground = 0
            '' Jump sound ?
        end if
    end if

    if inkey = chr(255) + "k" then
            running = 0
    end if
 

    ''======================

    xmin = xst: xmax = xnd

    if px < 112 or px > 112 then
        xst = (px - 8) \ 16: xnd = xst + 1
        if xst < 0 then xst = 0 ' hack
    else
        xst = 6: xnd = 7
    end if

    if (n1 = 14 and px = 112) or (dirn = 0 and px <> 112) then
        xmin = xst: xmax = xst
    elseif (n1 = 0 and px = 112) or (dirn = 4 and px <> 112) then
        xmin = xnd: xmax = xnd
    end if

    ''=================
    for x = xmin to xmax
        select case map(xpl + x, ytile + 1)
        case 0, 1, 20 to 28, 40, 88 to 90, 107 to 110, 91, 92, 111, 112
            if jump = 0 then
                drop = 1
                onground = 0
            end if
        case else
            onground = 1
            drop = 0
            exit for
        end select
    next

    if drop = 1 and jump = 0 then
        ymove = ymove - 1
        if ymove < 0 then
            ymove = 3
            ytile = ytile + 1
        end if
        py = py + 4
        if py > 200 then
            die = 1
            Play_dyn_Sound(soundspl)
        end if
    end if

    if jump > 0 and drop = 0 and jump < 14 then
        jump = jump + 1
        side = 0
        for x = xst to xnd
            select case map(xpl + x, ytile)
            case 0, 1, 20 to 28, 40, 88 to 90, 107 to 110, 91, 92, 111, 112
                side = x
            end select
        next
        xmin = xst: xmax = xnd
        select case side
        case xst
            if (n1 <> 14 and px = 112) or (dirn = 4 and px <> 112) then
                xmin = xnd: xmax = xnd
            end if
        case xnd
            if (n1 <> 0 and px = 112) or (dirn = 0 and px <> 112) then
                xmin = xst: xmax = xst
            end if
        end select
        for x = xmin to xmax
            select case map(xpl + x, ytile)
            case 0, 1, 2, 20 to 28, 40, 88 to 90, 107 to 110, 91, 92, 111, 112
                if py > 12 then
                    ymove = ymove + 1
                    if ymove = 4 then
                        ymove = 0
                        ytile = ytile - 1
                    end if
                    py = py - 4
                    exit for
                end if
            end select
        next
    elseif jump = 14 then
        jump = 0 : drop = 1
    end if

end sub
''==============================================================================
sub DELAY ()
    dim as double tm
    tm = timer
    do: loop while timer - tm < .04
end sub
''==============================================================================
sub DRAWCOINS ()
    
    dim as integer cnum, newxpl
    dim as integer  xdat(0 to 1)
    
    xdat(0) = xpl * 16
    xdat(1) = xpl * 16 + 300
    for cnum = 0 to 100
        if coins(cnum, 2) = 1 then
            if coins(cnum, 0) >= xdat(0) and coins(cnum, 0) <= xdat(1) then
                newxpl = coins(cnum, 0) - xdat(0) + n1
                put (newxpl*2, coins(cnum, 1)*2), sprites( 40 + cframe), trans
                if newxpl + 15 > px and newxpl < px + 15 then
                    if py + 20 > coins(cnum, 1) and py < coins(cnum, 1) + 15 then
                        numcoins = numcoins + 1
                        Play_dyn_Sound(soundcon)
                        if numcoins = 100 then
                            numcoins = 0
                            lives = lives + 1
                        end if
                        score = score + 50
                        coins(cnum, 2) = 0
                    end if
                end if
            end if
        end if
    next
    
end sub
''==============================================================================
sub DRAWENEMY ()
    
    dim as integer newxpl, xchg
    
    dim enemynum as integer
    
    for enemynum = 0 to 20
        if enemy(enemynum, 3) <> 999 and enemy(enemynum, 5) > 0 then
            if (xpl + 19) * 16 > enemy(enemynum, 4) and enemy(enemynum, 5) = 1 and ((xpl - 1) * 16 < enemy(enemynum, 4)) then
                newxpl = enemy(enemynum, 4) + xpl * -16 + n1
                if newxpl > -16 and newxpl < 320 then
                    put (newxpl*2, enemy(enemynum, 1)*2), sprites( 49 + eframe + enemy(enemynum, 2)), trans
                    put (newxpl*2, (enemy(enemynum, 1) + 16)*2), sprites( 69 + eframe + enemy(enemynum, 2)), trans
                end if
            elseif enemy(enemynum, 5) > 1 then
                newxpl = enemy(enemynum, 4) + xpl * -16 + n1
                if newxpl > -16 and newxpl < 320 then
                    put (newxpl*2, (enemy(enemynum, 1) + 4)*2), sprites( 48), trans
                    if enemy(enemynum, 5) < 10 then
                        enemy(enemynum, 5) = enemy(enemynum, 5) + 1
                        enemy(enemynum, 1) = enemy(enemynum, 1) + 1
                    end if
                end if
            end if
            if enemy(enemynum, 5) = 1 then
                if enemy(enemynum, 2) = 0 then
                    xchg = -1
                else
                    xchg = 1
                end if
                enemy(enemynum, 4) = enemy(enemynum, 4) + 2 * xchg
                if enemy(enemynum, 4) = enemy(enemynum, 3) then enemy(enemynum, 2) = 4
                if enemy(enemynum, 4) = enemy(enemynum, 0) then enemy(enemynum, 2) = 0
                if py + 10 > enemy(enemynum, 1) and py < enemy(enemynum, 1) + 15 then
                    newxpl = enemy(enemynum, 4) + xpl * -16 + n1
                    if newxpl + 15 > px and newxpl < px + 15 then
                        if drop = 1 then
                            enemy(enemynum, 5) = 2: jump = 8
                            drop = 0
                            score = score + 100
                            Play_dyn_Sound(soundbep)
                        else
                            if die = 0 then
                                die = 1
                                Play_dyn_Sound(sounddie)
                            end if
                        end if
                    end if
                end if
            end if
        end if
    next
    
end sub
''==============================================================================
sub DRAWMARIO ()
    
    if die = 0 then
        if jump = 0 then
            put (px*2, py*2), sprites(9 + spframe + dirn), trans
            put (px*2, (py + 16)*2), sprites(29 + spframe + dirn), trans
        elseif jump > 0 then
            put (px*2, py*2), sprites((dirn \ 2) * 20 + 17), trans
            put (px*2, (py + 16)*2), sprites(((dirn \ 2) + 1) * 20 + 17), trans
        end if
    else
        die = die + 1
    end if
    
end sub
''==============================================================================
sub DRAWTILES ()
    'this sub draws the background tiles and then the sprites.
    
    dim as integer x, y, xmap, ymap
    
    for y = 8 to 184 step 16
        ymap = (y - 8) \ 16
        for x = n1 - 16 to 303 + 16 step 16
            xmap = (x - n1) \ 16 + xpl
            put (x*2, y*2), sprites( map(xmap, ymap)), pset
        next
    next
    DRAWMARIO ()
    DRAWENEMY ()
    DRAWCOINS ()
    PUTFONT str$(world), 42
    PUTFONT str$(level), 64
    PUTFONT str$(score), 124
    PUTFONT str$(numcoins), 232
    PUTFONT str$(lives), 302
    
    if frcount = 0 then
        cframe = cframe + cfrdir
        if cframe = 3 or cframe = 0 then cfrdir = cfrdir * -1
    end if
    
end sub
''==============================================================================
sub ENDING ()

    color rgb(255, 128, 0), rgb(0, 0, 64)
    cls
    locate 20, 20 : print "   Thanks for playing Super Mario QB.   "
    locate 22, 20 : print "        Send your comments to           "
    locate 24, 20 : print "       spheresfantasy@yahoo.ca          "
    locate 26, 20 : print " and be sure to watch for new releases. "
    screencopy 1, 0
    sleep 5000
end sub
''==============================================================================
sub GETFONT (byval y as integer)
    
    dim as integer x
    
    for x = 0 to 9
        if font(x) = 0 then
            font(x) = imagecreate(16,16)
        end if
        get (x * 16, y * 2)-(x * 16 + 15, y * 2 + 15), font(x)
    next
    
end sub
''==============================================================================
sub GETGPH ()
    
    dim as integer x, y
    
    for y = 0 to 5
        for x = 0 to 19
            if sprites(y * 20 + x) = 0 then
                sprites(y * 20 + x) = imagecreate(32,32)
            end if
            get (x * 32, y * 32 + 16)-(x * 32 + 31, y * 32 + 47), sprites(y * 20 + x)
        next
    next
    
end sub
''==============================================================================
sub INTRO()
        
    LOADGPH ("logo")
    sleep 500
    LOADGPH ("title")
    sleep 1000

end sub
''==============================================================================
sub LEVELFINISH ()
    
    dim as integer chg, cdraw
    
    do
        for chg = 0 to 2
            line (chg + cdraw, chg + cdraw)-(319*2 - cdraw - chg, 199*2 - cdraw - chg), 0, B
        next
        
        screencopy 1, 0
        
        DELAY ()
        cdraw = cdraw + 3
    loop while cdraw < 200
    nextlevel = 1
    level = level + 1
    
end sub
''==============================================================================
sub LOADGPH (n as string)
    
    bload exepath + "/data/" + n + ".bmp"
    
end sub
''==============================================================================
sub LOADLEVEL ()
    
    dim as integer size, y, x, cnum, c, f
    
    f = freefile
    
    select case level
    case 1: open exepath + "/data/level1.txt" for binary as #f: size = 310
    case 2: open exepath + "/data/level2.txt" for binary as #f: size = 400
    case 3: open exepath + "/data/level3.txt" for binary as #f: size = 355
    end select
    
    for y = 0 to 11
        for x = 0 to size
            get #f, , map(x, y)
        next
    next
    
    close #f
    
    f = freefile
    
    xlen = x - 21
    
    select case level
    case 1: open exepath + "/data/coins1.txt" for binary as #f
    case 2: open exepath + "/data/coins2.txt" for binary as #f
    case 3: open exepath + "/data/coins3.txt" for binary as #f
    end select
    
    for cnum = 0 to 100
        for c = 0 to 2
            get #f, , coins(cnum, c)
        next
    next
    close #f
    
    
end sub


''==============================================================================
sub LOSELIFE ()
    dim as string pname(0 to 4)
    dim as integer pscore(0 to 4)
    dim as string s, k
    dim as integer i, ws
    dim as double tm
    
    while inkey <> "": wend

    lives = lives - 1
    if lives = 0 then
        
        color rgb(255, 128, 0), rgb(0, 0, 64)
        cls
        
        locate 20, 35 : print "GAME OVER"
        Play_dyn_Sound(soundgmo)
        screencopy 1, 0
        
        tm = timer()
        ws = LOADSCORES(pname(), pscore())
        
        do
            sleep 25
        loop while timer() - tm < 2
        
        cls
        locate 1, 1

        Box (20, 6, 58, 24, "Top Scores")

                locate 8,1
        for i = 0 to 4
            locate csrlin, 26
            print pname(i); space(15 - len(pname(i)));
            print using "########"; pscore(i)
            print
        next

        print
        
        if ws = -1 then
            locate csrlin, 26
            print "Your Score = ";score
            Stop_dyn_Sound()
            Play_dyn_Sound(soundlgh)
        else
            locate 28, 22
            print "             High Score!"
            locate 30, 22
            print "Please type your name and press Enter"
            locate 8 + ws*2, 26
            s = "" : k = ""
            screencopy 1, 0
            do
                k = inkey
                if( len( k ) = 1) then
                     
                    if k = chr$(13) then exit do
                                        if k = chr$(27) then exit do
                    if k = chr$(8) then
                        if len(s) > 1 then
                            s= mid(s, 1, len(s)-1)
                            locate csrlin,pos-1 : print "_ ";
                            locate csrlin,pos-2
                        elseif len(s) = 1 then
                            s = ""
                            locate csrlin,pos-1
                            print "_ ";
                            locate csrlin,pos-2
                        end if
                    else
                        if asc(k) > 31 and asc(k) < 129 then
                            print k;"_"; : locate csrlin,pos-1
                            s = s + k
                        end if
                    end if
                    screencopy 1, 0
                end if
                sleep 25
                k = ""
                if len(s) > 13 then exit do
            loop until k = chr$( 27 ) or k = chr$( 13 )
            pname(ws) = trim$(s)

            SAVESCORES(pname(), pscore())
            cls
            locate 2, 1
        end if
        while inkey <> "": wend
        
        locate 20,25 : print " Press any key for a new game "
        locate 22,25 : print "          Esc to quit         "
        screencopy 1, 0
                        
        sleep
        if(multikey(FB.SC_ESCAPE)) then fastexit = 0
        ' Wait 300 ms for key to be released
        sleep 300
        
        while inkey <> "": wend
        NEWGAME ()
    end if
    
end sub
''==============================================================================

sub NEWGAME ()
    world    = 1
    level    = 1
    numcoins = 0
    score    = 0
    lives    = 3
end sub

''==============================================================================
sub PUTFONT (fontnum as string, byval xstart as integer)
    
    dim as integer x
    
    for x = 1 to len(fontnum)
        put ((x - 1) * 16 + xstart*2, 0), font(val(mid(fontnum, x, 1))), pset
    next
    
end sub
''==============================================================================
sub SETGAME ()
    
    dim as integer enemynum, emax, y, x
    
    select case level
    case 1: restore level1        '' read in enemy positions
    case 2: restore level2
    case 3: restore level3
    end select
    for enemynum = 0 to 20
        read enemy(enemynum, 3)
        if enemy(enemynum, 3) = 999 then exit for
        read enemy(enemynum, 0)
        enemy(enemynum, 2) = 0
        read enemy(enemynum, 1)
        enemy(enemynum, 4) = enemy(enemynum, 0)
        enemy(enemynum, 5) = 1
    next
    emax = enemynum - 1
    LOADLEVEL ()
    LOADGPH ("graphics")
    dirn = 0
    move = 0
    die = 0
    n1 = 0
    nextlevel = 0
    xpl = 0
    ymove = 3
    onground = 1
    select case level
    case 1
        Stop_dyn_Sound()
        Play_dyn_Sound(soundm1)             '' Background music
        py = 117: ytile = 7: px = 100
        GETFONT 192
    case 2
        Stop_dyn_Sound()
        Play_dyn_Sound(soundm2)             '' Background music
        rrate = 1/45                        '' Faster
        py = 69: ytile = 4: px = 54: ymove = 3
        '' change color of menu to dark
        for y = 0 to 15
            for x = 0 to 639
                if point(x, y) = &hff0020ff then pset(x, y), &h001820
            next
        next
        GETFONT 184
    case 3
        Stop_dyn_Sound()
        Play_dyn_Sound(soundm3)             '' Background music
        rrate = 1/50                        '' Faster
        py = 101: ytile = 6: px = 100
        GETFONT 192
    end select
    
end sub



''==============================================================================
function LOADSCORES(pname() as string, pscore() as integer) as integer
    dim as integer i, j
    dim as integer namescore
    dim as string playername

    for i = 0 to 4
        pname(i) = "------"
        pscore(i) = 0
    next
    if (open (exepath + "/data/scores.txt", for input, as #1) = 0) then
        for i = 0 to 4
            input #1, playername, namescore
            pname(i) = playername
            pscore(i) = namescore
            if eof(1) then exit for
        next
        close #1
    end if

    for i = 0 to 4
        if pscore(i) < score then
            for j = 3 to i step -1
                pname(j+1) = pname(j)
                pscore(j+1) = pscore(j)
            next
            pname(i) = "_"
            pscore(i) = score
            '' return record to replace
            return i
        end if
    next
    
    '' Return -1 if score is lower than records
    return -1
end function
''==============================================================================
sub SAVESCORES(pname() as string, pscore() as integer)
    dim as integer i
    open exepath + "/data/scores.txt" for output as #1
    for i = 0 to 4
        write #1,pname(i),pscore(i)
    next
    close #1
end sub


''==============================================================================
sub Box (byval X1 as integer, byval Y1 as integer, byval X2 as integer, _
        byval Y2 as integer, Title as string)

    dim as integer Tb, Sid, Tlc, Trc, Blc, Brc, Tmp
    dim as string A

    Tb=205 : Sid=186 : Tlc=201 : Trc=187 : Blc=200 : Brc=188

    locate Y1,X1
    A = chr(Tlc) + string(X2-X1-1,chr(Tb)) + chr(Trc): print A;
    A = chr(Sid)+ string(X2-X1-1,chr(32)) + chr(sid)

    for Tmp = Y1 + 1 to Y2-1
        locate Tmp,X1,0 : print A;
    next

    locate Y2, X1
    A = chr(Blc) + string(X2-X1-1,chr(Tb)) + chr(Brc) : print A;
    locate Y1, X1+(X2-X1)\2-((len(Title)+4)*.5) : print "[ ";Title;" ]";
end sub

''==============================================================================
sub TIDYUP ()
    dim as integer x, y
    '' release font memory
    for x = 0 to 9
        if font(x) <> 0 then
            imagedestroy(font(x))
        end if
    next
    '' release sprite memory
    for x = 0 to 120
        if sprites(x) <> 0 then
            imagedestroy(sprites(x))
        end if
    next
    '' close down sound
    close_dyn_fmod()
end sub
