Liberty BASIC Community Forum
« Sample Program .bas code »

Welcome Guest. Please Login or Register.
Nov 24th, 2017, 10:49am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 ... 5 6 7  Notify Send Topic Print
 locked  Author  Topic: Sample Program .bas code  (Read 1441 times)
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program togglebutton.bas
« Reply #90 on: Aug 19th, 2017, 06:04am »

togglebutton.bas


unchanged


Code:
    'toggleButton.bas - How to make a toggle button
    nomainwin

    WindowWidth = 200'180
    WindowHeight = 200'96
    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)
    graphicbox #demo.colors, 2, 2, 62, 62

    'Stylebit To Make The "Toggle" Button
    stylebits #demo.toggle, _BS_PUSHLIKE,0,0,0
    checkbox #demo.toggle, "Toggle Button", [toggleon], [toggleoff], 67, 1, 105, 65
    open "Toggle Button Demo" for dialog as #demo
    #demo.colors "down;backcolor white"
    #demo "font ms_sans_serif 10"
    #demo "trapclose [quit.demo]"

[toggleoff]
    #demo.colors "delsegment seg;fill white;set 15 35;backcolor white;| OFF"
    #demo.colors "flush seg"
    wait

[toggleon]
    #demo.colors "delsegment seg;fill red;set 15 35;backcolor red;| ON"
    #demo.colors "flush seg"
    wait

[quit.demo]
    close #demo
    end
 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program tstprint.bas
« Reply #91 on: Aug 19th, 2017, 06:09am »

tstprint.bas


unchanged


Code:
    'test printing
    filedialog "Select a BASIC program", "*.bas", fname$
    printerdialog
    if fname$ = "" then end
    open fname$ for input as #1
    while eof(#1) = 0
        line input #1, text$
        lprint text$
    wend
    close #1
    dump
    end
 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program TTT.bas
« Reply #92 on: Aug 19th, 2017, 06:10am »

TTT.bas

print #brd, to #brd

Code:
    'Tic Tac Toe
    'Requires Liberty BASIC v2.0 or better
    'Copyright 1993,2001 Shoptalk Systems
    'All Rights Reserved

    nomainwin

    loadbmp "title", "bmp\titlettt.bmp"
    loadbmp "square", "bmp\squarttt.bmp"

    WindowWidth = 314
    WindowHeight = 380
    button #brd, " T ", [1], UL, 10, 60
    button #brd, " I ", [2], UL, 110, 60
    button #brd, " C ", [3], UL, 210, 60
    button #brd, " T ", [4], UL, 10, 160
    button #brd, " A ", [5], UL, 110, 160
    button #brd, " C ", [6], UL, 210, 160
    button #brd, " T ", [7], UL, 10, 260
    button #brd, " O ", [8], UL, 110, 260
    button #brd, " E ", [9], UL, 210, 260
    button #brd, "Pass Move", [skipMove], UL, 210, 10
    open "Tic Tac Toe Playing Board" for graphics_nsb as #brd
    #brd "trapclose [quit]";
    #brd "size 3 ; down";

[start]    'start game (or restart)

    gosub [drawBoard]
    for x = 1 to 9 : board$(x) = " " : next x
    gosub [modelBoard]
    turn = 0
    true = 1
    false = 0
    pass = false
    moveCounter = 0

[playLoop]


    input position$

  goto [playLoop]


[move]

    ' X moves
    if board$(val(position$)) <> " " then notice "Illegal Move!" : goto [playLoop]
    moveCounter = moveCounter + 1
    pass = false
    board$(val(position$)) = "X"
    gosub [drawX]
    gosub [modelBoard]
    gosub [checkForWinOrDraw]
    position$ = ""
    goto [oMoves]

[skipMove]

    pass = true

[oMoves]

    ' O moves
    gosub [computeMove]
    board$(val(position$)) = "O"
    gosub [drawO]
    gosub [modelBoard]
    gosub [checkForWinOrDraw]

  goto [playLoop]


[1]
    position$ = "1" : goto [move]
[2]
    position$ = "2" : goto [move]
[3]
    position$ = "3" : goto [move]
[4]
    position$ = "4" : goto [move]
[5]
    position$ = "5" : goto [move]
[6]
    position$ = "6" : goto [move]
[7]
    position$ = "7" : goto [move]
[8]
    position$ = "8" : goto [move]
[9]
    position$ = "9" : goto [move]


[drawBoard]

    #brd "cls ; fill lightgray ; drawbmp title 7 2";
    for x = 4 to 204 step 100
    for y = 54 to 254 step 100
        #brd "drawbmp square "; x; " "; y ;
    next y
    next x
    #brd "flush";
  return

[drawX]

    gosub [xlatePosition]
    #brd "size 6 ; color darkred";
    #brd "line "; squareX-11; " "; squareY-16; " "; squareX+19; " "; squareY+24
    #brd "line "; squareX+19; " "; squareY-16; " "; squareX-11; " "; squareY+24
    #brd "color red";
    #brd "line "; squareX-15; " "; squareY-20; " "; squareX+15; " "; squareY+20
    #brd "line "; squareX+15; " "; squareY-20; " "; squareX-15; " "; squareY+20
    #brd "flush";

  return


[drawO]

    gosub [xlatePosition]
    #brd "size 7 ; color darkblue";
    #brd "place "; squareX+5; " "; squareY+5
    #brd "circle 20 ; color blue";
    #brd "place "; squareX; " "; squareY
    #brd "circle 20 ; flush";

  return


[xlatePosition]

    squareX = 55
    if instr("258", position$) > 0 then squareX = 155
    if instr("369", position$) > 0 then squareX = 255

    squareY = int((val(position$)+2)/3)*100+5

  return


[modelBoard]

    row1$ = board$(1)+board$(2)+board$(3)
    row2$ = board$(4)+board$(5)+board$(6)
    row3$ = board$(7)+board$(8)+board$(9)

    col1$ = board$(1)+board$(4)+board$(7)
    col2$ = board$(2)+board$(5)+board$(8)
    col3$ = board$(3)+board$(6)+board$(9)

    diag1$ = board$(1)+board$(5)+board$(9)
    diag2$ = board$(7)+board$(5)+board$(3)

  return



[computeMove]


    'check for instant win!

    if row1$ = "OO " then position$ = "3"
    if row1$ = " OO" then position$ = "1"
    if row1$ = "O O" then position$ = "2"
    if row2$ = "OO " then position$ = "6"
    if row2$ = " OO" then position$ = "4"
    if row2$ = "O O" then position$ = "5"
    if row3$ = "OO " then position$ = "9"
    if row3$ = " OO" then position$ = "7"
    if row3$ = "O O" then position$ = "8"

    if col1$ = "OO " then position$ = "7"
    if col1$ = " OO" then position$ = "1"
    if col1$ = "O O" then position$ = "4"
    if col2$ = "OO " then position$ = "8"
    if col2$ = " OO" then position$ = "2"
    if col2$ = "O O" then position$ = "5"
    if col3$ = "OO " then position$ = "9"
    if col3$ = " OO" then position$ = "3"
    if col3$ = "O O" then position$ = "6"

    if diag1$ = "OO " then position$ = "9"
    if diag1$ = " OO" then position$ = "1"
    if diag1$ = "O O" then position$ = "5"
    if diag2$ = "OO " then position$ = "3"
    if diag2$ = " OO" then position$ = "7"
    if diag2$ = "O O" then position$ = "5"

    'make the purely defensive moves

    if row1$ = "XX " then position$ = "3"
    if row1$ = " XX" then position$ = "1"
    if row1$ = "X X" then position$ = "2"
    if row2$ = "XX " then position$ = "6"
    if row2$ = " XX" then position$ = "4"
    if row2$ = "X X" then position$ = "5"
    if row3$ = "XX " then position$ = "9"
    if row3$ = " XX" then position$ = "7"
    if row3$ = "X X" then position$ = "8"

    if col1$ = "XX " then position$ = "7"
    if col1$ = " XX" then position$ = "1"
    if col1$ = "X X" then position$ = "4"
    if col2$ = "XX " then position$ = "8"
    if col2$ = " XX" then position$ = "2"
    if col2$ = "X X" then position$ = "5"
    if col3$ = "XX " then position$ = "9"
    if col3$ = " XX" then position$ = "3"
    if col3$ = "X X" then position$ = "6"

    if diag1$ = "XX " then position$ = "9"
    if diag1$ = " XX" then position$ = "1"
    if diag1$ = "X X" then position$ = "5"
    if diag2$ = "XX " then position$ = "3"
    if diag2$ = " XX" then position$ = "7"
    if diag2$ = "X X" then position$ = "5"
    if position$ <> "" then return


[attack]

    ' now make the offensive moves

    moves$ = ""

    if instr(diag1$,"X") = 0 then
        moves$ = moves$ + mid$("159", instr(diag1$, " "), 1)
     else
        if instr(diag2$,"X") = 0 then
            moves$ = moves$ + mid$("357", instr(diag2$, " "), 1)
          else
            ' now make random moves
            all$ = row1$ + row2$ + row3$
            for i = 1 to 9
                if mid$(all$, i, 1) = " " then moves$ = moves$ + chr$(48+i)
            next i
            if moves$ = "" then position$ = "" : return
        end if
    end if
    i = int(rnd(1)*len(moves$))+1
    position$ = mid$(moves$, i, 1)
    return

[checkForWinOrDraw]

    ' check for win
    letter$ = "XXX"
    for i = 1 to 2
        if instr(row1$+","+row2$+","+row3$, letter$) then [win]
        if instr(col1$+","+col2$+","+col3$, letter$) then [win]
        if instr(diag1$+","+diag2$, letter$) then [win]
        letter$ = "OOO"
    next i

    'check to see if the board is full
    if instr(row1$+row2$+row3$, " ") then return

    'the game is a draw

    notice "Draw!"
    goto [askPlayAgain]


[win]

    if letter$ = "OOO" then notice "I WIN!  HA HA HA!"
    if letter$ = "XXX" then notice "You win!!! Bummer!"

[askPlayAgain]

    confirm "Play again?"; play$
    if play$ = "yes" then [start]

[quit]

    close #brd
    end



 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program turtle2.bas
« Reply #93 on: Aug 19th, 2017, 06:13am »

turtle2.bas

A tidy, trapclose etc

Code:
    ' This is a turtle graphics demo
    nomainwin

    dim color$(3)

    color$(0) = "red"
    color$(1) = "green"
    color$(2) = "yellow"
    color$(3) = "blue"


    open "Turtle Graphics Spiral" for graphics_fs as #turtleOut
    #turtleOut "trapclose [quit]"


    #turtleOut "home"
    #turtleOut "down"
    #turtleOut "north"
    #turtleOut "fill black"
    #turtleOut "size 8"

    for x = 1 to 200
        #turtleOut "color "; color$(x - int(x/4) * 4)
        #turtleOut "turn 122"
        #turtleOut "go "; x*2
    next x

    #turtleOut "flush"
    wait

    [quit]
    close #turtleOut
    end

 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program welcome.bas
« Reply #94 on: Aug 19th, 2017, 06:15am »

welcome.bas


unchanged


Code:
'Thanks for trying Liberty BASIC v4.5.1, the easiest programming
'language around since 1992!  Liberty BASIC has been featured by McGraw
'Hill and Ziff Davis, and also in a tutorial series in PC Plus magazine!
'See our Run BASIC system for the easiest web programming around!
'  http://www.runbasic.com
'
'Thousands of people use Liberty BASIC every day to write useful
'software, create software for sale, create games, and teach and learn
'programming.
'
'What to do first? Here are some ideas:
' - Look at some of the example programs (Click File+Open).
' - Check out the World of Liberty BASIC (see the toolbar above).
' - Check out the tutorial (under the Help menu).
' - GET A DEAL!  Send email to special@libertybasic.com.  Tell us where
'   you heard about Liberty BASIC to save $$$ on your purchase.
' - Read the What's New section in the help file.
' - Play a game (mayhem.bas, hanoi.bas, ttt.bas).
' - Play with the Freeform GUI editor (under the Run menu).
'
'Here are some of Liberty BASIC's exciting features:
' - A real BASIC for Windows!
' - Great price!  Free to try, and licenses start at just $59.95
' - Write modular code with subprograms and functions
' - Simple and easy to use GUI capabilities
' - Call operating systems APIs and use 3rd party DLLs!
' - Create games with sprite animation, sound, and music!
' - Great internet community helps learn & provide support!
' - Support for serial communications!
' - Create standalone programs with purchase of GOLD license!
'
'Enjoy!
'
'Note: To turn off this welcome screen, just click on the
'Setup menu, select Preferences.  Then just turn off "Open
'this file on startup" or change the filename to some other
'file as preferred.

  nomainwin
  q$ = chr$(34)
  run StartupDir$+"liberty.exe "+q$+DefaultDir$+"\lb4tutorial.lsn"+q$
  end

 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program .bas code
« Reply #95 on: Aug 19th, 2017, 06:17am »

Some demos still to revise API for one. Will zip all of these to a file for ease of access. May take a day or so yet.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Sample Program .bas code
« Reply #96 on: Aug 20th, 2017, 04:44am »

Ok, this is a link to a .zip of all of the original example files. They have been dusted down, tweaked and checked out as fit for Win10. Some have been renamed to make it more obvious what they offer.

If you download this .zip it will unzip all of the files to a folder called examples. They of course will need access to the bmp, media and sprite images. If you are going to check them out it would be best to copy the media,bmp and sprite subfolders into the unzipped directory. This will save disturbing the original set in %appdata%.

Freeform is not updated it is the same as distributed with v4.5.1

http://gamebin.webs.com/Liberty/Examples.zip

If Carl uses any of these in future releases you will no doubt find them in the usual place.
User IP Logged

Pages: 1 ... 5 6 7  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls