# DarkBASIC Programming/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/DarkBASIC_Programming

# Introduction to Programming

First of all, thanks for checking out this book on DarkBASIC. It's the first WikiBook on the subject of DarkBASIC Programming. Not everyone can program, but in the information age I believe that it's necessary for everyone to gain the skills to use different types of technology. Especially, computers and programming. Today and for the next 30 days you'll learn a lot about DarkBASIC Programming and a lot about what you can accomplish using a computer. DarkBASIC is all about making DirectX do your evil bidding and your domination of the world of computer game programming. I hope you read and contribute to this book, as well as rant because then I can fix stuff others will insult. I hope you enjoy!
In this tutorial I'm going to assume you have no prior programming experience, or else you wouldn't be reading this book. You would pick up the manual, compare it to C++, and program in DarkBASIC easily. First of all not to scare you but game programming is not easy and secondly you are not going to make a <insert copycat blockbuster game here> killer or clone or whatever in DarkBASIC for a long time. I'm also going to assume you have v1.0 of the "classic" or non-pro edition of DarkBASIC or else with another version you'll have some problems running this code unfortunately. Programming is not a new concept, and there are many hundreds of languages such as DarkBASIC out there, one such programming language comparable to DarkBASIC is BlitzBASIC, and you may have heard of Visual Basic.

## So what is programming?

This isn't a stupid question. Programming is not just making applications for Windows, or talking a language your computer understands. Programming is input, process, and output. If you take computer sciences, chances are you'll be doing program outlines, but right now you're a hobbyist programmer so let's keep it simple. Not all of us know VC++ or Pascal. DarkBASIC is game oriented. You can make applications in it, but it's harder. There are no message boxes in the version we'll be using. But DarkBASIC is easy and fun, especially for beginner's and junior programmers. Instead of having to write 1000+ lines of code to put a 2d character on screen you'll write one, instead of writing a massive program to make a cube spin you'll write a simple and human readable one in 20 or less lines. If you have no patience, take it slow and easy and don't move on until you understand it all or else you'll be bald by the time you finish this wikibook. I will say this: this book is a work in progress. I'll be finished as soon as possible so, bare with me please. Programming is simply entering lines of code that once compiled, or is run, tells the computer to follow out a task until it's done. What's involved is input, or you programming; processing or computing; and finally output, what you get to see.
Figure 1.1(This is the Model of Input, Process and Output.) So you know: A coder is another word for a programmer.
All programs follow the above method, since every program is (1) certainly programmed, (2) is certainly compiled, and (3) certainly displays results. Now, just what is code then? Code is the recipe for your program. Computers won't do a task wrong. You'll do the task wrong. To keep your program from doing things that could harm computers, the compiler(or program that checks to see if the program works) runs the program and generates syntax errors (if there's something wrong). These syntax errors, tell you that you made a mistake and you must fix it before you can see your program(in most cases). So, computers don't make mistakes.
There are many sorts of errors also! The most common is called a syntax error . A syntax error are mistakes in your program's code. A programming language like a written language is incorrect if it contains things that are gibberish. Like in English, asdfjdj is not a word, it's gibberish. Just like in DarkBASIC, print is a command, where as printf is not. Another kind of error is called a run-time error, this is when your program compiles but you don't get the results you want. Such as if your writing is sloppy, the word "hello" may look like jello and the person will get the wrong information from it. Run-time errors are things that aren't suppose to happen, like when in your favorite video game, when a character can sometimes walk through a wall, or the program crashing when this happens.
The process of fixing your mistakes is called debugging. You'll get familiar with this; half your time or more will be spent doing debugging. So as much as possible, save, compile, debug, and run. It'll save you lots of anger, and stress. Finally remember this: with computers, the language goes through assembly or getting put into binary, and sent back to your programming language. If the binary pattern is correct, then you have no syntax errors. Remember, 0s are off or false, and 1s are on or true. Remember when I said assembly above?
Well, here's how it works. First off, you compile or check syntax, then the code you entered passes through what's called machine language or assembly (its name changes depending on what its doing when it's compiling its assembler). Then it becomes pure binary (which is not human readable but meant for computers; they cannot understand anything other than 0s and 1s). Then it is sent back to the compiler where if the binary matches what's correct, it gets executed. If it doesn't, you get a syntax error and must debug. Now, don't quote me on that but that's basically what happens. So now you're ready to begin programming in DarkBASIC and will know what I talk about.
One more thing: DarkBASIC is not case sensitive. If you don't know what this means, it means that DarkBASIC does not distinguish between upper-case letters and lower-case ones.

# The Hello World Tradition

Alrighty, you came back. Good, let's hope you have played around with the DarkBASIC editor. Before you find out what the hello world tradition is (older programmers don't spoil it for them!), I think I should tell you something. The DarkBASIC editor is not very windows friendly, so make sure you download an IDE you like and then link it to the DarkBASIC.
So you know: an IDE is an integrated development environment. It allows programming to be easier for professionals and beginners alike. For now, don't worry what an IDE is too much, you're only beginning after all.
Okay, now then time to get to the tradition.
Code
End of Code
Copy and paste this source code into the IDE or editor (whichever) and run it. What you get is the easiest program you'll ever write in DarkBASIC. The hello world example is proud tradition of beginning programmers, so let's carry it on. Hello World Program puts the text hello world on the screen. But what are the quotation marks for? The quotation marks are used to show what you want to put on the screen. Otherwise, it would look like one big command.
Your Assignment:
Replace Hello World with any text you want
First of all, I hope you completed the assignment above. In case you haven't, or had problems with this program I'll show you how. Okay, let's say you want to print a couple of things and you need it spaced for readability. Remember I said that print, prints the things inside the quotes? Well they're called strings, now strings are characters that you can change and manipulate. With no string to technically print, you'll just get a blank space. Now for all on one line you use a semi-colon(;). Experiment with these two methods above until you understand them. In DarkBASIC, your program already ends on its own doesn't it?
But, it's good practice if you use the command "end" where your program needs to quit. So after the print commands in hello world, insert the command "end". Now, go to file and build EXE, save the EXE in whatever folder you want and run it. Technically it should just be a flash so quick you cannot see it. That's where you need some sort of waiting system, or input just to let you see the results. So delete the exe and open up DarkBASIC again, in our hello world before the end command insert a wait key. What a wait key does is allow the famous press of any key for the program to end, so rebuild the exe of this program and now run it. As you might see, the program isn't very windows friendly. At the top of the program put in these commands:
Code
End of Code
Now run the hello world program, see what happens? We now have a nice little window friendly environment. But hey, if you're on a different computer with different settings you'll miss some of the text, and what on earth are all those numbers for anyways? Okay the numbers are the X coordinates and the Y coordinates in case you didn't do math in school X is horizontal and Y is vertical. In case, you don't know what those words are X runs left and right or across, and Y runs up and down. So X & Y are 2 axes so we have 2 dimensions, when we get to 3d expect to get dimensional for a paragraph or two. Now then for our last part of the day, let's find out how to change the position of where our text goes.
Code
End of Code
Insert this piece of code above, right above the first Print statement. Our program is long now and we may also forget what some of the things above do. Welcome to comments, the Rem command lets you make notes, anything after Rem is ignored. You should always comment your code, so that others can understand it, and so you can know what you where thinking when you coded it and why you coded it the way you did. Remember, comment heavily!!
So you know: Rem is short for Remark, and if I haven't told you yet BASIC stands for Beginner's All Purpose Symbolic Instruction Code.
Here's what your program should look like in the end, roughly:
Hello World Program Results
End of the Program
Before we go here's a quiz:
    In DarkBASIC Strings Start With a:
    A)#
    B)*
    C)'
    D)"
    And they end with a:
    A)%
    B)^
    C)&
    D)"
    BASIC stands for:
    A)Beginner's All Purpose Symbolic Instruction Code
    B)Bears Are Purposely Stealing International Cookies
    C)Beginner's Alphanumeric String Informed Code
    D)Beginning All Inserted Symbolic Intel Coding
    In 2d there are 2 axis which are they:
    A)U & V
    B)X & Y
    C)U & V(in textures), X & Y also
    D)Z & U
    Rem can make comments for how many lines:
    A)1 or More
    B)1 or less
    C)Single Line
    D)Double Line
Okay Here Are the Answers, no cheating!
    1 & 2 is obviously D.
    3 is A.
    4 If you said anything but Z & U you should be on the right track but C is the best answer while
    B can be considered the most correct one.
    5 is C.

#### Assignment:

Experiment with the IDE and the DarkBASIC window commands. Comment the hello world program when done. Make a program that contains only comments. Look up IDE on wikipedia. Look around the DarkBASIC website(http://www.thegamecreators.com).

# Fundamentals of DarkBASIC

Well, we're almost at the end of your first week
experiencing DarkBASIC programming or at least this book! Okay, the fundamentals are all about logic, developing programs, design, setting outlooks and the building blocks. This is not application oriented, it's more game oriented as DarkBASIC is for game programming. You'll find out about conditions, variables, logic and more. Remember, awhile ago when I brought up 0s and 1s, well a better way of looking at them is TRUE and FALSE. DarkBASIC is simple, so there are no boolean operators or are there? Let's say you declare a variable called, oh, let's say A. For now A = 0 until we say A = 1, there's another value called B and it's equal to 1. If we had C = 1 when A or B is true or if C = 1 when A and B equal true, but what if neither of these values are true? Then we'd have to use another value and our program quickly looks like this:
    if A or B = 1
    C = 1
    else
    D = 1
    endif
That's logic, that's conditions, that's the if/endif command. Now I didn't indent above which I should have so anytime you use if/endif I want you to indent for the statements used okay? If you copied / pasted the above snippet in DarkBASIC and compiled it, you'd get no errors but if you'd ran this program you'd just get nothing. Why? They're just values and there is really no output to the screen involved, so let's change this program a bit.
Simple Conditional Statements Program
As you saw I used variables above, in case you didn't know variables are a way of storing data which can change. As you saw above if I changed the OR operator to a AND b = 1 then we'd have a whole new ball game on our hands. But let's sidetrack a little, if you wanted to remove the text on the screen you have to use this command "cls" which means clear screen. You can also change the transparency value of cls by simply putting a value ranging from 0 to 255 after the cls command. And I'll show you in a few lessons on how you can change the screen color to whatever you wish! You may be wondering about variables in DarkBASIC there are roughly three data types
There you are! For, good practice at the start of all your programs put cls, in other languages until you clear the screen you have text on the screen. For example, in pascal unless you clear the screen you get the same text leftover from your last modification. Indenting is also important but we'll get to that later. So why comment or indent? In team efforts or open source projects it makes your program more human readable, also it's easier to read and locate, plus you'll need to remember what's happening when you come back to your project later on and you might forget. Back to variables, you can name variables whatever you want but make sure they're not to long and they briefly describe what it does or used for.
Here are some variable examples
So why are there symbols in front of the variables? This shows what kind of variable we're dealing with, a $ is a string? A real or float is a #, in some languages there is no symbol in front of the values which can be a bit confusing. So again this makes it more human readable. So when designing software make sure it's source code is human readable! Get into good habits in programming like indenting - it'll save you and others time. For now, clear your mind and come back again tomorrow.
Alright it's time for some input other than press any key. Wouldn't you like to make your own game like Mad Libs? I'm trying to make one now in Visual C++, well in DarkBASIC you have to use the Input command. The input command needs a prompt(optional) and then a variable to store the input taken in. So what would this look like?
Remember semi-colons? Well they can also separate your string from a variable(even if it is another string strange eh?). You can also have input as just a variable input so it would look like this.
Oh yes you can use floats and integers also using input! Above where I use a comma you can also use a semi-colon if you prefer. Remstart means everything after it is a comment so beware, it will make everything a comment until you use the Remend command. What if you're database programming in DarkBasic(not a good practice use Visual Basic instead I advise!) and you have a hundred clients that need to be stored in variables? You can use the Dim command!
Let me show a more indepth example, and you can still use strings, integers and floats as long as you follow the rules(remember which symbol if any goes in front!).
A good dim statement
A bad dim statement
Hopefully you've been using these methods now, if not go review everything and put them into practice.
Your Assignment
Write a Mad Libs game
Quiz is coming up! For now write that Mad Libs game and take it easy for awhile.
Before we start here's a quiz! Told ya!
Which of these strings of text is a correct string?
    A)"Me"
    B)#noIam#
    C)"I am
    D)123456
The Input Command Does What?
    A)Accepts user input and stores it into a variable for later retrieving.
    B)Lets the user type in a string to be used later.
    C)Prints out text on the screen.
    D)None of the above.
It is good programming practice to?
    A)Not indent but comment heavily.
    B)Comment heavily.
    C)Comment when notes are needed, indent when needed, and use the cls command.
    D)Indent heavily.
DarkBASIC is oriented to what?
    A)Database Programming.
    B)Application programming.
    C)Game Programming.
    D)Web programming.
Why is it a good habit to make your program human readable?
    A)So that others can understand your program and you yourself can too.
    B)It's not.
    C)For open source projects.
    D)A + B.
In the case of A = 1 And B = 0, what will C equal if A or B is true?
    A)2
    B)0
    C)1
    D)N/A
#6 Corrected by: Cody C Oebel
Note: if A = 1, and B = 0,Without knowing the expression for what C will equal if A =1, and B = 0 tell's nothing of what C should equal . I Corrected answer for # 6 which should be D, or N/A.
Quiz Answers Cheating Won't Help You Be a Better Programmer
1 is A
2 is A
3 is C
4 is C
5 is A
6 is C note: Correct answer is D, or N/A
Edited by: Cody C Oebel Correct answer for 6 is N/A , or D
Okay, nice job if you didn't cheat and got a good mark or else try again until you do so. Let's take some time now to learn about game design a tad. Okay, let's take Donkey Kong Country it's a great graphical game and it's very fun. But some say that it has bad game design, in a sense it does you collect things that have no real value, does nothing special for the player. Let's look at Super Mario Bros.
You collect coins to give the player an extra life and a higher score, you collect green mushrooms to also get a quick extra life, your main attack is jumping and you must avoid falling off the platforms, there is lots for the player to discover on their own, you finally collect big mushrooms to be stronger and if your lucky a flower to get a sort of hippy firepower. That's a good outlook when designing a platform game, it's alright to loosely base design on other successful games but don't outright copycat it.
Now let's look at some Apogee games, Biomenace for example is like mario only more matured you have a gun but the basis for a good platformer is there. The game is based around the new advantage for the player as your gun is your survival. In space invaders, you do not collect things but it's a classic survival game, the arcade basis is survive as long as possible, get the highest score, with limited resources and powerups to temporarily allow prolonged survival through the hard parts. Let's look at an RPG, for example Final Fantasy is about your character growing and learning, it also has focus on the story.
There are no real levels as you build your character until he's the ultimate in the universe and then defeat the main baddy, so really it's you the man. Focus on the main area of your game and build around it, I have done this for awhile and generally my games are liked. Do not make the game too hard, and if you find it fun after millions of test phases it's good. If you the designer feel any lukewarmness around any part of the game, change it until you feel happy. try to imagine what a total stranger will feel! Design is as important as making it work, without design how will you create a game? You have nothing to go on except making it up as you go. Remember, before all else is gameplay, then multimedia, then story and finally test your game and put it all together. Play games to see what makes them fun!
Now, let's look at some operators. These are things that help us use math and logic in Dark Basic, they should be straightforward.
Operators
Now let's apply the above to condition statements.
In number crunching we'll see square roots and all the math commands in DarkBASIC but for now we'll just deal with the rnd command.
Here's the Syntax:
Unfortunately rnd runs from 0 to the set range value, I do not know how to change it I'm afraid but if you need help consider asking a question on the DarkBASIC forums: [http://www.thegamecreators.com The Game Creators]. This link takes you to the homepage of DarkBASIC I have no username on the forum but did before, but since everyone was too eager to cut each other's throats I stopped visiting. Follow the rules to a tee or they'll bury you, and make sure you know what you're posting and expect the worst. Sorry, for scaring you but sometimes it's the truth I've had lots of feelings hurt and been angered at the sheer cruelness of the users before. Now then let's make a guessing game!!
Note from a reader: You can get around this problem by simply using "value" as one less than the highest number you want to get to, and then adding 1, for example, if I want to generate a random number from 1 to 6 (like a standard 6-sided dice):
variable = rnd(5) + 1
We use the value of 5 because that is one less than 6 (the number that we want). This will generate a number from 0 to 5, and then add one, creating a number from 1 to 6. Hope this helps, and I would have asked before adding this, but I didn't see your contact info anywhere.
Wowzers! You started off programming hello world and now you've made a guessing game! Keep up the good work, if you're brave enough to show off your program at the newcomer's corner at the DarkBASIC forums. Now what does gosub do? Gosub stands for Go Subroutine, it's just a shorter geekier way of saying it. You need to have a label to jump too, and the label must be declared like so.
Note from another reader: I have noticed that if you write the following line
The game will choose a number, and then increase it until it reaches 10. All you have to do is to press 10 when you're asked for the number and you won. To correct the problem, write the program as this:
Now the game will only choose a number from 1 to 9, all you have to do next is to quess it.
Note from another reader: I found that there is an easier way to do the correction above. Dbtutor wanted a number one through ten and your correction only brought the number through 1 to 9. Also because of the way you wrote this, the probability of getting a 1 is higher now than getting any other number. An easier way to fix this is like this...