PacMan style game

Just BASIC Games
NJames
Posts: 43
Joined: Tue Jul 14, 2009 2:55 pm

PacMan style game

Post by NJames »

This is a demo of a PacMan style game. Very small.

Code: Select all

    nomainwin

    ' find how much whitespace the windows scheme is taking
    ' Anatoly's tip
    WindowWidth = 200
    WindowHeight = 200
    open "Ajusting..." for graphics_nf_nsb as #1
    #1, "home ; down ; posxy w h"
    w=200-2*w : h = 200-2*h
    close #1

    width = 10
    height = 10
    DIM board(width, height)
    DIM board$(height)

    wall$ = "X"
    dot$ = "."
    power$ = "O"
    wall = 0
    dot = 1
    power = 2

    board$(1) = "XXXXXXXXXX"
    board$(2) = "XO...X..OX"
    board$(3) = "X.XX.X.X.X"
    board$(4) = "X.X....X.X"
    board$(5) = "X.X.XX.X.X"
    board$(6) = "X........X"
    board$(7) = "X.X.X..X.X"
    board$(8) = "X.X.X.XX.X"
    board$(9) = "XO......OX"
    board$(10)= "XXXXXXXXXX"

    WindowWidth  = 400+w
    WindowHeight = 400+h
    UpperLeftX   = (DisplayWidth-WindowWidth)/2
    UpperLeftY   = (DisplayHeight-WindowHeight)/2
    open "ChompNChase" for graphics_nf_nsb as #g
    #g "trapclose Quit"
    #g "setfocus"
    #g "down"

    numOfDots = 0
    numOfPower = 0
    for x = 1 to width
        for y = 1 to height
            if mid$(board$(y), x, 1) = dot$ then numOfDots = numOfDots + 1
            if mid$(board$(y), x, 1) = power$ then numOfPower = numOfPower + 1
        next y
    next x

    dim power(numOfPower)

    call drawDots numOfDots
    call drawPower numOfPower
    call drawChomper
    call drawSpook

    dotToPlace = 0
    powerToPlace = 0
    for y = 1 to height
        for x = 1 to width
            t$ = mid$(board$(y), x, 1)
            #g "place " ; (x - 1) * 40 ; " " ; (y - 1) * 40
            select case t$
            case wall$
                t = wall
                #g "color black ; backcolor black"
                #g "boxfilled " ; x * 40 ; " " ; y * 40
            case dot$
                t = dot
                #g "color white ; backcolor white"
                #g "boxfilled " ; x * 40 ; " " ; y * 40
                dotToPlace = dotToPlace + 1
                #g "spritexy dot" ; dotToPlace ; " " ; (x - 1) * 40 ; " " ; (y - 1) * 40
            case power$
                t = power
                #g "color lightgray ; backcolor lightgray"
                #g "boxfilled " ; x * 40 ; " " ; y * 40
                powerToPlace = powerToPlace + 1
                #g "spritexy power" ; powerToPlace ; " " ; (x - 1) * 40 ; " " ; (y - 1) * 40
            end select
            board(x, y) = t
        next x
    next y
    #g "getbmp board 0 0 " ; width * 40 ; " " ; height * 40
    #g "background board"

    chompX = 120 : chompY = 240 : chompD = 1
    spookX = 320 : spookY = 360 : spookD = 1
    speed = 8
    spookSpeed = 4
    maxScared = 100
    gameSpeed = 60

    #g "when characterInput [key]"
    timer gameSpeed, [tick]
    wait

    [tick]
        SCAN
        '---------------
        'CHOMPER
        '---------------
        if chompX mod 40 = 0 and chompY mod 40 = 0 then
            select case newD
            case 1
                posX = int(chompX / 40) : posY = int(chompY / 40)
                if board(posX + 1, posY) <> wall then
                    chompD = newD
                else
                    newD = chompD
                end if
            case 2
                posX = int(chompX / 40) : posY = int(chompY / 40)
                if board(posX, posY + 1) <> wall then
                    chompD = newD
                else
                    newD = chompD
                end if
            case 3
                posX = int((chompX+39) / 40) : posY = int(chompY / 40)
                if board(posX - 1, posY) <> wall then
                    chompD = newD
                else
                    newD = chompD
                end if
            case 4
                posX = int(chompX / 40) : posY = int((chompY+39) / 40)
                if board(posX, posY - 1) <> wall then
                    chompD = newD
                else
                    newD = chompD
                end if
            end select
        end if
        select case chompD
        case 0
            #g "cyclesprite ChomperH 0"
            #g "cyclesprite ChomperV 0"
        case 1
            posX = int(chompX / 40) : posY = int(chompY / 40)
            if board(posX + 1, posY) <> wall then
                chompX = chompX + speed
            else
                chompD = 0
            end if
            #g "spritevisible ChomperV off"
            #g "spritevisible ChomperH on"
            #g "cyclesprite ChomperH 1"
            #g "spriteorient ChomperH mirror"
        case 2
            posX = int(chompX / 40) : posY = int(chompY / 40)
            if board(posX, posY + 1) <> wall then
                chompY = chompY + speed
            else
                chompD = 0
            end if
            #g "spritevisible ChomperH off"
            #g "spritevisible ChomperV on"
            #g "cyclesprite ChomperV 1"
            #g "spriteorient ChomperV normal"
        case 3
            posX = int((chompX+39) / 40) : posY = int(chompY / 40)
            if board(posX - 1, posY) <> wall then
                chompX = chompX - speed
            else
                chompD = 0
            end if
            #g "spritevisible ChomperV off"
            #g "spritevisible ChomperH on"
            #g "cyclesprite ChomperH 1"
            #g "spriteorient ChomperH normal"
        case 4
            posX = int(chompX / 40) : posY = int((chompY+39) / 40)
            if board(posX, posY - 1) <> wall then
                chompY = chompY - speed
            else
                chompD = 0
            end if
            #g "spritevisible ChomperH off"
            #g "spritevisible ChomperV on"
            #g "cyclesprite ChomperV 1"
            #g "spriteorient ChomperV flip"
        end select
        #g "spritexy ChomperH " ; chompX - 40 ; " " ; chompY - 40
        #g "spritexy ChomperV " ; chompX - 40 ; " " ; chompY - 40
        '---------------
        'SPOOK
        '---------------
        if (spookX mod 40 = 0 and spookY mod 40 = 0) then
            posX = int(spookX / 40) : posY = int(spookY / 40)
            'find legal directions
            choice$ = ""
            if board(posX + 1, posY) <> wall then choice$ = choice$ ; "1"
            if board(posX, posY + 1) <> wall then choice$ = choice$ ; "2"
            if board(posX - 1, posY) <> wall then choice$ = choice$ ; "3"
            if board(posX, posY - 1) <> wall then choice$ = choice$ ; "4"
            'do not reverse course
            if len(choice$) > 1 and spookD = 1 then choice$ = remove$(choice$, "3")
            if len(choice$) > 1 and spookD = 2 then choice$ = remove$(choice$, "4")
            if len(choice$) > 1 and spookD = 3 then choice$ = remove$(choice$, "1")
            if len(choice$) > 1 and spookD = 4 then choice$ = remove$(choice$, "2")
            'where is the chomper?
            distX = spookX - chompX
            distY = spookY - chompY
            'don't turn wrong direction
            if scared then
                distX = 0 - distX
                distY = 0 - distY
            end if
            if rnd(0) < .5 then
                if distX > 0 and instr(choice$, "3") then choice$ = "3"
                if distX < 0 and instr(choice$, "1") then choice$ = "1"
                if distY > 0 and instr(choice$, "4") then choice$ = "4"
                if distY < 0 and instr(choice$, "2") then choice$ = "2"
                if len(choice$) > 1 and distX > 0 then choice$ = remove$(choice$, "1")
                if len(choice$) > 1 and distX < 0 then choice$ = remove$(choice$, "3")
                if len(choice$) > 1 and distY > 0 then choice$ = remove$(choice$, "2")
                if len(choice$) > 1 and distY < 0 then choice$ = remove$(choice$, "4")
            else
                if distY > 0 and instr(choice$, "4") then choice$ = "4"
                if distY < 0 and instr(choice$, "2") then choice$ = "2"
                if distX > 0 and instr(choice$, "3") then choice$ = "3"
                if distX < 0 and instr(choice$, "1") then choice$ = "1"
                if len(choice$) > 1 and distY > 0 then choice$ = remove$(choice$, "2")
                if len(choice$) > 1 and distY < 0 then choice$ = remove$(choice$, "4")
                if len(choice$) > 1 and distX > 0 then choice$ = remove$(choice$, "1")
                if len(choice$) > 1 and distX < 0 then choice$ = remove$(choice$, "3")
            end if
            'choose direction
            if len(choice$) = 1 then
                spookD = val(choice$)
            else
                choices = len(choice$)
                choose = int(rnd(0) * choices) + 1
                spookD = val(mid$(choice$, choose, 1))
            end if
        end if
        select case spookD
        case 1
            posX = int(spookX / 40) : posY = int(spookY / 40)
            if board(posX + 1, posY) <> wall then spookX = spookX + spookSpeed
        case 2
            posX = int(spookX / 40) : posY = int(spookY / 40)
            if board(posX, posY + 1) <> wall then spookY = spookY + spookSpeed
        case 3
            posX = int((spookX+39) / 40) : posY = int(spookY / 40)
            if board(posX - 1, posY) <> wall then spookX = spookX - spookSpeed
        case 4
            posX = int(spookX / 40) : posY = int((spookY+39) / 40)
            if board(posX, posY - 1) <> wall then spookY = spookY - spookSpeed
        end select
        #g "spritexy Spook " ; spookX - 40 ; " " ; spookY - 40
        if scared then
            scared = scared - 1
            if not(scared) then #g "spriteimage Spook Spook1"
        end if
        '---------------
        'DOTS
        '---------------
        for d = 1 to numOfDots
            #g "spritexy? dot" ; d ; " dotX dotY"
            if chompX - 40 = dotX and chompY - 40 = dotY then #g "spritevisible dot" ; d ; " off"
        next d
        for p = 1 to numOfPower
            #g "spritexy? power" ; p ; " powerX powerY"
            if chompX - 40 = powerX and chompY - 40 = powerY then
                #g "spritevisible power" ; p ; " off"
                if power(p) = 0 then
                    scared = maxScared
                    power(p) = 1 'eaten
                    #g "spriteimage Spook Spook1b"
                end if
            end if
        next p
        '--------------
        'COLLISIONS
        '--------------
        #g "spritecollides Spook collide$"
        if instr(collide$, "Chomper") then
            if scared then
                #g "spriteimage Spook SpookEyes"
                scared = 100
            else
                timer 0
                notice "caught!"
            end if
        end if
        #g "drawsprites"
    wait

    [key]
        select case asc(right$(Inkey$, 1))
        case _VK_RIGHT
            newD = 1
        case _VK_DOWN
            newD = 2
        case _VK_LEFT
            newD = 3
        case _VK_UP
            newD = 4
        case _VK_SPACE
            notice spookX ; ", " ; spookY ; " dir=" ; spookD
        end select
    wait



    sub Quit handle$
        timer 0
        close #handle$
        end
    end sub

    sub pause length
        timer length, [unpause]
        wait
        [unpause]
        timer 0
    end sub

    sub drawChomper
        x=20
        y=20
        r=16
        for slice = 0 to 4
            #g "cls"
            #g "color black ; backcolor black"
            #g "place ";x;" ";y
            #g "circlefilled ";r
            #g "color white ; backcolor white"
            #g "piefilled 40 40 " ; (180 - slice * 12) ; " " ; 1 + slice * 24
            '---------------------------------
            #g "color black ; backcolor black"
            #g "place 0 40"
            #g "boxfilled 40 80"
            #g "place ";x;" ";y*3
            #g "color yellow ; backcolor yellow ; circlefilled ";r
            #g "color black ; backcolor black"
            #g "piefilled 40 40 " ; (180 - slice * 12) ; " " ; 1 + slice * 24
            '---------------------------------
            #g "getbmp chompH" ; slice ; " 0 0 40 80"
        next slice
        #g "addsprite ChomperH chompH0 chompH0 chompH1 chompH2 chompH3 chompH4 chompH3 chompH2 chompH1"
        #g "cyclesprite ChomperH 1"
        for slice = 0 to 4
            #g "cls"
            #g "color black ; backcolor black"
            #g "place ";x;" ";y
            #g "circlefilled ";r
            #g "color white ; backcolor white"
            #g "piefilled 40 40 " ; (90 - slice * 12) ; " " ; 1 + slice * 24
            '---------------------------------
            #g "color black ; backcolor black"
            #g "place 0 40"
            #g "boxfilled 40 80"
            #g "place ";x;" ";y*3
            #g "color yellow ; backcolor yellow ; circlefilled ";r
            #g "color black ; backcolor black"
            #g "piefilled 40 40 " ; (90 - slice * 12) ; " " ; 1 + slice * 24
            '---------------------------------
            #g "getbmp chompV" ; slice ; " 0 0 40 80"
        next slice
        #g "addsprite ChomperV chompV0 chompV0 chompV1 chompV2 chompV3 chompV4 chompV3 chompV2 chompV1"
        #g "cyclesprite ChomperV 1"
    end sub

    sub drawSpook
        x=20
        y=20
        r=19
        #g "cls"
        #g "place ";x;" ";y
        #g "color black ; backcolor black"
        #g "piefilled 30 40 0 360"
        #g "color white ; backcolor white"
        #g "place ";x-6;" ";y + 20
        #g "piefilled 16 20 157 202"
        #g "place ";x+6;" ";y + 20
        #g "piefilled 16 20 157 202"
        #g "color black ; backcolor black"
        #g "place 0 40"
        #g "boxfilled 40 80"
        'image
        #g "place ";x;" ";y*3
        #g "color green ; backcolor green"
        #g "piefilled 30 40 0 360"
        #g "place ";x;" ";y*3 + 20
        #g "color black ; backcolor black"
        #g "place ";x-6;" ";y*3 + 20
        #g "piefilled 16 20 157 202"
        #g "place ";x+6;" ";y*3 + 20
        #g "piefilled 16 20 157 202"
        #g "place ";x-5;" ";y*3-5
        #g "backcolor white ; circlefilled 3"
        #g "place ";x+5;" ";y*3-5
        #g "backcolor white ; circlefilled 3"
        #g "getbmp Spook1 0 0 40 80"
        'scared image
        #g "place ";x;" ";y*3
        #g "color blue ; backcolor blue"
        #g "piefilled 30 40 0 360"
        #g "place ";x;" ";y*3 + 20
        #g "color black ; backcolor black"
        #g "place ";x-6;" ";y*3 + 20
        #g "piefilled 16 20 157 202"
        #g "place ";x+6;" ";y*3 + 20
        #g "piefilled 16 20 157 202"
        #g "place ";x-5;" ";y*3-5
        #g "backcolor white ; circlefilled 3"
        #g "place ";x+5;" ";y*3-5
        #g "backcolor white ; circlefilled 3"
        #g "getbmp Spook1b 0 0 40 80"
        'eyes
        #g "cls"
        #g "place ";x;" ";y
        #g "color black ; backcolor black"
        #g "place ";x-5;" ";y-5
        #g "circlefilled 3"
        #g "place ";x+5;" ";y-5
        #g "circlefilled 3"
        #g "color black ; backcolor black"
        #g "place 0 40"
        #g "boxfilled 40 80"
        #g "color white ; backcolor black"
        #g "place ";x-5;" ";y*3-5
        #g "circlefilled 3"
        #g "place ";x+5;" ";y*3-5
        #g "getbmp SpookEyes 0 0 40 80"
        #g "addsprite Spook Spook1 Spook1b SpookEyes"
    end sub

    sub drawDots dots
        #g "cls"
        #g "color black ; backcolor black"
        #g "place 20 20"
        #g "circlefilled 4"
        #g "place 0 40"
        #g "color black ; backcolor black"
        #g "boxfilled 40 80"
        #g "color black ; backcolor yellow"
        #g "place 20 60"
        #g "circlefilled 4"
        #g "getbmp dot 0 0 40 80"
        for d = 1 to dots
            #g "addsprite dot" ; d ; " dot"
        next
    end sub

    sub drawPower powers
        #g "cls"
        #g "color black ; backcolor black"
        #g "place 20 20"
        #g "circlefilled 8"
        #g "place 0 40"
        #g "color black ; backcolor black"
        #g "boxfilled 40 80"
        #g "color black ; backcolor white"
        #g "place 20 60"
        #g "circlefilled 8"
        #g "getbmp power 0 0 40 80"
        for p = 1 to powers
            #g "addsprite power" ; p ; " power"
        next
    end sub

    'drops first occurence of a sub-string
    function remove$(string$, drop$)
        if instr(string$, drop$) then remove$=mid$(string$,1,instr(string$,drop$)-1);mid$(string$,instr(string$,drop$)+len(drop$)) else remove$=string$
    end function