I was asked to do a little tutorial for BlitzMax. A suggested game was a simple top down shooter where the player controls a rotating turret in the center of the screen and has to shoot different enemies that approach him from all sides. The game gets more and more difficult by raising the speed of the enemies, introducing different enemies that take more hits and so on. This sounded doable to me and it seemed to offer many possibilities to introduce some OO and other nice features of BlitzMax so I decided to give it a go.
|Published (Last):||13 April 2009|
|PDF File Size:||17.86 Mb|
|ePub File Size:||15.54 Mb|
|Price:||Free* [*Free Regsitration Required]|
This guide was written for people new to BlitzMax and perhaps even programming, especially recommended to those who want to take a first step in Object Oriented Programming OOP in BlitzMax. My aim is to give anyone the opportunity to get a good start in learning the fantastic BlitzMax. Ways have changed but I promise you, it's for the better. When you have taken the first steps and learned your way around there is no other place like BlitzMax ;. Also the code in this tutorial may be hard to paste, sometimes it laps pages and the tabs don't seem to copy all the time.
A good way to learn is to do. Read my examples but try to write them yourself. It gives good coding practice. If you want to use any code within this guide for anything, please do so, the code is public.
You probably know this: A variable is a place where you may store a number or a text. There are different variable "types" depending on their use.
Here are the most basic types: Integers which store numbers, Strings which store text and Floats which store decimal numbers.
We also have object-"types" which includes these basic types such as Arrays, Lists and your own custom Types more on these later. See the Language Reference if you want more information about BlitzMax variables.
If you want to increase a variable, let say speed. Both are identical, but the last is shorter. It is required that you declare your variables before use. I do not use SuperStrict in my examples, so if you want to use SuperStrict make sure you declare all the variables. You should always use Strict unless you are very lazy or are making a tiny examples or minor tests, it will help you find misspelling-bugs before they occur and save you hours of debugging.
Same goes with all commands, like rem or Rem or reM. A variable can be Global or Local. Globals can be accessed from the entire program. Locals on the other hand are more complicated, cause where they exists depends on where they where declared. To declare a local variable use the keyword Local in front of the variable name. If you declare a local variable in a function it will only exist in that function.
If it is declared in a loop if will only exists as long as you are in that loop. If a local is in an if-statement it will only exists in that if statement. You can also declare constants. A constant will have the value you gave it when you first declared it. The value of a Constant can never change, it will always stay the same.
If you try to change a constant'a value the complier will warn you when you compile - build. Constant is useful for values that always will stay the same. You'll encounter constants in examples later on. Comments are text which explains the code. Comments are not required for your program to work, still it's one of those things you can't live without! Here is a sample of a comment:. Comment much, it helps others who read your code and it will help you, because eventually you will forget why you did it a certain way or why you added that function and what it did.
While you are new to programming I would advice you to comment almost every line. To explain something is a good way to learn it, use comments as your walking stick when you take your first steps in programming and BlitzMax.
If statements are used if you want to check if a condition has been meet and then act upon that. A,B,C and R are variables.
Note: "End if" can also be written "EndIf", it does not matter which you use. When to use Then? You can put it after your If expression. The use of Then , is not required and I think the code is just as easy to read without Then. If you take a BlitzMax file and delete all Then , it will run the same. Use Then if you think it helps you read the code. When I use then, then it is to make an ifstatement on one line more readable, like in the example above and in the example below.
My advice is, never use then if your if-statement is not on one line. False means something is equal to 0, True if it's not equal 0.
Many functions return 1 if success and else 0. BlitzMax doesn't have Boolean variables which only accepts True or False. The two lines above do the exact same thing. You can also check objects with true and false, if an object is "null" does not exist it's false else it's true.
If you want to know how to use these commands check out the Module Reference. To be able to use your graphics card you will first need to set a graphics mode - specifying the resolution you want to use. Just enter Graphics , for a full screen resolution of x Graphics ,,0 gives you windowed mode, very good for debugging. Note: Keep reading there is a sample below! A loop is a way to tell Blitz to do one thing several times, or in games to update the game until the game is ended.
Loops are what makes games run in real-time. So it will loop depending on X. Try to run the example on the next page:. Press F5 to build and run the example. The above code will create a loading line on top of the screen. If you put a Cls on a new line below Flip , you'll notice that a instead of a line, a box will be traveling from 0 to , measured in pixels. Also you can replace DrawRect with DrawOval and guess what…. In BlitzMax everything you draw is drawn to an invisible Board. You can draw how much you want to this board but it won't show up on screen.
You can see it as if BlitzMax is drawing on the back of your screen, the when you want to show it, you Flip the Board and we can see what have been drawn to it. If we keep drawing a lot of stuff and Flip, then continue draw a lot of stuff, the board will be a mess. This is why we clear the board after we have flipped it, but this also means we have to redraw everything we previously had at this board! And that's how it works. This board is known as the back buffer. This method with flip and clear is called double buffering and is done to prevent flickering graphics.
This part is identical to BlitzBasic. At the Top Left of the screen we have the point 0,0. Add these lines to the above example, just one row below "Repeat": They might be hard to see, try fullscreen by removing the last ",0" in graphics. The resolution is what determines how many pixels you will have at each axis. So in our example the screen width in pixels would be and the screen height The more pixels at screen the more calculations is required by the computer both in 2D and 3D.
Add the following to our example: DrawLine 40,40,80,80 ;DrawLine 40,40,40, 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.
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. If you done it right. You'll notice a speed increase when you drive around. See the scan code section of the manual for a complete listing of keycodes. 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:.
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:. Now add the function above, Collectdata, to the bottom or start of your file.
Add the following inside your loop:. The name, id and age are the function Collectdata's input parameters.
Beginners Guide to BlitzMax
Top down shooter tutorial with BlitzMax