Beginners Tutorial - Coming To Grips With Commands

Started by kevin, December 27, 2005, 09:50:59 PM

Previous topic - Next topic

kevin


Beginners Tutorial - Coming To Grips With Commands in PlayBasic  

Written:  28th,Dec,2005    -   Document Revision: V0.02

By Kevin Picone

© Copyright 2005 by Kevin Picone, All Rights reserved!




What is this ?


   This extremely brief tutorial is a crash course in learning the basic principals of  making PlayBasic programs.




Lets Begin


   In it's most simplistic form, learning to programming is the ability  arrange a sequence of  instructions into order.   These instructions come in the form of 'commands'.   That is to say, we are commanding PlayBasic to do something.

 So in order to get the hang of things quickly, It's generally best to start with some simple graphics commands.  These allows us see what each command does and how altering them changes the output.

This first example is perhaps the most classic programming tuition example in history.  It's commonly referred to as the "Hello World" program.  What does it do ?  Not much really, it just displays a Hello World message on the screen, then waits for the user to press a key before ending

This first example uses the PRINT, SYNC, WAITKEY and END commands.    For a more detailed information about each command,  move your mouse over any Highlighted command within a program, left click it to position the edit cursor within it, then press the F1 key.   This will launch the integrated help, and give you some information on this command.


PlayBASIC Code: [Select]
;------------------------------------------------------------------
; This example displays a Hello World message to the screen
; then waits for the user to press a key before ending.
;------------------------------------------------------------------

; Display the message to the screen at the current cursor position.

; Note: Each time we draw a text messages to the screen, each new message
; will be drawn at the start of a new line, following the previous message.
print "Hello World - This is my first program"



; Call the SYNC command to show the drawn graphics to the user.
Sync


; Tell PB to wait fopr a Key press
WaitKey


; Tell PB to end your program and return to the editor
End








Why are some words different colours when I cut & paste  or type program code into the PlayBasic Editor?


  The PlayBasic editor incorporates what is known (in programming circles) as syntax highlighting.   This is just a fancy way of saying that it will automatically colour the different words in your program, in an effort to make it easier to identify the commands from comments and variables ( Read the PB help tutorials about variables. Located in About Section under Variables)


Commands by default are highlight in RED

Comments are highlight in GREEN

Variables will be in black




What are comments ?


 Your programs don't have to be made up purely of program code, you can also interleave 'comments'  into your programs.  These comments might be anything, from a line describing what the following piece of code does through to slabs of descriptions / ideas lists, things to do, your favourite colours, through to the complete script of "lord of the Rings" if your wanted..

Comments are for users to document how your program works,  thus PlayBasic will completely ignore any commented section within your program.

You can insert comments in two different ways.   Single line or Multi Line.

For single line comments, you simply place a ";" or "//"  chrs at the start of the commented line.  This will make PB ignore this line

I.e
PlayBASIC Code: [Select]
; This is a single line comment

// This is a single line comment




Or for those old school programmers, you can even use the REM statement,

I.e

PlayBASIC Code: [Select]
   Rem This is also a single line comment




For Multi line, you can either use the REMSTART and REMEND pair or the  c styled /* and */  pairings

 eg of   REMSTART and REMEND  mutli line comments

PlayBASIC Code: [Select]
   REMSTART
This is a multi line comment
Here I can put anything I like, from how my program works, through to my shopping list if i wanted.
REMEND





 eg of    /* and */  mutli line comments

PlayBASIC Code: [Select]
   /*
This is a multi line comment
Here I can put anything I like, from how my program works, through to my shopping list if i wanted.
*/









Why do some commands have numbers,  text or both , after them ?


These are called parameters.  

While some commands perform their action without requiring any parameters, many don't.  In fact the majority of commands will require you provide them with a list of parameters to specify how this action should be performed.

If you look back the first example you'll notice the Print command expects a parameter, while the others don't.


You can obtain the parameters for any command in a couple of ways.  

1) Type the command into the editor then press space after it.  This should pop up the expected parameter list for this command.

2) Move the mouse over any command you need parameter/help information about, left click it (to position the edit cursor at this position)  then press the F1 key. Which will take you directly to that commands help page.

3) Use the Search feature built into the Editors help system.  (Requires PB1.89 or above)


Example 1.


 Lets look at the DOT command.   If we look in the Help files under Graphics commands, we should find that the Dot command expects two numeric parameters.  

 Those parameters being Xpos & Ypos.   These are short hand for X position and Y Position of the dot we wish to draw.  


Lovely, but what does it do ?  - Each Dot command draws a single dot to the screen in the current ink colour.   So it's parameters specify what dot (out of the thousands available on the screen) we wish to draw upon the screen.

Note:  The screen is nothing more a 2D grid of dots. Where the X axis runs across the screen (from 0 to Screen Width) and the Y axis runs down the screen (from 0 to Screen Height)     So position 0x / 0y is the TOP LEFT position of the screen.


PlayBASIC Code: [Select]
; Draw a DOT at X position 100 and Y position 50 on the screen
Dot 100,50

; Call the SYNC command to show the screen to the user
SYNC

; Tell PB to Wait until the users presses a key
Waitkey





Example 2

 Lets look at the LINE command.   If we look in the Help files under Graphics commands, we should find that the Line command expects four numeric parameters.  

 Those parameters being X1,Y1,X2,Y2     Where X is short hand for X position and Y  is short hand for Y position.

  Line draws a line between any two points (dots) upon the screen.  The first pair of X & Y parameters represent the starting coordinate and the second pair represent the ending coordinate.



PlayBASIC Code: [Select]
; Draw a LINE  start at X position 50 and Y position 25 on the screen, to 200X and 100Y
Line 50,25,200,100

; Call the SYNC command to show the screen to the user
SYNC

; Tell PB to Wait until the users presses a key
Waitkey






Example 3

 Lets combine some of our drawing commands to draw something a little more interesting.   In this example we'll use the following commands DOT, LINE, CIRCLE, BOX and PRINT.   Look them up in the Help for parameter descriptions.


PlayBASIC Code: [Select]
; Display a Message to the screen
Print "Example of Drawing Commands"

; Draw a Dot at X position 100,Y position 50
Dot 100,50

;Draw a straight line from 100x,100y to 200x,100y
Line 100,100,200,100

;Draw a Filled Box from 100x,150y to 300x,160y
Box 100,150,300,160,true

; Draw a circle at 110x,200y with a radios of 10 pixels
Circle 110,200,10,true


; Call the SYNC command to show the screen to the user
SYNC

; Tell PB to Wait until the users presses a key
Waitkey






That's it for now!