Author Topic: First steps in coding for newbies  (Read 8403 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
First steps in coding for newbies
« on: July 12, 2009 »
    [Edit] As pointed out by a forum member, I have refered to an older version of Freebasic, 2.0 is not the latest version, I will update this tutorial when and if I get time, if anyone has any difficulty following this tut, post it here, please dont PM me about it, I have my hands full at the moment ... This goes for every other post on this forum, it is impossible to maintain and update them all because there are over 50,000 posts, if anyone has a problem, post it in the thread and you can get help that way. thanks.[/edit]


    Part 1:

    How to install Freebasic 2.0 with fbedit, tinyptc ext++ and ufmod.

    This is a beginners guide to getting Freebasic actually installed on your computer ready for action!
    The series of simple tutorials that will follow this will talk you through some simple concepts and end up with you making some nice things.

    Freebasic sadly hasn't been updated for a while but it is still a really great language for you to learn with. In fact, many of the things you see in todays demos can all be achieved with a little bit of thought in Freebasic.

    The things we need to use Freebasic 2.0 , tinyptc_ext++ and ufmod are:

    • The Freebasic Binaries.
    • A Code Editor.
    • Tinyptc_ext++



    Step 1:

    So let's download the stuff we will need.

    Please download all this stuff to your desktop, just download it, don't extract anything yet!

    First of all we'll leech freebasic..
    Download this;
    FREEBASIC 2.0

    Next we need to leech Fbedit (a pretty nice code editor as it goes):

    FBEDIT

    Next we need to grab Tinyptc_Ext and ufmod extension from;

    TINYPTC EXT++

    Please note that you have to be logged into your account to download it. You will find the file attached to the bottom of the first post.



    Step 2:

    Installing Freebasic 2.0

    • Look on the desktop. There is a white circular icon with a drawing of a horse on it.
    • Double click this to start the installation process.
    • After you have agreed to the agreement etc you will come to a box that allows you to choose your destination folder, make a note of where it is going and just click next to continue the setup.
    • Please wait while the files extract themselves into the right places.






    Step 3:

    Installing FBEDIT

    • The Fbedit file is in a zip file on your desktop.
    • Click on it with the *right mouse button* and choose the option that says:
      Extract to Fbedit/ .. Allow it to extract the files.
    • A directory called FbEdit will have been created on your desktop.
    • Go into that directory and inside it you will find a file called Fbedit.exe this is what you need to click to launch Fbedit, double click it to set it up.
    • A prompt will come up to ask you to specify the locations for freebasic.
    • You can safely leave the project path as it is.
    • The compiler path should point at the directory you made a note of when installing freebasic
    • The help path should be the same with \Help at the end



    Step 4:

    Installing PTC_EXT and ufmod..

    • Right click on the archive called Tinyptc_ext++ and extract to Tinyptc_ext++
    • You need to cut the two files;
      libmmx.a
      libtinyptc_ext++.a

      and paste them into the lib\win32 folder of your freebasic installation, the path probably looks similar to this:
      C:\Program Files\FreeBASIC\lib\win32
    • Go back to the desktop into the directory that was created when you extracted tinyptc_ext++
      Cut the file;
      tinyptcx_ext++.bi
      and paste it into the inc folder of your freebasic installation. The path looks something like this;
      C:\Program Files\FreeBASIC\inc
    [/i]
    [/list]

    Once it's installed in there you have FREEBASIC 2.0, FBEDIT, TINYPTC_EXT and UFMOD all installed so you will be ready to start working through the code examples that follow this post.



    Congratulations, you're ready to start coding!

    The next posts that follow this will contain lessons to teach you some programming concepts eventually leading up to you making some simple effects :)
    « Last Edit: September 19, 2009 by Shockwave »
    Shockwave ^ Codigos
    Challenge Trophies Won:

    Offline Shockwave

    • good/evil
    • Founder Member
    • DBF Aficionado
    • ********
    • Posts: 17412
    • Karma: 498
    • evil/good
      • View Profile
      • My Homepage
    Re: First steps in coding for newbies
    « Reply #1 on: July 17, 2009 »
    Part 2!

    First steps.



    The time has come to learn some boring (but essential) programming before we go onto the more fun stuff.

    I am going to take a procedural approach to this, some people would say you should take an object orientated approach, others still would reccomend that you learn assembly language so you can understand how a program is processed.

    I will be taking the procedural approach because it is simple enough to learn.
    You do not need to know the difference between these conventions at the moment because all we are going to be doing is learning how to count, how to do aritmetic, how to store numbers and text.

    I'll keep this to a minumum and then we can go onto drawing some stuff on the screen.



    Lets get ready to write our first program..

    First of all we need to open our editor so go into the FBEDIT directory and double click its icon to launch freebasic, we need to start a new program now so click on the "file" tab at the top.
    Then "new code file" from the menu that drops down.

    a window called "untitled.bas" will appear, this is the window where we will be doing our coding.



    From now on, everything you need to type I will be surrounding in
    Code: [Select]
    code boxes, you can highlight the things in these boxes and copy-paste into your editor if you want to save typing them yourself.

    The first thing you need to know are white space and comments

    There are two ways of doing comments in your freebasic listings.
    You can see them in the box below;

    Code: [Select]

    rem This is a comment
    '   And so is this!


    You need to know that the freebasic compiler ignores comments and white space, white space is just blank lines or areas of blank text, you can use white space to lay out your code neatly so it is readable, you would use comments in your code so that you can see what your code is doing.

    You should always comment your code, the rem command and the ' character are treated exactly the same by freebasic, they are ignored!

    So the first thing that you learned does nothing but is the most important thing that you will learn today.
    put as many comments into your programs as possible so you can go back at any time and see exactly what is going on.

    So let's put some comments in our program, it's a good idea to put what the program is going to do in the comments.

    Code: [Select]

    ' Program 01 "Hello World"
    ' Written by me on 17th July 2009.
    ' --------------------------------


    You will see that fbedit colours these comments in green when you type them in.
    In fact it's pretty useful like that, it colours different parts of your program according to what they do to make it more easy for you to read it.

    Here's the program you need to type then...

    Code: [Select]

    ' Program 01 "Hello World"
    ' Written by me on 17th July 2009.
    ' --------------------------------



    'Open an 800 pixel wide 600 pixel high screen
    Screen 19,,1,,

    'Print our message
    Print "Hello World!"

    'Wait 3 seconds (3000 milliseconds)
    Sleep 3000

    'Exit the program
    End


    There really is no time like the present so lets run it and see what happens..
    First we need to save the file.
    click "file" on the top of the screen.
    Then "save as"
    and call it:
    helloworld.bas
    You'll notice that the tab has changed its name to "helloworld.bas"

    We need to do a couple of things to run the program.
    The "make" tab at the top of the screen, click on that and select "compile".

    When it has compiled you will notice a file called "helloworld.exe" has appeared in the directory with the helloworld.bas file.

    This is your compiled program, you can either run it by double clicking on it or straight from fbedit by pressing shift and f5

    It opens a screen, (I am not going to go into details of this command as we will be using a different method for screens later on)
    Prints Hello World on the screen
    Waits 3 seconds
    Then exits



    It may not seem like a lot, but there are some concepts to learn here, the programs execution starts at the top line.
    The top line is white space which is ignored, so are the next lines (which are comments), the first command that is looked at is the screen command.
    It opens the screen
    the next command it comes to is the print command.
    It prints out our message on the screen.
    Then it comes to the sleep command.
    It pauses for 3000 milliseconds (3 seconds)
    then it encounters the end command
    The program is safely terminated.



    The concept that you need to grasp from the outset is that of the list, this is why programs are called listings, each item in the list is executed one after the other. just like going through any list and reading off each item one by one.

    This is as simple as it gets, clearly a program is just a list of instructions, the computer has an internal marker to keep track of where it is in the list, the list contains commands for the computer to carry out, it executes an instruction, moves the marker down the list to the next command and executes that, repeating the process until the list is finished.



    We'll look at two of the commands here.

    Firstly the print command.

    Only briefly because this is going to be used to output the results of some stuff we do in a little while.

    At its simplest, the print command is followed either by a variable or some alphanumeric text (letters, numbers and symbols).

    Our "hello world" message is surrounded in quotes "" You'll notice that fbedit coloured them red, the message within was coloured black and this is what got printed to the screen. There is another variation, we'll come to that in the next tutorial, but putting it simply, the message to be printed is contained within a pair of these quotation marks.

    We will use the print command in the next few tutorials, but we'll be moving on to better things as print is very limited really.


    The next one is the sleep command.

    It was necessary to specify a delay before the program ended otherwise it would have been all over before you had a chance to see it.

    You type the command;
    sleep
    followed by a delay in milliseconds
    1000 milliseconds = 1 second

    So.

    Here's one last example, apologies that this tutorial is very basic(pun intended), we will do some more interesting stuff next time!



    Your excercise is..

    Open a new source file
    type this listing;

    Code: [Select]

    ' Program 02 "Hello World 2"
    ' Written by me on 17th July 2009.
    ' --------------------------------



    'Open an 800 pixel wide 600 pixel high screen
    Screen 19,,1,,
    Dim As Integer a
    'Print our message
    Print "Hello World!"
    'Wait 1 second (1000 milliseconds)
    Sleep 1000
    'Print our message
    Print "This is my second attempt!"
    'Wait 2 seconds (2000 milliseconds)
    Sleep 2000
    'Exit the program
    End

    See if you can work out what it will do before you run it.
    Now modify it to add some more messages at the end.

    See you in the next one!
    Shockwave ^ Codigos
    Challenge Trophies Won:

    Offline Shockwave

    • good/evil
    • Founder Member
    • DBF Aficionado
    • ********
    • Posts: 17412
    • Karma: 498
    • evil/good
      • View Profile
      • My Homepage
    Re: First steps in coding for newbies
    « Reply #2 on: July 17, 2009 »
    Part 3

    Using variables

    The next concept we need to learn is variables.
    You may or may not know what a variable is, here's a good way to understand them.

    Imagine that you have an envelope, a sheet of paper is kept inside that envelope, we have a fruit shop and we need to keep track of how many apples we have for sale, let's pretend that there are 10 apples.

    On the envelope you can write the word "apples"

    This is so that you know what is stored inside the envelope.
    On the sheet of paper inside the envelope, you write the number 10.

    If you want to know how many apples you have, you can open that envelope with apples written on it and just examine the contents of the envelope, you'll see the number 10.

    A few important concepts here;

    Firstly we take a number that is meaningless on it's own and we assign it a name.
    Secondly we have a space (inside the enveolpe) where the number is stored.
    Thirdly, it should be possible to change the contents of that envelope if we sell some apples we can erase the number that is in the envelope and replace it with the new total, the number can vary.

    This is essentially the principle behind variables, they are areas of memory that are assigned names so we can refer to them easily, these variables are used to store various kinds of information, we can retrieve them at ease and manipulate the information that is stored inside them.

    Time for some code to display the simple use of variables.

    Code: [Select]

    ' Storing And Manipulating Numbers
    ' ================================


    'Open screen;
    Screen 19,,1,,

    'Set up some variables
    Dim As Integer apples
    Dim As Integer oranges

    'Set the variables
    apples=10
    oranges=6

    'Display the text
    Print "We have:";apples;" Apples for sale."
    Print "We have:";oranges;" Oranges for sale."
    Print "We have:";oranges+apples;" Fruits for sale."

    'pause for 5 seconds to give a chance to read it.
    Sleep 5000

    'exit the program
    end


    From top to bottom, skipping the commands we already know about.

    Dim As Integer apples

    the Dim command is short for "Dimension", it is the command we use when we want to declare a variable, so we type;

    Dim

    This is followed by the type of variable.

    As Integer

    An Integer is a whole number (with no decimal point), we can define many different types of variables, integers are a fast way of manipulating whole numbers. It is 32 bits long and is signed.

    The sign of a number determines if it is positive or negative, and integer can store numbers in the range;

    -2147483648  to 2147483648

    Which are pretty big and probably adequate for anything you want to do.

    You can also declare unsigned integers;

    Uinteger

    a Uinteger can store numbers between;

    0 to 4294967295


    Short

    Similar to an integer but can only store numbers between;

    -32768 to 32767

    Ushort

    Similar to a Uinteger but can only store numbers between;

    0 to 65365


    String

    This is an array of characters, it is used to store text, you can store between 0 and 2GB of characters in a string.


    Other variable types you can have..

    Single (Use Singles to store 32 bit numbers with decimal points)

    Double (Similar to Singles but much more precise and can store much larger numbers)

    These variable types are also known as Floating Point numbers because the decimal point can appear anywhere in the number... Eg:

    2.32423423
    343243.2346

    etc..


    So..

    Dim As Integer apples

    Tells the computer to create a variable called apples which will be used to store whole numbers.

    Dim As Integer oranges

    Similarly tells the computer to create a variable called oranges which will be used to store whole numbers.

    we use the equals sign = to put something into a variable.

    apples=10

    stores the number 10 in the variable apples.
    (just like writing the number 10 and putting it in the envelope with apples written on it).

    oranges=6
    Stores the number 6 in the variable called oranges.

    We can see in the next command one way it may be used..


    Print "We have:";apples;" Apples for sale."

    The text is displayed in double quotes ""

    Then we have a semi colon ;

    Then the variable name apples

    Then another semi colon ;

    Then the next bit of text.

    If you've run the program you will see that the output is not;

    We have: apples Apples for sale.

    Rather;

    We have: 10 Apples for sale.
    Print "We have:";apples;" Apples for sale."

    Remember that the text is enclosed in the quotes, then the semi colon seperates the text from the variable, it sees the word apples, as apples is not enclosed in its own set of double quotes, it knows that apples is a variable, it retrieves the apples envelope, fetches the number from inside it and displays that number.
    There is then another seperator ; and then some more text " Apples for sale"

    We do the same with the next line, the bottom line performs a simple arithmetic operation on the variables.

    oranges+apples

    Adds the numbers inside apples and oranges together.

    We can use various operators on variables and numbers, here are just a few of them;


    + = addition
    - = subtraction
    / = division
    * = multiplication

    (There are a lot more but these are enough for now).

    You might wonder what is the point of this, why not just write;

    Print "We have: 10 Apples for sale."

    etc...

    Well, if we had 20 oranges to start off with, we can just change the value in oranges at the start of the program..

    Code: [Select]

    ' Storing And Manipulating Numbers
    ' ================================


    'Open screen;
    Screen 19,,1,,

    'Set up some variables
    Dim As Integer apples
    Dim As Integer oranges

    'Set the variables
    apples=10
    oranges=20

    'Display the text
    Print "We have:";apples;" Apples for sale."
    Print "We have:";oranges;" Oranges for sale."
    Print "We have:";oranges+apples;" Fruits for sale."

    'pause for 5 seconds to give a chance to read it.
    Sleep 5000

    'exit the program
    end


    So just by altering one line of code, we radically change the output of the program.
    And what's more, we can do other things that are more interesting, for example we can alter the contents of the program during its execution, adding some other examples of how we can perform calculations, you should now be able to understand this;

    Code: [Select]

    ' Storing And Manipulating Numbers
    ' ================================


    'Open screen;
    Screen 19,,1,,

    'Set up some variables

    Dim As Integer apples
    Dim As Integer oranges
    Dim As Double apple_price
    Dim As Double orange_price
    Dim As Integer apples_sold
    Dim As Integer oranges_sold
    Dim As Double revenue


    'Set the variables
    apples=10
    oranges=6
    apple_price=.40
    orange_price=.55
    apples_sold=7
    oranges_sold=3

    'Display the text
    Print "We had:";apples;" Apples for sale."
    Print "We had";oranges;" Oranges for sale."
    Print "We had:";oranges+apples;" Fruits for sale."
    'pause for 3 seconds to give a chance to read it.
    Sleep 3000

    ' How many did we sell and how much did we make?
    Print "We sold:";apples_sold;" Apples and earned: (GBP)";apples_sold * apple_price
    Print "We sold:";oranges_sold;" Oranges and earned: (GBP)";oranges_sold * orange_price

    'pause for 3 seconds to give a chance to read it.
    Sleep 3000

    ' Calculate how much we made all together and how many of each we have left!
    revenue = (apples_sold*apple_price) + (oranges_sold*orange_price)
    apples=apples-apples_sold
    oranges=oranges-oranges_sold

    ' Display how it all ended up
    Print "Our total revenue was:";revenue
    Print "We have this many apples left:";apples
    Print "We have this many Oranges left:";oranges
    Print "We Ended up with:";oranges+apples;" Fruits left."
    'pause for 8 seconds to give a chance to read it.
    Sleep 8000

    'exit the program
    end

    A few things to point out.
    You'll see the word  Double  in some of those variable declarations, this means that these numbers can contain decimal points (essential for displaying currency! eg 1.20 etc.)

    You will see some calculations

    apples_sold * apple_price

    for example, this multiplies one by the other.

    revenue = (apples_sold*apple_price) + (oranges_sold*orange_price)

    The brackets make sure that the calculations are evaluated seperately (always use brackets this way to ensure that things are done properly...)

    An example of what this protects against..

    1 + 100 * 10
    would evaluate to;

    101 * 10 = 1010

    1 + (100 * 10)
    would evaluate to;

    1+ 1000 = 1001

    The rules of BODMAS are obeyed, here is the order that a mathematical expression is evaluated;

    Brackets, Of(indices), Division, Multiplication, Addition, Subtraction

    apples=apples-apples_sold

    the value stored in apples is equal to whatever is in it already minus the amount we sold.


    And finally...

    Let's introduce strings, it's very easy, you just have to define them as String.

    You'll see from the listing how to assign text to a string and display it.

    Code: [Select]

    ' Storing And Manipulating Numbers
    ' ================================


    'Open screen;
    Screen 19,,1,,

    'Set up some variables

    Dim As String your_name
    Dim As Integer apples
    Dim As Integer oranges
    Dim As Double apple_price
    Dim As Double orange_price
    Dim As Integer apples_sold
    Dim As Integer oranges_sold
    Dim As Double revenue


    'Set the variables
    your_name="Shockwave"
    apples=10
    oranges=6
    apple_price=.40
    orange_price=.55
    apples_sold=7
    oranges_sold=3

    'Display the text
    Print "Hello, ";your_name;" !"
    Print "We had:";apples;" Apples for sale."
    Print "We had";oranges;" Oranges for sale."
    Print "We had:";oranges+apples;" Fruits for sale."
    'pause for 3 seconds to give a chance to read it.
    Sleep 3000

    ' How many did we sell and how much did we make?
    Print your_name;", we sold:";apples_sold;" Apples and earned: (GBP)";apples_sold * apple_price
    Print "We sold:";oranges_sold;" Oranges and earned: (GBP)";oranges_sold * orange_price

    'pause for 3 seconds to give a chance to read it.
    Sleep 3000

    ' Calculate how much we made all together and how many of each we have left!
    revenue = (apples_sold*apple_price) + (oranges_sold*orange_price)
    apples=apples-apples_sold
    oranges=oranges-oranges_sold

    ' Display how it all ended up
    Print "Our total revenue was:";revenue
    Print "We have this many apples left:";apples
    Print "We have this many Oranges left:";oranges
    Print "We Ended up with:";oranges+apples;" Fruits left ";your_name;" And thats the end of this tutorial!"
    'pause for 8 seconds to give a chance to read it.
    Sleep 8000

    'exit the program
    end


    Btw, I will have to sort out the formatting of this text later.. Sorry.
    « Last Edit: July 17, 2009 by Shockwave »
    Shockwave ^ Codigos
    Challenge Trophies Won: