BASIC

Author Topic: Help Learning to code HELP  (Read 2868 times)

Offline Mikewas

  • Mc. Print
  • *
  • Posts: 6
    • View Profile
Help Learning to code HELP
« on: 2010-Jul-15 »
I am using the tutorials in the GlBasic app and I understand all of it exept when the section first game hits and the part with subs and jumpmarks. I dont really see a use for jumpmarks with the way it is explainded and I dont uderstand subs at all and I would like to code more complex games but I feel like these things are holding me back so if any one can elaborate so I can start programing I would appreciate it Thank You


PS and I understand arrays but dont really know how to implent it my programs


PLEASE HELP ME  :help: :help:

MrTAToad

  • Guest
Re: Help Learning to code HELP
« Reply #1 on: 2010-Jul-15 »
SUBroutines and FUNCTIONS are used to split code into easy-to-read sections that could be used repeatedly, perhaps in different parts of your code.  Aside from saving you time having to re-type the same code over and over, it allows code to be debugged easier, smaller code and probably faster too.

Hence repetitive code is idea for these.

The only real difference between SUB and FUNCTION is that the latter can take parameters, whilst the former can't.

So, if you want to produce rows of X's, you could do :

Code: GLBasic [Select]
FOR x%=1 to 100
FOR c%=1 to 10
DEBUG "X"
NEXT
DEBUG "\n"
NEXT
 

or you could do it :

Code: GLBasic [Select]
FOR x%=1 to 100
GOSUB printX
NEXT

SUB printX:
  FOR c%=1 to 10
     DEBUG "X"
  NEXT
  DEBUG "\n"
ENDSUB
 

Offline Mikewas

  • Mc. Print
  • *
  • Posts: 6
    • View Profile
Re: Help Learning to code HELP
« Reply #2 on: 2010-Jul-15 »
Thank You but I just dont know when to use it I guess and any help with the game tutorial and arrays?

MrTAToad

  • Guest
Re: Help Learning to code HELP
« Reply #3 on: 2010-Jul-15 »
Arrays are simple to use.  You can either treat them as a list :

LOCAL array%[]

or a fixed size :

LOCAL array%[]; DIM array%[5]

You cant just do LOCAL DIM array%[5]

Then you can either push data into the array using DIMPUSH array%[],4 or directly using an index : array%[4]=4

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Help Learning to code HELP
« Reply #4 on: 2010-Jul-15 »
For starters, don't worry about 'SUBS' and only focus on 'FUNCTIONS'.
'FUNCTIONS' can do everything a 'SUB' can do, but can optionally accept parameters.

You create a new 'FUNCTION' when you see a pattern in your code, and you figure you can shorten it (or simplify it) by moving some common functionality out of your current code block into a separate block that will have a particular purpose.

Consider this:
Code: GLBasic [Select]
// Main Loop
WHILE running = TRUE
  // Rotate player '10' degress
  player_angle = player_angle + 10.0
  // Make sure new player angle is within the range of (0 to 359.99999)
  IF player_angle < 0.0 then player_angle = player_angle + 360.0
  IF player_angle >= 360.0 then player_angle = player_angle - 360.0

  // Rotate enemy &#39;-20&#39; degrees
  enemy_angle = enemy_angle - 20.0
  // Make sure new enemy angle is within the range of (0 to 359.99999)
  IF enemy_angle < 0.0 then enemy_angle = enemy_angle + 360.0
  IF enemy_angle >= 360.0 then enemy_angle = enemy_angle - 360.0
WEND
END
 

You then notice that for both the player and the enemy angle, you want to ensure that the new angles are within a certain range.  Both checks look very similar, so you decide to create a 'FUNCTION' to do the range checking for ANY angle, such as:
Code: GLBasic [Select]
// Main Loop
WHILE running = TRUE
  player_angle = player_angle + 10.0
  player_angle = Angle_Clamp(player_angle)

  enemy_angle = enemy_angle - 20.0
  enemy_angle = Angle_Clamp(enemy_angle)
WEND
END

FUNCTION Angle_Clamp: angle
        // Ensure &#39;angle&#39; is not below 0.0
        WHILE angle < 0.0
                INC angle, 360.0
        WEND
        // Ensure &#39;angle&#39; is below 360.0
        WHILE angle >= 360.0
                DEC angle, 360.0
        WEND
        RETURN angle
ENDFUNCTION
 

The function accepts as a parameter the angle you want to clamp, and returns the new clamped value.
Now, your main loop is much cleaner and easier to read.
And, you now have a useful function to use from now on anytime you want to ensure an angle is in a valid range.
« Last Edit: 2010-Jul-15 by Slydog »
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline okee

  • Dr. Type
  • ****
  • Posts: 324
    • View Profile
Re: Help Learning to code HELP
« Reply #5 on: 2010-Jul-19 »
Like Slydog said concentrate on functions. They are very useful for reducing
the amount of code you have to write and also making your program more readable
and easier to debug.

In the example below we need to reverse a persons name so it's displayed backwards
The best way to do this is create a function and do all the necessary hijinks and then any time
in our program we need to reverse a persons name we just call ReverseName$("The name of the Person")
As you go along any useful functions you create you can add to an include file and add it to
your project for any new games you create, and you can use them easily without having to write all the code
again.


Code: GLBasic [Select]
// main program loop
WHILE KEY(1) <> 1

PRINT ReverseName$("George Bartholemue The Third"),0,10
PRINT ReverseName$("John Doe"),0,20
PRINT ReverseName$("Larry"),0,30

SHOWSCREEN

WEND

// A function to reverse the string passed to it
FUNCTION ReverseName$: TheString$

        LOCAL LengthOfString //holds the length of the string passed to the function
        LOCAL n
        LOCAL StringReversed$ // the finished product  the name reversed
       
        // Get the length of the string
        LengthOfString = LEN(TheString$) -1
        // loop through the string in reverse and copy each letter
        // to the StringReversed$ string
        FOR  n = LengthOfString TO 0 STEP -1
                StringReversed$ = StringReversed$ + MID$(TheString$,n,1)
        NEXT

        // return the reversed string
        RETURN StringReversed$

ENDFUNCTION

 
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)