Skip to content

Welcome to my site on programming Android apps with BASIC!
Check out the Tutorials page to begin programming your Android device.

writeIntShort and readIntShort


! 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

Drawing Objects

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.

  1. Open the graphics mode
  2. create a drawing area
  3. begin drawing mode
  4. set the drawing color
  5. draw a object
  6. end the draw mode
  7. send the object to the display list
  8. 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.

 

 

Controlling The Flow

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

Loops

I use the FOR and WHILE loops the most. Typically FOR loops cycle through contained code a set number of times. A WHILE loop REPEATs the code while a condition is met. A DO loop repeats the code UNTIL a condition is met.


! Count from 1 to 10 with a FOR loop
cls
for i=1 to 10
     print i
next
print "All done!"

Each FOR loop must be terminated with NEXT.

The following program using a WHILE loop accomplishes the same as the previous FOR loop. You must terminate your WHILE loop with REPEAT.


!  Count from 1 to 10 with a WHILE loop
cls
i=1
while i=<10
     print i
     i+=1
repeat
print "All done!"

i+=1 is the same as i=i+1. This is just a shortcut to increment a variable by a certain value.

The DO loop is similar to the WHILE loop and will loop until a condition is met.


!  Count from 1 to 10 with a DO loop
cls
do
     i+=1
     print i
until i=10
print "All done!"

You can see that the DO loop will always get executed at least once because the condition is not tested until the end of the loop. A WHILE loop will not be executed if the conditions are not met since the condition is checked at the beginning of the loop.

You can also nest your loops.


! Examples of nested loops
cls
print "A nested FOR loop"
for i=1 to 10
     for j=1 to 10
          print i+", "+j
     next
next

print "A nested WHILE/FOR loop"
i=1
while i=<10
     for j=1 to 10
          print i+", "+j
     next
     i+=1
repeat

Both loops accomplish the same task.

Converting Variable Data

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.

Variable Basics

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$

Return to contents

Let’s Start Programming

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.


Return to the contents