Beginners Programming Series VII

Appendix and Glossary

© 2004, Brad Moore

author contact:

http://www.freewebz.com/lb-connection

Home

Tip Corner: Using

Game Programming

Round Buttons

Rotating Objects

LBW: Book Marks

Slide Puzzle

Speech DLL

Addresses

Beginners Programming

Newsletter help

Index


Appendix A

Show Dice - The complete program:


'** Show Dice
'by Brad Moore - in the public domain

NOMAINWIN
WindowWidth = 740
WindowHeight = 160
UpperLeftX = 40
UpperLeftY = 40

Open "Show Dice" for Graphics_nsb as #main

    #main "trapclose [quit]"
    #main "down; fill white; up; flush"
    #main "font ms_sans_serif 10"

    'show dice outlines
    #main "goto 20 20; down; box 100 100; up"
    #main "goto 140 20; down; box 220 100; up"
    #main "goto 260 20; down; box 340 100; up"
    #main "goto 380 20; down; box 460 100; up"
    #main "goto 500 20; down; box 580 100; up"
    #main "goto 620 20; down; box 700 100; up"

    'set the back color
    #main "backcolor black"

    'draw one - origin 20,20
    #main "up; goto 60 60"
    #main "down; circlefilled 5"

    'draw two - origin 140,20
    #main "up; goto 155 35"
    #main "down; circlefilled 5"
    #main "up; goto 205 85"
    #main "down; circlefilled 5"

    'draw three - origin 260,20
    #main "up; goto 300 60"
    #main "down; circlefilled 5"
    #main "up; goto 275 35"
    #main "down; circlefilled 5"
    #main "up; goto 325 85"
    #main "down; circlefilled 5"

    'draw four - origin 380,20
    #main "up; goto 395 35"
    #main "down; circlefilled 5"
    #main "up; goto 445 85"
    #main "down; circlefilled 5"
    #main "up; goto 445 35"
    #main "down; circlefilled 5"
    #main "up; goto 395 85"
    #main "down; circlefilled 5"

    'draw five - origin 500,20
    #main "up; goto 515 35"
    #main "down; circlefilled 5"
    #main "up; goto 565 85"
    #main "down; circlefilled 5"
    #main "up; goto 565 35"
    #main "down; circlefilled 5"
    #main "up; goto 515 85"
    #main "down; circlefilled 5"
    #main "up; goto 540 60"
    #main "down; circlefilled 5"

    'draw six - origin 620,20
    #main "up; goto 635 35"
    #main "down; circlefilled 5"
    #main "up; goto 685 85"
    #main "down; circlefilled 5"
    #main "up; goto 685 35"
    #main "down; circlefilled 5"
    #main "up; goto 635 85"
    #main "down; circlefilled 5"
    #main "up; goto 635 60"
    #main "down; circlefilled 5"
    #main "up; goto 685 60"
    #main "down; circlefilled 5"
    #main "flush"

    Wait

[quit]
    close #main : END


Appendix B

Roll Dice - The complete program:


'** Roll Dice
'by Brad Moore - in the public domain

NOMAINWIN
WindowWidth = 260
WindowHeight = 230
UpperLeftX = 40
UpperLeftY = 40

button      #main, "Quit",[quit],UL, 130, 150, 100, 25
button      #main, "Roll Dice",[roll],UL, 15, 150, 100, 25

Open "Roll Dice" for Graphics_nsb as #main

    #main "trapclose [quit]"
    #main "down; fill white; up; flush"
    #main "font ms_sans_serif 10"


    'show dice outlines
    #main "goto 20 20; down; box 100 100; up"
    #main "goto 140 20; down; box 220 100; up"
    #main "flush"

    Wait

[quit]
    close #main
    END

[roll]
    'roll a random number of times
    playwave "dice01.wav", async
    times = int(rnd(0)*4)+4
    for j = 1 to times
       'clear any used memory
       #main "discard"
       'clear last dice
       #main "backcolor white"
       #main "up; goto 20 20; down; boxfilled 100 100; up"
       #main "goto 140 20; down; boxfilled 220 100; up"
       'choose some dice values
       dice = int(rnd(0)*6)+1
       gosub [showDice1]
       dice = int(rnd(0)*6)+1
       gosub [showDice2]
       'put a loop in here to pause for a brief moment (100 millisec)
       t = time$("ms")
       while time$("ms") < t + 100 : wend
    next j
    wait

[showDice1]
    #main "backcolor black"
    if dice = 1 or dice = 3 or dice = 5 then
       #main "up; goto 60 60"
       #main "down; circlefilled 5"
    end if

    if dice > 1 then
       #main "up; goto 35 35"
       #main "down; circlefilled 5"
       #main "up; goto 85 85"
       #main "down; circlefilled 5"
    end if

    if dice > 3 then
       #main "up; goto 85 35"
       #main "down; circlefilled 5"
       #main "up; goto 35 85"
       #main "down; circlefilled 5"
    end if

    if dice = 6 then
       #main "up; goto 35 60"
       #main "down; circlefilled 5"
       #main "up; goto 85 60"
       #main "down; circlefilled 5"
    end if
return


[showDice2]
    #main "backcolor black"
    if dice = 1 or dice = 3 or dice = 5 then
       #main "up; goto 180 60"
       #main "down; circlefilled 5"
    end if

    if dice > 1 then
       #main "up; goto 155 35"
       #main "down; circlefilled 5"
       #main "up; goto 205 85"
       #main "down; circlefilled 5"
    end if

    if dice > 3 then
       #main "up; goto 205 35"
       #main "down; circlefilled 5"
       #main "up; goto 155 85"
       #main "down; circlefilled 5"
    end if

    if dice = 6 then
       #main "up; goto 155 60"
       #main "down; circlefilled 5"
       #main "up; goto 205 60"
       #main "down; circlefilled 5"
    end if
return


Glossary

FreeForm - FreeForm is a utility that comes with Liberty Basic that will allow the user to design a window (or form) for a program using drag and drop techniques, and setting properties in control property windows. The program will automatically generate the code required to create the window you created. FreeForm comes with its own helpfile, which says the following about itself:

What is Freeform?

Freeform is a tool that allows programmers to easily create a form (a window with controls), manipulate it is a number of ways, then generate the Liberty BASIC code for that form. One can even create a skeleton program built around the form. This help file is part user's guide, part forms tutorial.

FreeForm is unique in that it is written in 100% Liberty Basic code and is compiled into a tokenized file for distribution with Liberty Basic. It was origiannly written by Carl Gundel (creator of Liberty Basic), but has since been under community sponsorship, and is maintained in an open source environment.

To access FreeForm from the Liberty Basic editor, Select the RUN menu and then select 1) FreeForm from the bottom of the menu list. Here is a screenshot of FreeForm in action:

Main console window - We have discussed the main console window before, but for a refresher, it is the default programming window where input and output are directed for Liberty Basic. It is character based only. Refer back to the first installment of this series for more information and examples of programming in the main console window.

When GUI based applications are developed, it is sometimes desirable to turn off (or suppress) the main console window. This is done with the NOMAINWIN statement.

Absolute Referencing - Absolute referenceing applies to the location of graphics or controls on a window or in a graphicsbox. It means that all items are place at coordinate locations in relationship to the window (or graphicsbox) origin 0,0 - which is the upper left of the window (or graphicsbox).

GUI - GUI means Graphical User Interface. It is the presentation component of many major operating systems including Microsoft's Windows. It allows for easier, more intuitive interfaces with software based on graphic pictures representing many real world items or tasks, arrows, pointers, selection by point and click and many other innovative ideas. It is important to realize that just because GUI exists, it does not automatically mean that every graphic user interface is well designed or more meaningful than a character based presentation would have been. This really depends on the designers of the interface. There is an excellent article about good GUI design called User Interface Design For Programmers by Joel Spolsky on-line at [http://www.joelonsoftware.com/uibook/fog0000000249.html]. I highly recommend this read!

Random number generator - A random number generator is anything but random - it really is a set of tables of computed probable random numbers based on a certain model (there are several models) that describe how often a ceratin value should appear. Then based on these tables and some internal magic, a random value is generated. In the case of Liberty Basic, the random number is a value between 0 and 1 - not including 0 or 1.

Liberty Basic allows the programmer to get a more random state from the random number generator by seeding it with a start value. This is done with the RANDOMIZE statement. Here is what the helpfile says about seeding the random number generator:

RANDOMIZE n

Seed the random number generator in a predictable way. The seed numbers should be greater than 0 and less than 1. Numbers such as 0.01 and 0.95 should be used with RANDOMIZE.

Usage:

  'this will always produce the same 10 numbers
  randomize 0.5
  for x = 1 to 10
    print int(rnd(1)*100)
  next x

The helpfile describes how to get the same predictable results by seeding the generator with the same seed each time. By seeding the generator with a truly random value (such as the number of seconds since midnight divided by 10000 or something even more unpredictable, yet measurable and unassociated with the RND function) you can get even more unpredictable randomness.

Code-Reuse - Code Reuse is the industry phrase used to describe the process of writing efficient code that has many sub-routines, functions and sub programs that are used by multiple processes, allowing less code to be written and usually resulting in simpler maintenance (although not always). It also refers to a separate but relate process in programming of keeping a library of regularly used routines in a file, sometimes in a library that is linked directly to your projects at compile time. Liberty Basic does not support linking in the sense that a compiled language does, and thus does not support the use of native library files, but it does allow dynamic linking of library functions found in DLLs. But that is another story altogether.


Home

Tip Corner: Using

Game Programming

Round Buttons

Rotating Objects

LBW: Book Marks

Slide Puzzle

Speech DLL

Addresses

Beginners Programming

Newsletter help

Index