DarkBASIC Programming/Fundamentals of DarkBASIC

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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


Rem we can also use a ` to comment quickly I will do that
Rem from now on!

`Declare some variables
a = 1
b = 0

`The conditional statement
if a or b = 1
   c = 1
else
   d = 1
endif

if c = 1 then Print "Condition C is Met"
if d = 1 then Print "Condition D is Met"
wait key
end

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

Rem Clear the Screen
cls
Rem String Data
"Wikibooks rock!"
"My name is John Smith"
"Look I'm a string!"
Rem the number below is a real number or a floating point decimal
35.5555
Rem the number below that is an integer number
12345
wait key
end

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

string$ = "Hey I'm a string and I love it!"
float# = 333.445
integer = -11111

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?


Remstart
Input Example
By Dbtutor
Yes you can have groups of comments
Remend

Input "Insert Name Here ",name$
Print name$;" is your name"

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.

Input a$

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!

Dim arrayname(arrayvalue,arrayvalue2(optional),arrayvalue3(optional)
arrayname(arrayvalue) = value

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

dim list$(10)
list$(0) = "Milk"
list$(1) = "Bread"
list$(2) = "Whatever"
Etc...

A bad dim statement

dim list$(10)
list$(1) = "Whatever"
list$(11) = "This is a no no - you can't exceed the array declared value!"

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!

                                Quiz #2

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

> is Greater Than
< is Less Than
>= is Greater than or equal to
<= is Less than or equal to

Now let's apply the above to condition statements.

`10 = 9 is not 1 it's 0 
`So no text will be printed
if 10 = 9 then print "That'll never happen"
`But 10 is greater then or equal to itself
if 10 >= 10 then Print "This will always happen"
`10 is greater then 8 
if 10 > 8 then Print "10 is bigger then 8 no doubt"  
Etc.

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:

variable = rnd(value)

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.




`Guessing Game Example
`By Dbtutor

cls

`Ask the users for their name
Input "Enter your name before you play! ";name$
wait key
cls

`Create a random number let's change the range a bit
value = rnd(9)
if value = 0 then value = 1
if value = 1 then value = 2
if value = 2 then value = 3
if value = 3 then value = 4
if value = 4 then value = 5
if value = 5 then value = 6
if value = 6 then value = 7
if value = 7 then value = 8
if value = 8 then value = 9
if value = 9 then value = 10

gosub guess

`Take a guess!
guess:
Input "Take a guess ";name$;"! ";guess
gosub checkguess

`Check the guess
checkguess:
if guess > value
  wrong = wrong+1
  Print "Sorry too high!"
  wait key
  cls
  gosub guess
endif

if guess < value
  wrong=wrong+1
  Print "Too low!"
  wait key
  cls
  gosub guess
endif

if guess = value then gosub win

`You won!
win:
cls
Print "You rock you've won the game!"
Print "You were wrong ";wrong;" times"
Print "Your guess was ";guess
Print "Press Any Key"
wait key
cls
end

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

value = rnd(9)
if value = 0 then value = 1
if value = 1 then value = 2
if value = 2 then value = 3
if value = 3 then value = 4
if value = 4 then value = 5
if value = 5 then value = 6
if value = 6 then value = 7
if value = 7 then value = 8
if value = 8 then value = 9
if value = 9 then value = 10

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:

`Guessing Game Example
`By Dbtutor
`modified by Huruba Dumitru Alin

cls

`Ask the users for their name
Input "Enter your name before you play! ";name$
wait key
cls

`Create a random number let's change the range a bit
value = rnd(9)
if value = 0 then value = 1

gosub guess

`Take a guess!
guess:
Input "Take a guess ";name$;"! ";guess
gosub checkguess

`Check the guess
checkguess:
if guess > value
  wrong = wrong+1
  Print "Sorry too high!"
  wait key
  cls
  gosub guess
endif

if guess < value
  wrong=wrong+1
  Print "Too low!"
  wait key
  cls
  gosub guess
endif

if guess = value then gosub win

`You won!
win:
cls
Print "You rock you've won the game!"
Print "You were wrong ";wrong;" times"
Print "Your guess was ";guess
Print "Press Any Key"
wait key
cls
end

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...


<pre>
`Guessing Game Example
`By Dbtutor
`modified by Huruba Dumitru Alin


cls

`Ask the users for their name
Input "Enter your name before you play! ";name$
wait key
cls

`To solve the 0 being a value problem just add 1 instead 
value = rnd(9)+1
`the value down here is now no longer needed making the program less sloppier
`executes the command better now.

gosub guess

`Take a guess!
guess:
Input "Take a guess ";name$;"! ";guess
gosub checkguess

`Check the guess
checkguess:
if guess > value
  wrong = wrong+1
  Print "Sorry too high!"
  wait key
  cls
  gosub guess
endif

if guess < value
  wrong=wrong+1
  Print "Too low!"
  wait key
  cls
  gosub guess
endif

if guess = value then gosub win

`You won!
win:
cls
Print "You rock you've won the game!"
Print "You were wrong ";wrong;" times"
Print "Your guess was ";guess
Print "Press Any Key"
wait key
cls
end

This is a much easier way to take 0 out of the options of random numbers while not affecting your probability at all. In the future, if you need a random number then use

value = rnd(x)+1 
'with x being one less than you want to go to, I hope that helped.




gosub mylabel

mylabel:

There you are, experiment a bit with labels to see what they can do. You can return to the last label you leaped to by using the return command.

Assignment

Show off your work at the forums(optional)!

Make the hints more precise.

Give a limited number of lives.

Okay that's a day, go take a nap or something lol! :D


I think I said the lesson would end with Day 7, I was wrong. Now let's learn about functions. Functions are used to make your own commands that are absent from DarkBASIC. They are also used to perform functions isn't it obvious. DarkBASIC has no object oriented programming or OOP so you can use functions to substitute if you are a very decent programmer. So how do you use a function?


Note from reader: DarkBASIC Professional supports OOP with user defined types and with pointers.


Here's a Simple Function:

hello_world(1)
wait key
end

function hello_world(1)
  Print "Hello World"
endfunction

Note from a reader: The above code includes a parameter to the function that is technically illegal in the BASIC language! The (1) following the function name should produce an error because the '1' would be considered to be the parameter's variable name and variable names can not start with a numeral. For DarkBASIC to allow such errors through promotes sloppy programming and would confuse new programmers if they were to 'upgrade' to more professional BASIC programming languages such as Microsoft's Visual BASIC (Ok, semi-professional!) Also, given the nature of the function, no parameters are needed so the correct code should be as follows:

hello_world()
wait key
end

function hello_world()
   Print "Hello World"
endfunction

Functions go at the end of your program and if your program runs into a function declaration beware! You can use exitfunction to exit a function easily. The brackets that follow a function's name can contain a list of parameters required by the function. The list should be made up of the variable names to be used for each parameter. Let's say we would like to place the "Hello World" message anywhere on the screen. We could provide the x & y coordinates as parameters to the hello_world() function as follows:

hello_world(20,10)
wait key
end

function hello_world(x,y)
   set cursor x,y
   print "Hello World"
endfunction

In this example you can see when we called the hello_world function at the start we provided 2 numbers. When the function is called, these numbers are placed into x and y so that x = 20 and y = 10. These parameter variables are local only to the function which means they will not change any other variables of the same name that are outside the function (unless those variables are made global which starts to make this introduction to functions get more complicated so we will leave that until later). The best way to use functions is by creating a library with your own commands/procedures. For example, you can make your own output command by modifying our hello_world function:

function output(x,y,msg$)
   set cursor x,y
   print msg$
endfunction

Using this you can in a sense use Object Oriented Programming, if your a more advanced user.

Example of a Library in DarkBASIC

`Basic Function Example

#include "myfunctionlib.dba"
x = 100
y = 10
hello_world(x,y)

`Now we have our own command!

Rem #####################################################################################

`Note the example below was not included in the above program it was in a different file named my
`functionlib.dba
`A little function library

function hello_world(x,y)
set cursor x,y
Print "Hello World"
endfunction

The #include command in VC++ works like so:

#include <iostream.h>
using namespace std;
Etc.

In DarkBASIC a library works similarly but not exactly like the C++ example. Now you've learned what a function is and does!

Assignment:

Use functions and libraries to make a Mad Libs game. Make your own command, like one for adding and so on.


This will probably be the last day in the life of lesson 3. We have been doing most of the time application and software programming. But soon enough we will write a game per day starting with Pong going to BreakOut and more. Unfortunately we won't write a Role Playing Game, for development of a role playing game takes months and roughly 5,000 or so lines of code including tools we need to create.


The Design Process of Software Is Like So

The Software I want to Develop Is
It's Main Purpose Is
Problems That Need to be Solved
Etc.


In fact there's a method that involves a series of pictures that I cannot recall and another method similar to the one above. I hope you learned something about DarkBASIC programming during the course of Lesson 3. Day 9 will continue in the next lesson...