Wave's~ BlitzMax Tutorial for NG | ~ November, 2015 ~ Version 11
Beginners guide to BlitzMax |
Input
Input is an easy and simple part of BlitzMax. If you understood the Loop Example this should be a piece of cake. (We're talking about keyboard and mouse input, not the command called input)
Strict Graphics 800,600,0 Local X=500 ,Y=500 '<- - - - - - - start location '- - - - - - - - - - Loop Start - - - - - - - - - - While Not KeyDown(Key_Escape) ' This loop is equal to repeat except the condition is at the top. Cls 'DRAW___________________________________ DrawOval X,Y,8,8 'Draw an Oval at X,Y with a width and height of 8 DrawText "Press Arrow Keys to go around, Space to Center",20,20 'INPUT___________________________________ If KeyDown(Key_Left) X:-1 'Decrease X <-- go left If KeyDown(Key_Right) X:+1 'increase x go right --> If KeyDown(Key_Down) Y:+1 'Increase Y Go Down \/ If KeyDown(Key_Up) Y:-1 'Decrease Y Go Up /\ If KeyHit(Key_Space) Then 'Set position X=400 Y=300 End If Flip Wend '- - - - - - - - - - Loop End - - - - - - - - - -
To optimize the above code add a new variable called speed after Graphics but before the loop. Now replace all 1's with this new variable speed. Go to the place where you have your variable speed (at the top) and set it = 5. If you done it right. You'll notice a speed increase when you drive around. Also note that all keys have names which start with Key_ The a-key is Key_A and so on for all letters and numbers. See the scan code section of the manual for a complete listing of key-codes.
Functions
A function is a way of reusing code. It can also be a way to split up your code into easier to manage parts. They are just the same as in previous Blitz.
A function looks like this:
Function Collectdata$( Name$, Id, Age ) ' $ means String, nothing means Int Local TotalData$ = "Name: " +name+ ", ID: "+id+", Age: " +Age Return TotalData End Function
To use this function I must pass a name, an id and an age to it.
Take your Input example and add this line below graphics but before the loop:
Local TestData$ = Collectdata("Blast" , 8, 30)
Now add the Collectdata Function to the bottom or start of the code.
Add the following inside the loop:
DrawText TestData,20,40
The name, id and age are the function Collectdata's input parameters. The function returns a String. A function can return any type of data including objects. What type it returns is specified after the function name , the "$" for String. If you change this to an int "%" or :Int. Function CollectData:Int(..←Now you will receive a common error: "Unable to convert from 'String' to 'Int' ". The function does actually return a string but you specified you wanted it to return an int so BlitzMax will warn you that something is wrong. But this does not mean you can't convert strings to ints, see below.
To convert strings to integers simply do this:
Local A% = Int ( "123E234 -This is string" )
This will result in A = 123, same goes with floats B# = Float(A), This will result in B = 123.0
Here is another example of a simple function:
'--------------------- A D D ---------------------- ' Parameters: A,B,C ; numbers to add ' Returns: The sum of these numbers ' Note: Only accepts integers '_______________________________ Function Add( A% , B% , C% = 0 ) Return A + B + C End Function '-------------------------------------------------
Number = Add(2,2,3) 'Will set Number to 7 Number = Add(2,2) 'Will set Number to 4 Note that in the function that C=0, which means use C=0 as default if none specified. If the function would have had a C% only, without the =0 part, you would encounter a compiler error doing Add(2,2) but it would work fine to Add(9,9,9). Note that it is important to comment your functions. Write what the parameters are and what it returns and what it does. Always try to keep your functions less than a page in size. If they become larger try to divide them up into several functions. Functions can be used inside other functions. In the long run good comments and frequent use of functions will save a lot of your time! If you don't specify any return parameter as in this latest example, BMax assumes you want to return an int. Nothing = Default = Int.
To Index | Next Page | page 7