Foundations

Introduction:

This article is part of "Krylar's Games", a series of tutorials.

The first time you sit down and look at someone else's code it looks like Greek. Now, if you happen to read Greek, well, then pick a language you don't know and replace Greek with that. :)

The intent of this article is to help you understand the fundamentals of the BB language and it's syntax.

 

Comments:

One of the things that a lot of people do (assuming they're conscious that others will be looking at their code) is comment that code. This is an extrememly important thing to work into your daily programming life because you or someone else will eventually have to study your code.

I can't stress the importance of commenting enough, but most people only learn to do it after they've had to go back and fix something 6 months after they thought it was complete. When you're looking at the code on a daily basis, it makes perfect sense to you. But take a week away and you'll be kicking yourself for not commenting appropriately.

"Okay, Captain Spanky, so how do I comment?" Simple! All you need to do is use a semi-colon. Anything after a semi-colon (to the end of the line) will be considered a comment by BB and will NOT be compiled into your code. So, it's for your reference only. Comments will not increase the size of your finished application and they will not slow your application down in the least.

Here's a few examples of comments:


  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; Commenting code demo!
  ;; Developed by: Krylar
  ;; Last Revision: 10/09/2000
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  ; Let's create our constants here
  Const kSpacebar = 57      ; Holds the scan-code of the keyboard's spacebar 
    
  ; Let's create out global variables here 
  Global iShields = 200     ; Holds the current shield power 
  Global iArmor = 100       ; Holds the current armor power 

Note that at the top I put a little information on the program. This isn't 100% necessary, but it's sometimes nice to keep track of things like this. Usually that section is useful for explaining what the program is made to do. Also, you may notice that I got a little creative with the semi-colons...making a little box-like thing around that program info. That's just a habit of mine ;)

Second thing is that I seperate each major section with a space and a comment. I do this just to keep things organized. That's why the "global" variables section sits apart from the "constant" values section.

Finally, I put a comment to the right of each constant/variable created so I can define exactly what each one is for.

Now this example shows some pretty detailed commenting, but that's only so I could demonstrate the various comment options. If your variable names are done smartly, a lot of this commenting will be unnecessary. For example, it's pretty obvious what the iShields and iArmor are all about, no?

 

BlitzBasic Commands

Whenever you program, you'll need to use a set of "commands" that the language supports. Each command has it's own "syntax", or rules for use, that you must follow in order to make the command do it's job properly. Generally, but not always, this syntax includes the use of "arguments".

An argument is simply a value that you send along with the command. For example, let's say that we are using the Graphics command in BB. For this command to work properly, we'll need to tell that command what resolution we want our graphics mode to be. Games can support many resolutions, everything from 320x200 up to 1280x1024. Most games nowadays support 640x480, 800x600, and 1024x768.

So, since we need to tell the Graphics command which one we want to use, we'll need to "pass it arguments". Here is how it would look for us to set up an 800x600 resolution.

 


  Graphics 800,600

That's it! Super simple ;) Now, you *can* also request that command to set up the "color depth", which how many colors to make that resolution support. I personally wouldn't recommend this though since BlitzBasic will grab what it feels is the best color depth for you.

There are tons of BB commands and I'm not going to cover them all here, but I am going to cover a few that are key to developing a simple application. The commands I'll cover are:

 

Graphics: This command is the heart and soul of BB. Without this command, it's almost pointless to press "Run". You can do some stuff without it, like writing to files and such, but it'll be necessary if you want to create anything that outputs to the screen.

Use Graphics to set up the video mode (resolution) that you want to use with the game. Examples would be 640x480, 800x600, 1024x768.

While...Wend: This is a combination command. This combination allows you repeatedly run all the commands sitting between the While and the Wend until the condition set in the While section is met. This is also known as a "Loop". Here's an example...this will not work if you enter it AS IS in BlitzBasic. Later though I'll be using this combination command in a program to show you how it's used. For now, just study this piece:

 


  iCounter = 1                  ; create a local variable called iCounter
  
  ; If iCounter is less than 10
  While iCounter < 10          
   
    Text 200,200,"Hello!"       ; print out Hello
    iCounter = iCounter + 1     ; Add one to the current value of iCounter
    
  Wend
  

So, what this will do is continue running through that While...Wend Loop until iCounter, which is being added to each time, is greater than or equal to 10. So, it will print "Hello!" ten times on the screen...unfortunately it will print it over top of itself, so it's quite anti-climatic.

If...Then...Else...Endif:

This is probably one of the most utilized command sets when coding. It basically allows you to check a condition and do something if that condition is valid. So, let's say you only wanted to print a score if it's greater than 0 points. You would go:

 


  If iScore > 0 Then
     Text 200,200, "Score = "+iScore 
  Endif
  

As you can see, if iScore is less than or equal to zero nothing will be printed!

Now, let's say that you wanted to print the score if it's greater than zero, but you still want to print something if it's not. You would do this:

 


  If iScore > 0 Then
     Text 200,200, "Score = "+iScore
  Else
     Text 200,200, "No Score Yet!"
  Endif
  

This basically says that if our score is greater than 0, print out the score. Else (or, otherwise) print out "No Score Yet!".

There is one other thing I want to touch on with this command set, and that's nesting. Nesting basically means "having Ifs within Ifs". Here's an example:

 


  If iScore > 0 Then
     Text 200,200, "Score = "+iScore
  Else
     If iScore < 0 Then
        Text 200,200, "You're in the Negatives!"
     Else
        Text 200,200, "No Score Yet!"    
     Endif
  Endif
  

This one does *almost* the same thing as the last one. The primary difference is that when the score is NOT greater than 0, we check to see if it's LESS than 0. If so, we print "You're in the Negatives!"...Else we print "No Score Yet!". So, here's the table for that:

iScore > 0, so print "Score = " and then the score
iScore < 0, so print "You're in the Negatives!"
iScore = 0 (since it's neither greater than, nor less than, 0), so print "No Score Yet!"

Text:

This command has been used throught my little code snippets above. What this does is tells BB that you want to print out text at a certain location on the screen. The arguments passed are:

 

You can also set this command up to control whether or not to center the line on the X, Y axis or not. To do so, you would use the optional arguments "centerX" and "centerY". Here's what a centered version would look like:

 


  Text 200,200,"Hello, World!", 1, 1
  

One other thing to note is that you can add a variables value to the text by using a "+" sign and the variable. Here's an example:

 


  iAge = 32
  Text 200,200,"Hello, World! I'm "+iAge+" years old!", 1, 1
  

This will print out, "Hello, World! I'm 32 years old!".

Cls:

This command requires no arguments. It simply clears the screen. Yep, that's it!

Now, you can use it's sister function, ClsColor to clear the screen with a specific color. In order to do that, you would have to provide the Red, Green and Blue color arguments (all numbers between 0 and 255). The default is 0, 0, 0. Here is some code:

 


  Cls                     ; standard clear screen...clears it to black
  ClsColor                ; clear screen...no arguments, so defaults to black
  ClsColor 255, 255, 255  ; since red=255, green=255 and blue=255...it will clear to white
  

You can use almost any paint program to find out the various RGB (Red, Green, Blue) values.

End:

This is not a completely necessary command to use, but if you don't use it you'll end up getting a windows dialog box that explains the program has ended. Plus, I don't know what it will do if you have a compiled version of your code. So, just to be safe I include it at the end of the main While...Wend loop.

All this command does is instructs BB to shut everything down and return control to Windows.

 

Sample Program:

Okay, now to tie all this together, here is a simple program that moves "Hello, World!" across the screen over and over until you hit the ESC key. Something to note is that this WILL flicker as it flies across the screen. This is because we're not using a good animation technique (which I'll get into in a future article)...this is to demonstrate our commands only.

Pay CLOSE attention to the comments here!

 


  ; Call the BB "Graphics" routine to initialize our graphics mode
  Graphics 800, 600
 
  ; declare a global variable to keep track of the x
  ; position of our text
  Global iX=-120
 
  ; keep doing this piece until the 
  ; user hits the ESC key
  While Not KeyHit(1)
  
      ; clear the screen
      Cls

      ; print the text at our current iX, iY location
      Text iX,iY,"Hello, World!"
      
        iX = iX + 1      ; add 1 to the current value of iX

      ; check to see if the value in iX is greater than 800
      ; (which is our screen resolution)
      If iX > 800 Then
         iX = -120       ; it is greater, so reset the value
      EndIf
      
      Delay 3            ; tell BB to delay for 3 milliseconds
  Wend
  
  End                    ; Give control back to Windows
  

Conclusion:

Enter the above program into BlitzBasic and run it...it ain't fancy but it's a start! Also, play around with the iX values and the value after the Delay command to see what kind of wackyness you can create! ;)

This tutorial is by Christian Coders>