PlayBASIC Program Layout

By: Kevin Picone


I N D E X:






Introduction


      If you've never programmed before, or even if you have, PlayBASIC (like all programming languages), follows certain conventions on how it expects your program code to set out. While most of these are fairly universal, that doesn't help if this is the first time you've ever programmed. So lets go over the basics.

      Firstly, PlayBASIC expects program code to be arranged from Top to Bottom. So our program is processed starting at line #1, then line #2, line #3 and so on down the program code.

      Each line of code, can have one or more commands on it. To have multiple commands on a line, requires that each command be separated by the colon character :.

E.g

  
; This is two lines of program code on separate lines
  Print "Hello"
  Print "Welcome to PlayBASIC"
  
; Those lines could also be written on the same line, like this
  Print "Hello" : Print "Welcome to PlayBASIC"
  
; Show the screen
  Sync
  
;Wait For a key press
  WaitKey
  


      If you cut and paste this example into the IDE(editor) and execute it (Press F5). This example will display the following,

  
  Hello
  Welcome To PlayBASIC
  Hello
  Welcome To PlayBASIC
  


      This example uses three basic commands, Print, Sync and WaitKey While it's very simple, it does demonstrate the top -> down flow of our program code.

      You can however change a programs flow by using decision (see: if and looping commands (see Loops) which will be necessary in order to keep your program running. This is because when PlayBASIC reaches the end of your programs code, it will end your program.


      Therefore most programs are broken down into three main parts.

Declarations


      When a program starts, we'll generally need to initialize a number of things. Ranging from the display settings, variables, arrays or load common pieces of media such as images or sounds. Once these are set up, our program is ready to start running it's Main Loop


Main Loop


      To keep a program running, we often have one central loop. This loop will generally either perform all the necessary work there and then, or call a list of sub routines / functions that perform the various tasks required.



Functions / Sub Routines


      Functions and sub routines are a way for us to break large sections of program code down into smaller reusable parts (if need be). This can be a real life saver as projects get larger, since it not only helps to keep the length of our programs down, but it makes our program easier to read. Generally the simpler the code is to follow, the easier it'll be to maintain. (Ie. Find bugs, add new features etc)

      So a program (in format terms) might look something like this. (this is not real code by the way, it's just an example of the logic)

  
; Initialize Program Settings
  Load All the Required Graphics, Sounds, And Initial And variables And arrays we require
  
; Start Programs Main Loop
  Repeat
     
   ; call function to move the aliens/characters in our game
     MoveAliens()
     
   ; call function to update the players position
     MovePlayer()
     
   ; Draw everything  to the screen so we can see it
     DrawGraphics()
     
   ;  Jump back to the Repeat statement,  if the PLAYER is still ALIVE
  Until Player<>ALIVE
  
; If the main loop ever exits (the player dies), tell play to END the program
  End
  
; Functions
Function MoveAliens()
; Code to move the aliens goes in here
EndFunction
  
  
Function MovePlayer()
; Code to move the Player goes in here
EndFunction
  
  
Function DrawGraphics()
; Code to draw the aliens, Player and anything else goes here.
EndFunction
  





      If you're not familiar with some of these terms, then it's highly recommended you make your way though the other included tutorials. See Variables, Loops, ArrayBasics, Functions&Psub, Types


Top






Compiler Passes


      PlayBASIC by design is a two pass compiler.

      Pass #1 is a pre-scan of the your program codes layout. During this pass, PB is only looking to identify Label names, Functions and Psub declarations. This pre-passing is what enables PlayBASIC to detect if a Labels exists or what parameters user defined functions use.

      Pass #2 is the compilation pass. This is where you code is compiled ready for execution.

Top






Variable Declarations


      PlayBASIC is a loosely typed language by default. This means that variables can be used anywhere in your program, without them needing to be explicitly declared previously. This is often more convenient for beginners in the short term, but can actually lead to 'typo' styled errors during the development of larger programs. However, you can also make PlayBASIC enforce variable declaration if you want using Explicit operator. When explicit is enabled, variables must be declared prior to being used.



What about Global/Local/Static Variable Declarations ?

      Global/Local and Static variables are an exception. These variable types must be declared before they are used.

eg. Correctly pre declared Local variables
  
  Global A
  
Function ShowGravity()
; declare local variable (A)
  Local A
; Use this variable
  A=45
EndFunction
  



eg. Incorrect use of Local variable declaration
  
  Global  A
  
Function ShowGravity()
; This will
  A=45
; Declaration should be before the variable assignment
  Local A,B,C
EndFunction
  




     Not sure what variables are ? then make sure you read the Variables Tutorial.

Top







Array Declarations


     Arrays require previous declaration before you can start using them. This means that an array has to be dimensioned some place prior to be the array being used (ie. storing or retrieving information from it).

      Ideally you will normally dimension arrays at the start of your program (your programs declaration area) or at the top of your include files, which will make the array declarations visible to the following code.

eg. Correct Usage

  
; Dimension (declare) arrays at the top of
; program (prior to their use).
  Dim Table(1000)
  Dim Copy_Of_Table(1000)
  
  For lp=0 To 1000
     Table(lp)=Rnd(100)
  Next
  
  For lp=0 To 1000
     Copy_Of_Table(lp)=Table(lp)
  Next
  
  



eg. Incorrect Usage (This will throw a compiler error)

  
  For lp=0 To 1000
   ; Array is being used before it's been declared
   ; as such, the compiler won't know what "Table" is
   ; and produce and produce an error
     Table(lp)=Rnd(100)
  Next
  
; Dimension arrays
  Dim Table(1000)
  Dim Copy_Of_Table(1000)
  
  For lp=0 To 1000
     Copy_Of_Table(lp)=Table(lp)
  Next
  



      While you can dimension arrays at any point throughout a program, the array will only be visible to code that is bellow this declaration.

eg.

  
; Dimension the Table array.  This array is visible
; from here down through out this short program.
  
  Dim Table(1000)
  For lp=0 To 1000
     Table(lp)=Rnd(100)
  Next
  
; Dimension another array.  This array is only
; visible bellow this point in the program,
; you try and access this array before it's
; declaration you'll get an error
  Dim Copy_Of_Table(1000)
  For lp=0 To 1000
     Copy_Of_Table(lp)=Table(lp)
  Next
  
  



     Not sure what arrays are or how to used them ? then make sure you read the ArrayBasics Tutorial.

Top







Funtions & Psub Declarations


      Function & Psub's can be declared just about anywhere throughout your program. So unlike arrays, you can call your user defined functions prior to their declaration (see example bellow). However there are a few strict conventions you should be aware of.


* Functions & Psub names can be made up of a combination of letters (abc..xyz ABC...XYZ), Numeric (0123456789) characters and the underscore character '_'

* Functions/Endfunction & Psub/EndPsub keywords can Not indented. This helps keep

* Functions/Endfunction & Psub/EndPsub can Not be declared inside any existing nest.


ie Correct usage
  
; Call the 'Add' function and display whatever result it returns
  Print Add(100,50)
  
  Sync
  WaitKey
  
Function Add(ValueA,ValueB)
  Result=ValueA+ValueB
EndFunction Result
  



ie InCorrect usage
  
; Call the Add function and display the result of the addition
  Print Add(100,50)
  
  Sync
  WaitKey
  
; >>> PB Doesn't allow functions to be indented
  .Function Add(ValueA,ValueB)
  Result=ValueA+ValueB
  .EndFunction Result
  



      Not sure what functions and Psub are? or how to use them? Then make sure you read the Functions&Psub tutorial.

Top







Label Declarations


      Labels can be declared all through your program. However there are some things you'll need to keep in mind when using them.


* Labels must have a colon at the end of them. The colon is only needed when declaring a label, it's not required when calling the label. See example bellow.

* Label names can only be made up of a combination of letters (abc..xyz ABC...XYZ), Numeric (0123456789) characters and the underscore character '_'

* Labels can Not be indented. They must be left justified!

* Important Labels declared inside Functions/Psubs are only accessible from inside the Function/Psub also. You can't jump into/out of functions.

* You can jump (change control) to a label using the GOTO / GOSUB commands




  
; Jump to the Label
  Gosub DisplayName
  
  Print "Done"
  
; Display screen and wait for the user to press a key
  Sync
  WaitKey
  
  
DisplayNames:
  Print "Bill"
  Print "Fred"
  
; return to where this sub-routine was called from
  Return
  


Top








Do I Need Line Numbers ?


      No, line numbering is obsolete in modern BASIC, therefore PlayBASIC doesn't support line numbers.

Top






How Can I Continue lines ?


      PlayBASIC uses the underscore symbol _ (followed by a hard return) as it's continuation. What this does is left allows you to break long lines of code up into something that will fit on screen. You can use the continuation character pretty much any place you like. For example.


  
; draw a filled circle at 100x,200y
; with a radius of 50 pixels
  Circle 100,200,_
  50,_
  true
  
; show the display and wait for a key press
  Sync
  WaitKey
  


Top



 
Related Info: ArrayBasics | Functions&Psub | Images | Loops | Memory | NamingConventions | Operators | Types | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com