! Function to write a short integer
! Range is from 0 to 65535
! Parameters: path to the file as a string, value of the integer as a number
fn.def writeIntShort(fileName$,intValue)
intValue=abs(floor(intValue))
firstByte=floor(intValue/256)
if firstByte>0 then
secondByte=intValue-firstByte*256
else
firstByte=0
secondByte=intValue
endif
byte.open a, file_number, fileName$
byte.write.byte file_number, firstByte
byte.write.byte file_number, secondByte
byte.close file_number
fn.end
! Function to read a short integer
! Parameters: path to file as a string, position to start reading from
fn.def readIntShort(fileName$,filePosition)
byte.open r, file_number, fileName$
byte.position.set file_number, filePosition
byte.read.byte file_number, firstByte
byte.read.byte file_number, secondByte
byte.close file_number
intRead=firstByte*256+secondByte
fn.rtn intRead
fn.end
file.delete fileResult,"savegame.dat"
! Write 257 to a 2 byte block
call writeIntShort ("savegame.dat",257)
! Write 14 to a 2 byte block
call writeIntShort ("savegame.dat",31032)
! Read a 2 byte block at position 1 an return the integer
someInt=readIntShort ("saveGame.dat",1)
print someInt
! Read a 2 byte block at position 3 an return the integer
someInt=readIntShort ("saveGame.dat",3)
print someInt
RFO BASIC! has several graphical objects that can be drawn to the screen.
- Arch
- Circle
- Line
- Oval
- Pixels
- Polygon
- Rectangle
Here is a brief overview of how to display graphical objects.
- Open the graphics mode
- create a drawing area
- begin drawing mode
- set the drawing color
- draw a object
- end the draw mode
- send the object to the display list
- render the display list
This program will draw a solid black square in the upper left corner.
! Open graphics mode with a white background and no transparency
Gr.open 255, 255, 255, 255
! Clear the graphics screen
gr.cls
! Create a new bitmap called viewScreen with a width of 320 pixels and height of 240 pixels
gr.bitmap.create viewScreen,320,240
! Select the bitmap called viewScreen
gr.bitmap.drawinto.start viewScreen
! Set the color to no transparency, black and draw both stroke and fill
gr.color 255,0,0,0,2
! Draw a rectangle 32 pixels wide and 32 pixels high in the upper left corner and name it objTemp
gr.rect objTemp,0,0,32,32
! Stop drawing on the bitmap
gr.bitmap.drawinto.end
! Draw this bitmap onto the graphics screen at the upper left corner and reference this bitmap as viewObj
gr.bitmap.draw viewObj,viewScreen,0,0
! Render the graphics screen
gr.render
! Wait unit the screen is touched
do
gr.touch touched, x, y
until touched
Draw all of your shapes before initiating the gr.bitmap.drawinto.end command. Refer to the manual for the parameters for the other shapes.
You can use the IF-THEN-ELSE statements to control the flow of your program based on the conditions you set.
! Print a statement based on the result of a random number
! Generate a number from 1 to 6
randomNumber=floor(rnd()*6)+1
if randomNumber>3 then
print "A high number was rolled."
else
print "A low number was rolled."
endif
The RND statement generates a number less than one but equal to or greater than zero. The FLOOR statement rounds the number down to the closest integer. So in the case above the random number would be multiplied by 6 giving your a result range of 0-5. By adding one to the result, you create a random integers with a range of 1-6.
The IF statement tests randomNumber and prints the appropriate statement. You can include multiple statements between the IF/ELSE and ELSE/ENDIF segments. You can even include nested IF statements.
! Example of nested IF statement
<pre>! Generate a number from 1 to 6
randomNumber=floor(rnd()*6)+1
if randomNumber>3 then
if randomNumber=6 then
print "The highest number was rolled."
else
print "A high number was rolled."
endif
else
if randomNumber=1 then
print "The lowest number was rolled."
else
print "A low number was rolled."
endif
endif
You can use the “&“, AND, and “|“, OR, to create Boolean tests.
! Test a Boolean OR statement based on the result of a random number
! Generate a number from 1 to 6
randomNumber=floor(rnd()*6)+1
if randomNumber=1 ! randomNumber=6 then
print "An extreme number was rolled."
else
print "A regular number was rolled."
endif
! Test a Boolean AND statement based on the result of random numbers ! Generate a number from 1 to 6 randomNumber1=floor(rnd()*6)+1 randomNumber2=floor(rnd()*6)+1 if randomNumber1=1 & randomNumber2=1 then print "A rare number was rolled." else print "A common number was rolled." endif
Sometimes you may need to convert the data from one variable type to another.
If you want to convert a string to a number so you can use it in a calculation, you use the VAL command.
! Convert a string to a number and use it in a calculation cls numberString$="10" print 5+val(numberString$)
You can use the STR$ command to convert a number into a string.
! Convert a number to a string and join it with another string cls gameNumber=10 gameTitle$="Big "+str$(gameNumber) print gameTitle$
If you run the code, you will notice that “Big 10.0” is displayed.
You can use the FORMAT$ command to convert the number into a formatted string so online “10” is displayed.
! Convert a number to a string and join it with another string
cls
gameNumber=10
gameTitle$="Big "+format$("##", gameNumber)
print gameTitle$
You can review the BASIC! documentation on different ways to format numbers.
Variables are used to store information in BASIC!. The two types of variables that BASIC! uses are string variables and number variables.
Note: Variables are not case-sensitive in BASIC!
Here is an example of a number variable.
someNumber=10.5
The variable someNumber is set to a value of 10.5. Anytime you use someNumber in a calculation or print statement, 10.5 is used for the value.
! Print the variable someNumber cls someNumber=10.5 print someNumber
! Print the sum of someNumber and 5 cls someNumber=10.5 print someNumber+5
String data is always enclosed with quotes. String variable names end with a “$”.
Here is an example of a string variable.
firstName$="John"
Here is an example of printing string variables.
! Print the variable firstName$ cls firstName$="John" print firstName$
You can also join together string variables.
! Print the variable firstName$ and lastName$ cls firstName$="John" lastName$="Doe" print firstName$+lastName$
Usually the first program you are taught in a programming tutorial is the “Hello World” program. This is just a simple program to display the text “Hello World”.
Here’s the code:
! A program to print Hello World cls print "Hello World"
Run the code and you should see the words “Hello World” on your screen. The PRINT statement lets you display strings and numbers on the screen.
The first line of code that leads off with a “!” is a comment. Comments are ignored by the program. It is good practice to add comments to your programs for both you and other programmers who will work with your code. Later on I will show you how to use different commenting features.
The CLS command clears the screen and prepares a fresh slate for you to use.
Welcome to my site for learning how to program Android apps using RFO BASIC!. RFO BASIC! is an Android app that lets you create programs for your Android device using a dialect of Dartmouth Basic. These tutorials are designed to teach someone who has little or no experience in programming how to create programs using BASIC!.
To begin:
- Download the BASIC! app by Paul Laughton from the Google Play Store.
- Download the latest BASIC! documentation at http://www.laughton.com/basic/
You can create and run your programs from the app. The documentation includes instructions on how to compile your program into an app so you can share it with friends or upload it to app stores such as Google’s Play Store or Amazon’s App Store.
I find it convenient to type the code on my desktop and copy it over to the Android through an USB connection. I use Notepad++ to edit my programs. Here is a link to my Notepad++ language definition file I use for BASIC! https://www.box.com/s/p8w4u676k3xslx9q4ije In Notepad++, click on Language, Define your language, Import and select the XML file. You can change the colors to suit your preference.
I use Nicolas Mougin’s automated tool,
http://mougino.free.fr/rfo-basic-app-builder.zip,
to compile BASIC! programs into a standalone app.
! Function to calculate dice rolls
! By Johnny Jarrell
! Version 4-16-2013
fn.def rollDice(number,size,modifier)
number=floor(number)
size=floor(size)
modifier=floor(modifier)
for i=1 to number
diceTotal+=floor(rnd()*size+1)
next
diceTotal+=modifier
fn.rtn diceTotal
fn.end
cls
! Print the result of 2d4+2
print rollDice(2,4,2)
! Function to return the distance between two points ! By Johnny Jarrell ! Version 4-16-2013 fn.def pointDistance(x1,y1,x2,y2) distanceBetweenPoints=sqr(pow((x2-x1),2)+pow((y2-y1),2)) fn.rtn distanceBetweenPoints fn.end cls ! Calculate the distance between point (2,3) and point (5,6) print pointDistance(2,3,5,6)