Tomorrow I have to do a demonstrative speech demonstrating how to do something. I chose TI-BASIC as my topic. My plan is to write the source code to a little program I made today on the board and explaining the functions on each line. I think I have made things easy to understand. What do you guys think of my speech?
Here is the code to the program I will use as a demonstration:
Code:
Speech Plan wrote:
Demonstrative Speech on Ti-83/84 Plus BASIC Programming
If you’ve never done any programming, then TI-BASIC is a good language to start with. It is very easy to learn. By the end of this speech, you will have learned what you need to know to make a game on your calculator.
First off, you need to start a new program. To do that, press the [PRGM] button, press the left arrow, and press enter. Now you have to name your program. You can name it anything you want. It helps to give it a name that is descriptive of what it does. We will name our program CATCHX. To type, press [ALPHA] and a letter, or [2nd] then [ALPHA] to type a lot of letters, then [ALPHA] again to stop. You should see a flashing cursor next to a colon. This is a line where you write commands, and you press [ENTER] to get a new line. The first thing you will want to do is clear the screen. Press [PRGM], right, then 8 to get the ClrHome function. Press [ENTER] for a new line. Most games have a title screen, and we will use the Output( function to display text on screen. Press [PRGM], right, and 6. The first thing you do is put the y value, or the vertical location. The screen has 8 rows and 16 columns. Next, put a comma and then put the x value, or the horizontal location. Last, put another comma, quotation marks [ALPHA][+] and the text.
:ClrHome
:Output(3,3,"CATCH THE X
:Output(4,5,"MADE BY
:Output(5,1,"ROBERTO SANCHEZ
We can use the Pause function, [PRGM][8], to pause the program until the user presses enter. You will want to store to values in a program. You select a value depending on your data type, press [STO >], and type in your variable. We will worry only about strings and numbers. To store to a string, type a quotation mark, the text, sto, and [VARS], 7 for a string. Loops are used to repeat lines of code, and three kinds of loops will be used in this program. Our first loop will keep playing the game until the user wants to quit. It is a while loop [PRGM][5], and executes code while a condition is true. While the user wants to play, the code for the game will repeat.
:Pause
:"Y→Str2
:While Str2="Y"
We will clear the screen again. Next we will store the (y,x) location of the user character at variables (A,B), the (y,x) location of the “x” at (C,D), and the values to change the user character’s location at E for A and F for E.
:ClrHome
:1→A
:1→B
:randInt(4,8→C
:randInt(4,16→D
:1→E
:1→F
You get randInt by pressing [MATH][left][5]. It gets a random number from a lower limit, the first number, to an upper limit, the second number. You can use the Input [PRGM][right][ENTER] function to get user input. First enter a question in quotations, a comma, and the variable to store into. We will store a name into a string.
:Input "NAME? ",Str1
Most games have some sort of limit, like a time limit. A for loop [PRGM][5] can do this. First enter the variable to be change, then the start, and last the end. The loop will increment the variable by 1 when it reaches the end of the code block, and repeat the code until the upper limit is met.
:For(G,1,360
Next, clear the screen. You would want to output the time left, and you can output it in seconds by dividing by 6, since 360 divided by 6 equals 60. You can see the program has a minute time limit. You only want to show a whole number though, not the decimals. To do that, press [MATH][right][5] and type G/6. Then we output the user character, theta [ALPHA][3] to the locations stored in the location variables. We also output the “x” at the location variables.
:ClrHome
:Output(1,15,iPart(G/6
:Output(A,B,"θ
:Output(C,D,"X
The getKey function [PRGM][right][7] checks for a key press and returns the value pressed. If no key is pressed, 0 is returned. You store the value to a variable. The only key values we will worry about are left, right, up, and down. Left is 24, Up is 25, Right is 26, and Down is 34. We will then adjust the values that change the location of the user-character using the code of the key pressed, however, since the values can’t be more than 1 or less than –1, we will check those too.
:getKey→H
:E-((H=25)-(H=34→E
:F-((H=24)-(H=26→F
:E-((E=2)-(E={-}2→E
:F-((F=2)-(F={-}2→F
The reason this works is explained by Boolean logic. Basically, if you have E=2 and E does equal two, it returns 1 for true. If E does not equal two, it returns 0 for false. For example, in the first line after getKey, if the key pressed was Up, it would decrement E by one and if Down was pressed, it would add one because subtracting a negative is addition. Now we have to update the locations by adding E and F.
:A+E→A
:B+F→B
Since there are 8 rows and 16 columns, if, for example, the row variable was 9, you will get an error. Because of that, we must also check for this.
:A-8((A=9)-(A=0→A
:B-16((B=17)-(B=0→B
Now we check to see if the user caught the x. If x was caught, a series of commands must be executed. To set aside these commands, we use an If [PRGM][1] Then [2] statement. If the condition is true, then it executes the code block. If the user caught the x, the user scores a point and the x has to be moved to another random location. Now that we are reaching the end of code blocks, we have to end [PRGM][7] them. We will put one end after If Then and another one right after to end the for loop that controls the time limit:
:If A=C and B=D
:Then
:randInt(1,8→C
:randInt(1,16→D
:I+1→I
:End
:End
That is the end of the game code. After the game ends, we clear the screen and show the user how many points were earned. After that, clear the screen again. :ClrHome
:Disp Str1,"SCORED",I
:Pause "POINTS
:ClrHome
Now you ask the user if they want to play again. There’s no guarantee they will return the correct answer though, so we will repeat the code until the user enters the desired value by using a Repeat loop [PRGM][6]. The code will repeat until either Y or N is entered.
:Repeat Str2="Y" or Str2="N"
:Input "PLAY AGAIN?(Y/N)",Str2
:End
We must add a final end to enclose the while code block that decided if you play again or not. If the user doesn’t want to play again, the game ends. You must delete all the variables that were used so that you don’t waste memory. After that, add a quotation mark to finish.
:End
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar GDelVar HDelVar IDelVar Str1DelVar Str2"
That is the entire code for the program. If you learn from this code, you can use the concepts learned to make other programs. Once you become proficient with BASIC, you can learn other calculator languages that are far more powerful and allow you to make better games like Phoenix. For more tutorials and information, visit the following sites:
www.ticalc.org
Www.ticalc.org/archives/files/authors/95/9542.html
http://tifreakware.calcgames.org/tutorials.htm
If you’ve never done any programming, then TI-BASIC is a good language to start with. It is very easy to learn. By the end of this speech, you will have learned what you need to know to make a game on your calculator.
First off, you need to start a new program. To do that, press the [PRGM] button, press the left arrow, and press enter. Now you have to name your program. You can name it anything you want. It helps to give it a name that is descriptive of what it does. We will name our program CATCHX. To type, press [ALPHA] and a letter, or [2nd] then [ALPHA] to type a lot of letters, then [ALPHA] again to stop. You should see a flashing cursor next to a colon. This is a line where you write commands, and you press [ENTER] to get a new line. The first thing you will want to do is clear the screen. Press [PRGM], right, then 8 to get the ClrHome function. Press [ENTER] for a new line. Most games have a title screen, and we will use the Output( function to display text on screen. Press [PRGM], right, and 6. The first thing you do is put the y value, or the vertical location. The screen has 8 rows and 16 columns. Next, put a comma and then put the x value, or the horizontal location. Last, put another comma, quotation marks [ALPHA][+] and the text.
:ClrHome
:Output(3,3,"CATCH THE X
:Output(4,5,"MADE BY
:Output(5,1,"ROBERTO SANCHEZ
We can use the Pause function, [PRGM][8], to pause the program until the user presses enter. You will want to store to values in a program. You select a value depending on your data type, press [STO >], and type in your variable. We will worry only about strings and numbers. To store to a string, type a quotation mark, the text, sto, and [VARS], 7 for a string. Loops are used to repeat lines of code, and three kinds of loops will be used in this program. Our first loop will keep playing the game until the user wants to quit. It is a while loop [PRGM][5], and executes code while a condition is true. While the user wants to play, the code for the game will repeat.
:Pause
:"Y→Str2
:While Str2="Y"
We will clear the screen again. Next we will store the (y,x) location of the user character at variables (A,B), the (y,x) location of the “x” at (C,D), and the values to change the user character’s location at E for A and F for E.
:ClrHome
:1→A
:1→B
:randInt(4,8→C
:randInt(4,16→D
:1→E
:1→F
You get randInt by pressing [MATH][left][5]. It gets a random number from a lower limit, the first number, to an upper limit, the second number. You can use the Input [PRGM][right][ENTER] function to get user input. First enter a question in quotations, a comma, and the variable to store into. We will store a name into a string.
:Input "NAME? ",Str1
Most games have some sort of limit, like a time limit. A for loop [PRGM][5] can do this. First enter the variable to be change, then the start, and last the end. The loop will increment the variable by 1 when it reaches the end of the code block, and repeat the code until the upper limit is met.
:For(G,1,360
Next, clear the screen. You would want to output the time left, and you can output it in seconds by dividing by 6, since 360 divided by 6 equals 60. You can see the program has a minute time limit. You only want to show a whole number though, not the decimals. To do that, press [MATH][right][5] and type G/6. Then we output the user character, theta [ALPHA][3] to the locations stored in the location variables. We also output the “x” at the location variables.
:ClrHome
:Output(1,15,iPart(G/6
:Output(A,B,"θ
:Output(C,D,"X
The getKey function [PRGM][right][7] checks for a key press and returns the value pressed. If no key is pressed, 0 is returned. You store the value to a variable. The only key values we will worry about are left, right, up, and down. Left is 24, Up is 25, Right is 26, and Down is 34. We will then adjust the values that change the location of the user-character using the code of the key pressed, however, since the values can’t be more than 1 or less than –1, we will check those too.
:getKey→H
:E-((H=25)-(H=34→E
:F-((H=24)-(H=26→F
:E-((E=2)-(E={-}2→E
:F-((F=2)-(F={-}2→F
The reason this works is explained by Boolean logic. Basically, if you have E=2 and E does equal two, it returns 1 for true. If E does not equal two, it returns 0 for false. For example, in the first line after getKey, if the key pressed was Up, it would decrement E by one and if Down was pressed, it would add one because subtracting a negative is addition. Now we have to update the locations by adding E and F.
:A+E→A
:B+F→B
Since there are 8 rows and 16 columns, if, for example, the row variable was 9, you will get an error. Because of that, we must also check for this.
:A-8((A=9)-(A=0→A
:B-16((B=17)-(B=0→B
Now we check to see if the user caught the x. If x was caught, a series of commands must be executed. To set aside these commands, we use an If [PRGM][1] Then [2] statement. If the condition is true, then it executes the code block. If the user caught the x, the user scores a point and the x has to be moved to another random location. Now that we are reaching the end of code blocks, we have to end [PRGM][7] them. We will put one end after If Then and another one right after to end the for loop that controls the time limit:
:If A=C and B=D
:Then
:randInt(1,8→C
:randInt(1,16→D
:I+1→I
:End
:End
That is the end of the game code. After the game ends, we clear the screen and show the user how many points were earned. After that, clear the screen again. :ClrHome
:Disp Str1,"SCORED",I
:Pause "POINTS
:ClrHome
Now you ask the user if they want to play again. There’s no guarantee they will return the correct answer though, so we will repeat the code until the user enters the desired value by using a Repeat loop [PRGM][6]. The code will repeat until either Y or N is entered.
:Repeat Str2="Y" or Str2="N"
:Input "PLAY AGAIN?(Y/N)",Str2
:End
We must add a final end to enclose the while code block that decided if you play again or not. If the user doesn’t want to play again, the game ends. You must delete all the variables that were used so that you don’t waste memory. After that, add a quotation mark to finish.
:End
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar GDelVar HDelVar IDelVar Str1DelVar Str2"
That is the entire code for the program. If you learn from this code, you can use the concepts learned to make other programs. Once you become proficient with BASIC, you can learn other calculator languages that are far more powerful and allow you to make better games like Phoenix. For more tutorials and information, visit the following sites:
www.ticalc.org
Www.ticalc.org/archives/files/authors/95/9542.html
http://tifreakware.calcgames.org/tutorials.htm
Here is the code to the program I will use as a demonstration:
Code:
PROGRAM:CATCHX
:ClrHome
:Output(3,3,"CATCH THE X
:Output(4,5,"MADE BY
:Output(5,1,"ROBERTO SANCHEZ
:Pause
:"Y→Str2
:While Str2="Y"
:ClrHome
:1→A
:1→B
:randInt(4,8→C
:randInt(4,16→D
:1→E
:1→F
:Input "NAME? ",Str1
:For(G,1,360
:ClrHome
:Output(1,15,iPart(G/6
:Output(A,B,"θ
:Output(C,D,"X
:getKey→H
:E-((H=25)-(H=34→E
:F-((H=24)-(H=26→F
:E-((E=2)-(E={-}2→E
:F-((F=2)-(F={-}2→F
:A+E→A
:B+F→B
:A-8((A=9)-(A=0→A
:B-16((B=17)-(B=0→B
:If A=C and B=D
:Then
:randInt(1,8→C
:randInt(1,16→D
:I+1→I
:End
:End
:ClrHome
:Disp Str1,"SCORED",I
:Pause "POINTS
:ClrHome
:Repeat Str2="Y" or Str2="N"
:Input "PLAY AGAIN?(Y/N)",Str2
:End
:End
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar GDelVar HDelVar IDelVar Str1DelVar Str2"
Generated by SourceCoder (http://www.cemetech.net/projects/basicelite/sourcecoder.php)
© 2005 Cemetech (http://www.cemetech.net)