
'Liberty Hockey
'aka "Pong"
'by David Drake


'Play against the computer!
'A demonstration of a game creation technique
'The computer determines where the puck will end up and moves its piece there

'Released as open source, so don't blame me  ;-)

[setupWindow]
    nomainwin

    notice "Liberty Hockey!"+chr$(13)+_
        "Don't let the puck slip past you!"+chr$(13)+_
        "You are the left paddle. Move paddle up and"+chr$(13)+_
        "down with the arrow keys. Good luck!"

    gosub [startMusic]

    UpperLeftX = DisplayWidth/2-640/2
    UpperLeftY = DisplayHeight/2-480/2
    WindowWidth = 640
    WindowHeight = 480
    graphicbox #main.gb 0,0,640,480
    open "Hockey" for window as #main

    #main.gb "down;fill white;color lightgray; size 6; place 4 4 ; box 629 446;line 0 230 640 230;line 319 0 319 480; flush"
    #main.gb "color darkcyan;place 280 215;font times_new_roman 36;\L"
    #main.gb "place 330 280;font times_new_roman 36;\B"
    #main "trapclose [quit]"

    #main.gb "getbmp bg 1 1 640 480"
    loadbmp "hitter","bmp\hitter.bmp"
    loadbmp "puck","bmp\puck.bmp"
    #main.gb "addsprite hlt hitter"
    #main.gb "addsprite hrt hitter"
    #main.gb "addsprite puck puck"
    #main.gb "background bg"
    #main.gb "drawsprites"
    #main.gb "spritexy hrt 610 200"
    #main.gb "spritexy hlt 20 200"
    #main.gb "spritemovexy puck 8 12"
    #main.gb "setfocus"
    #main.gb "when characterInput [keyPressed]"
    starttime = time$("milliseconds")
    puckvx = 14
    puckvy = 12
    level = 10
    desty = 170
    delay = 80
    timer 80, [loop]
    wait

[keyPressed]
    c$ = lower$(Inkey$)
    if asc(right$(c$,1))=38 then hlty = hlty - 10:hltDy = -1
    if asc(right$(c$,1))=40 then hlty = hlty + 10:hltDy = 1
    if hlty > 550 then hlty = 550
    if hlty < 0 then hlty = 0
    if c$=chr$(27) then goto [quit]
    wait

[loop]
    if time$("milliseconds") > endMusicTime then
        gosub [stopMusic]
        gosub [startMusic]
    end if
    #main.gb "spritexy? hrt hrtx hrty"
    #main.gb "spritexy? puck puckx pucky"
    if hrty < desty-25 and puckvx > 0 then hrty = hrty + 10:hrtDy = 1:if hrty> 530 then hrty = 530
    if hrty > desty-25 and puckvx > 0 then hrty = hrty - 10:hrtDy = -1:if hrty<0 then hrty = 0
    if puckx < 1   and puckvx < 0 then [pointComputer]
    if puckx > 590 and puckvx > 0 then [pointHuman]
    if pucky < 1   and puckvy < 0 then puckvy = 0 - puckvy:puckFlag = 1:playwave "media\bump.wav",async
    if pucky > 400 and puckvy > 0 then puckvy = 0 - puckvy:puckFlag = 1:playwave "media\bump.wav",async
    #main.gb "spritecollides puck col$"

    if instr(col$,"hlt") > 0 and puckvx < 0 then
        puckvx = 0 - puckvx
        puckvy = puckvy + hltDy
        if rnd(1) < .1 then
            if puckvy < 0 then puckvy = puckvy - 1
            if puckvy > 0 then puckvy = puckvy + 1
        end if
        puckFlag = 1
        playwave "media\beep.wav",async
        gosub [guessLocation]
    end if

    if instr(col$,"hrt") > 0 and puckvx > 0 then
        puckvx = 0 - puckvx
        puckvy = puckvy + hrtDy
        if rnd(1) < .1 then
            if puckvy < 0 then puckvy = puckvy - 1
            if puckvy > 0 then puckvy = puckvy + 1
        end if
        puckFlag = 1
        playwave "media\beep.wav",async
    end if

    if puckFlag = 1 then
        puckFlag = 0
        #main.gb "spritemovexy puck ";puckvx;" ";puckvy;
    end if
    #main.gb "spritexy hrt 605 ";hrty
    #main.gb "spritexy hlt  20 ";hlty
    #main.gb "drawsprites"
    hltDy = 0
    hrtDy = 0
    elapsed = (time$("milliseconds")-starttime)/1000
    if elapsed > level then
        level = elapsed + 5
        delay = delay-4
        if delay < 50 then
            delay = 50
            if puckxv < 0 then puckxv = puckxv - 1
            if puckxv > 0 then puckxv = puckxv + 1
        end if
        timer delay, [loop]
    end if
    wait

[guessLocation]
    'This speeds up the loop every 5 seconds
    #main.gb "spritexy? puck puckx pucky"
    m = puckvy/puckvx
    desty = m*(580-puckx)+pucky
    print desty;" ";
    if desty < 0 then desty = int(abs(desty))
    if desty > 400 then desty = int(800 - desty)
    desty = abs(desty)
    return

[pointComputer]
    timer 0
    notice "Computer wins! You lasted ";elapsed;" seconds."
    goto [quit]

[pointHuman]
    timer 0
    notice "You win! Way to go."
    goto [quit]

[startMusic]
    endMusicTime = time$("milliseconds")+60000+42000
    a$="open media\theme.mid type sequencer alias fictoplay"
    calldll #winmm,"mciSendStringA",a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    a$="play fictoplay"
    calldll #winmm,"mciSendStringA",_
        a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    return

[stopMusic]
    a$="close fictoplay"
    calldll #winmm,"mciSendStringA",_
        a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    return

[quit]
    gosub [stopMusic]
    close #main
    end



