Casio Basic Tutorial
#1
Posted 28 November 2005 - 04:37 PM
This tutorial was made to provide an in-depth overview of the Casio BASIC programming language. Many Casio sites offer a basic tutorial for game programming (using Getkey & Locate) where as this tutorial is meant to cover not only those but every other aspect of Casio game programming. This tutorial does not cover the mathematical uses of calculator programming except when it relates to a game. When you have completed this tutorial you will be able to create basic games of your own, and with practice much more advanced ones.
Note by me:
Please be aware that the // signs and the text beyond are comments. However, they are not supported by the calculator, so do not type them in! If you want to add comments to your programs then use the ' chars as prefix.
Return to Tutorial Index
- Rudy and foroplus like this
#2
Posted 28 November 2005 - 04:41 PM
I really shouldn't have to explain this, but if I don't I know someone will ask. To create a new program press MENU' /> then locate the program icon and hit EXE' />, press F3' /> to create a new program. You will then be asked to enter a name for your program, also you may create a password at this time (WARNING: if you put a password on your program you will not be able to use the debugger).
Return to Tutorial Index
- Rudy likes this
#3
Posted 28 November 2005 - 04:42 PM
A variable is something which holds a value. The most commonly used are the letters A~Z and r,_. There are others, but they are not covered in this section. To store a value into a variable you must use the key, like this:
1->A // Assigns 1 to A
To assign values to more than one variable at a time (Algebra FX2) press OPTN' /> F5' /> F5' /> F4' /> then select ~, On the CFX calcs hit Alpha' /> then select ~ from the menu.
It should look like this:
1->A~D // Assigns 1 to A B C & D
Variables are the most important aspect of game programming, without them it is almost impossibly to make a game (You could make a very simplistic guess the number game, but why bother).
Return to Tutorial Index
- Rudy likes this
#4
Posted 28 November 2005 - 04:43 PM
Your calculator comes with a very simplistic debugger, or error finder (historical note: its called debugging because back in the dawn of computers roaches would crawl into computer vacuum tubes and cause problems, then someone would have to go get them out, hence the term debugging). To use the debugger run the program, then on an AFX2 Press ESC' /> when an error message appears or on the CFX hit left or right when you get an error. The debugger will take you to the place where the error occurred so that you can fix it. Warning, the debugger sometimes reports an error to be in one place, but what caused the error might be in a different place. If you can't find anything wrong at the place the debugger took you to go up a few lines and see if something up there is causing an error.
Return to Tutorial Index
- Rudy likes this
#5
Posted 28 November 2005 - 04:44 PM
Loops are used to repeat blocks of code. There are several types of loops in Casio BASIC: Goto/Lbl, Do/lpWhle, For/Next, and While/WhileEnd. Here is an example of a Goto/Lbl loop:
1->A //Assigns 1 to A Lbl 1 A+1->A //Adds 1 to the variable A and stores new value Goto 1 //Returns to Lbl 1
This loop will execute infinitely, Adding 1 to a each time (Actually it will stop when the value of A is greater than 99*10^99 because you will get a mem error).
Here is an example of a For loop note that it doses not take as many lines.
For 1->A to 100 Step 1 //this loop assigns 1 to A then adds 1 to A until it equals 100 Next //Goes back to the start of the loop, adds the step to the variable.
Anything between the For and next statement will be executed until the expression evaluates true (A equals 100). By changing the value of Step you can change how much A is incremented by.
The While loop checks to see if the expression is true then executes the code. After the code has been executed it returns to the top, checks the expression, and if it is false jumps out of the loop and continues with executing the program. This is an example of a While loop.
1->A While 1=1 //1 always equals 1 so the expression always evaluates to True A+1->A WhileEnd
Since the while loop evaluates the expression before executing the code it is possible that if the expression is false before loop begins (ex: 1=2) then the loop will never occure, it will just skip right over the code and continue with the program.
Unlike a while loop a Do/LpWhle loop will always execute at least once since it evaluates the expression after the code has been executed. A Do/LpWhle loop looks like this.
1->A Do //Start of the Do/LpWhle loop A+1->A LpWhle A<100 //Loops while A is less than 100
Return to Tutorial Index
- Rudy likes this
#6
Posted 28 November 2005 - 04:45 PM
Selection Statements are used to make programs have differing outcomes, instead to executing the same way every time they are run. A selection statement checks an expression, sees if it is True or False, then if True executes the rest of the statement otherwise it skips to below the statement and continues with execution.
There are two types of selection statements on the calculator, but one can only be used on models before the AFX series. They are the If/Else statement and the => arrow.
An If/Else statement works like this
1->A If A=1 //expression to be evaluated Then “HI” //result if expression is true “HOW ARE YOU” Else “BYE” //result if expression is false “SEE YOU LATER” If End //end of statement
Result:
Since A is 1 the statement evaluates to true, therefore
HI
HOW ARE YOU
Is printed. If you replaced the first line with 0->A then the if statement would evaluate to False, and
BYE
SEE YOU LATER
Would be printed.
An If statement can contain many different things, and can be many lines long, they are the keystone to making a game, and before going on you should feel comfortable using them.
The => arrow is also very useful, it is a single line selection statement that takes up less space but can do less than a normal if/else.
This is the same code as above, except using the => arrow instead of the if statement.
1->A A=1=>”HI” A=1=>“HOW ARE YOU” A<>1=>”BYE” // <> means not equal to A<>1=>”SEE YOU LATER”
Neat huh? As you can see, the => arrow provides cleaner looking code, and occasionally takes up less space than the if statement. In general, the best time to use a if statement is when you have at least to lines of code to be put inside, otherwise use the => arrow.
Return to Tutorial Index
- Rudy likes this
#7
Posted 28 November 2005 - 04:47 PM
Now that you have a good understanding of both loops and selection statements I will go into the more advanced features of both.
First off we’ll talk about nesting. Nesting is when you take one loop and put it inside another. Here is an example:
0->C For 1->A To 10 //Step can be omitted instead of using Step 1 For 1->B To 10 A+B+C->C Next Next C_ // the _ represents the output sign, it displays whatever is before it and Pauses until [EXE] is pressed.
By executing this code you will get this output:
1100
Disp //caused by _
Now we will go through the code and look at what each line does.
Line 1: 0->C
Assigns the value 0 to the variable C
Line 2: For 1->A To 10
Tells the program that you will be looping until A is equal to 10, adding 1 with each loop.
Line 3: For 1->B To 10
Tells the program that you will be looping until B is equal to 10, adding 1 with each loop.
Line 4: A+B+C->C
The current value in A will be added to the current value of B, that value is then added to the current value of C and then assigned to C.
Line 5: Next
Goes to line 3.
Line 6: Next
Goes to line 2.
Line 7: C_
Displays the final value of C.
As you can see, the second loop executes completely for each iteration (loop) of the first loop.
Nesting also applies to selection statements, you can nest if or => inside each other as needed.
Example:
A<1=>A>0=>”HI” If A<1 Then if A>0 Then “HI” End If End If
Return to Tutorial Index
- Rudy likes this
#8
Posted 28 November 2005 - 04:51 PM
As you have just seen, there are times when you will want more than one condition in a selection or loop, you can nest the statements or you can use a logical operator: And, Or, Not.
And and Or are operators which can be used in selection statements to specify additional conditions, therefore the code above could be written:
A<1 and A>0=>”HI”
Instead of:
A<1=>A>0=>”HI”
The Or operator allows you to specify alternate conditions which are evaluated independently and if any are true then the entire statement is considered true. This means you can do this:
If A=1 Or B=1 Then “TRUE” End If
When this executes it will print TRUE if A or B equals 1. Handy huh?
The Not operator can also be used in selection statements, though it is not used nearly as often. Not returns False if the statement is true (or 1) and True if the statement is False (or 0) so:
If Not (A=1) Then “TRUE” End If
This will print true if A is any number but 1. In this case the Not statement is used as an <> (not equal to) statement, but it has other uses. We will cover more on this later.
Return to Tutorial Index
- Rudy likes this
#9
Posted 28 November 2005 - 04:53 PM
I have already done some stuff with text display as you saw above, putting anything in “quotes” causes it to be displayed to the screen. However there are other ways to display text, and each has its good points and bad points.
The “”s:
Quotes displays what ever is inside it on the left hand side of the screen, and automatically adds a return to the end, this means that the start of each new " begins on a new line, there is no way to prevent this. If the screen is full and you are trying to display more text then " will cause the screen to scroll, giving you a new line to put text on.
The Locate command:
Locate is used in text based games, it allows you to place text anywhere on the screen using x,y coordinates. Locate does not cause the screen to scroll since you cannot place anything below the 7th line, if you do you get an error. Also locate does not wrap onto the next line so if your text goes off the screen it wont show up on the next line. With locate you must be careful not to exceed the boundaries of the screen:
1,1
______________________
|000000000000000000000|
|000000000000000000000|
|000000000000000000000|
|000000000000000000000|
|000000000000000000000|
|000000000000000000000|
|000000000000000000000|
--------------------------------- 21,7
Syntax for locate:
Locate x,y,”YOUR TEXT IN QUOTES HERE”
Placing anything outside these boundaries will result in an error.
The Text command:
Its important not to get text and locate mixed up, text does the exact same thing as locate except that it places the text on the graph screen (right next to that nice parabola you made in math).
An important note about the text and graph screen, they are totally independent of each other, so you can switch back and forth between the two without messing up the other one, this can come in handy in games.
Syntax for Text:
Text y,x,”YOUR TEXT IN QUOTES HERE”
Please note the juxtaposition of the X and Y values, when using Text the Y value comes first and the X value second. Also, you cannot exceed the boundaries of the screen with Text either, though they are different than those of locate since Text goes by pixels (the little dots that make up your screen). Here are the Text boundaries:
1,1
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….…….
……….……….……….……….……….……….……….……….……….……….……….……….……. 63,127
Remember, x and y are reversed.
If you took the time to count that you would get 8128 pixels, meaning you could place your text in any one of those places, as opposed to locate’s 147 places. As you can see, text gives us a lot more control than locate, but each has their uses.
Some other useful text functions:
ClrText: clears all the text on the text screen.
Cls: clears the entire graph screen.
ClrGraph: clears the graph and sets the view window (covered later) to its default.
Return to Tutorial Index
#10
Posted 28 November 2005 - 04:54 PM
In any game you will want to get information from the player, otherwise they might as well be watching a movie. The simplest way to get input from the user is to use the ? command.
Syntax: ?->(Variable)
Description: this command causes a ? to appear on the screen, execution pauses until the user enters data and hits EXE' />, then the data is put into the variable. This is a good way to ask yes or no questions, with the user having to enter 1 for yes and 0 for no.
Another way you can use the ? command is to put a prompt before it: “Continue (1=YES 0=NO)”?->C
This causes the text to be displayed with a ? after it and waits for data.
Return to Tutorial Index
#11
Posted 28 November 2005 - 04:54 PM
In some games you will want to know what key the user is pressing, to do this you must use the Getkey command.
Syntax: Getkey->(variable) or if Getkey=(key number)
Description: Getkey returns a different value for each key pressed, if no key is pressed then it returns 0, here is a simple program used to display the value of a key.
Lbl 1 Locate 1,1,Getkey Goto 1
The only key which does not return a value is AC/On.
Return to Tutorial Index
#12
Posted 28 November 2005 - 04:55 PM
To better help you understand the first few lessons I have written a sample program so you can see how everything works.
Lbl 0 ClrText Locate 9,4,"CATCH" Locate 1,7,"BY: JAKE FINLEY" Do LpWhile Getkey<>31 ClrText For 1->T To 7 Locate 15,T,"I" Next 7->A -1->S 0->V Locate 16,1,"POINTS" Lbl r Isz S Locate 16,2,S 1->r Int 14Ran#+1->o Lbl 1 Locate o,r,"o" Locate A,7,"> <" Locate o,r," " Locate A,7," " Getkey=27=>A<12=>A+1->A Getkey=38=>A>1=>A-1->A Isz r r=7=>o>=A And o<=A+2=>A+1->o r=8=>o=A+1=>Goto r r=8=>Goto 2 If Getkey=79 Then Locate 16,4,"VORTEX" 1->V IfEnd V=1=>o<A+1=>Isz o V=1=>o>A+1=>Dsz o Goto 1 Lbl 2 ClrText Locate 7,4,"GAME OVER" Locate 1,7,"SCORE:" Locate 8,7,S
Return to Tutorial Index
#13
Posted 28 November 2005 - 05:01 PM
ok, if you have been reading this tutorial all the way from the begining you will know understand the concept of loops, this chapter deals mostly with with nesting loops.
A simple loop:
While A<>B WhileEnd
A nested loop:
While A<>B Do LpWhile A=B WhileEnd
get it? a nested loop is just a loop inside of a loop, just like a nested if. what most people have trouble with is the execution of nested loops, if you have programming experiance then you will all ready know this but if your new to field take a look at the sample below:
Lbl 1 1->A 1->B 1->C //counter var 1->D //counter var Do A+B->A While D<=5 //loops 5 times B+A->B Isz D WhileEnd 1->D Isz C LpWhile C<3 //loops 3 times A+B_
Go through this program and see if you can figgure out what the output will be, then look below for the answer.
if you 610 got as the output then you did this right, if not then you fell pray to a mistake that many begining programmers make (I took a C++ programming class and some people had all sorts of programs with this). If you did get this wrong its because you do not understand one of the basics of nested loops:
for those of you who are looking at me like I'm stupid: trust me, some people will make this mistake, I have seen highly intelegent people make this mistake so I thought I'd save others the trouble (and frustration).PRIMARY RULE: an inner loop executes fully for each interation (loop) of an outer loop
You can nest as many loops as you want, I dont know if there is an upper limit on the amount you can nest but I cant see you ever needing to worry about it.
Now you should know what the break statement is, but if you dont I'll review: Break terminates execution of a loop and resumes normal program flow at the end of that loop.
While A=A //endless loop Isz A Break "THIS ISN'T SHOWN, IT ISN'T EVEN LOOKED AT BY THE PROGRAM" "NEITHER IS THIS" WhileEnd "ESCAPE FROM THE LOOP" //Break goes to here
now we will look at how Break works in nested loops
While A<>B Do Break LpWhile A=A //Break goes to here WhileEnd //loops like normal
As you can see, Break only exits the current loop, and when that loop comes around again and if Break isn't executed (if its in an if statement) then the loop does not terminate and continues like normal. Get it?
Return to Tutorial Index
#14
Posted 28 November 2005 - 05:03 PM
Those of you who know other programming languages have probably seen a big problem in what you have read so far, what if you need more than 28 variables in your game? A long time ago (in a calc far, far away), when I was first learning to program I was making little games like Example 1 of this tutorial. But then I started trying to make some more complex games and I ran into a problem, I needed more variables and there were none to be found! Eventually I found the answer: Lists and Matrices.
Lists – A list is exactly that, a list of variables, they can be up to 255 cells long.
Here is a visual of a list with 5 cells:
[ ]
[ ]
[ ]
[ ]
[ ]
to access a cell in a list you just use List #[#] (List 1 ), the first # represents the number of the list and the second represents the index of the cell. You can use Lists just like you use normal variables, anything you can do with a letter variable you can do to a list, you just have to specify what list and what cell.
Some special list functions:
X->Dim List # : creates a list with X cells in it, all cells are set to 0.
Fill(X,List #) : fills a list with X.
There are others, but you probably wont use them.
Matrices – A matrix is a two dimensional list
Here is a visual of a 5x4 matrix.
[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
Matrices are just like lists except that they are two dimensional, I like to use them for maps, especially tiled maps. To use a matrix use Mat (letter A~Z)[Y,X] (Mat A[1,1]), the X and Y are reversed, make sure not to confuse them.
Special Matrix functions:
{Y,X}->Dim Mat (letter) : creates a YxX matrix
Fill(#,Mat (letter)) : fills a matrix with a #
Again, there are more, but you wont need them right now.
Tip: List and Matrices are very useful for loops, since they are indexed you can loop through them or use List 1[A] so you can change what list cell you are using dynamically. Also note that you cannot do this with the number of the list.
Return to Tutorial Index
- PsySc0rpi0n, frankmar98 and GoncaloR4mos like this
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users