Ozapell Basic Learn Programming #11

Sometimes you will need to add delays in your program.

Let’s assume you wrote a program in Ozapell Basic to draw a white pixel on a black screen in graphics mode:

BACKCOLOR=COLORBLACK
MODE=1
SETMODE
TEXTCOLOR=COLORWHITE
X=10
Y=10
COLORXY

You run the program and nothing happens. The program executed properly and drew its pixel, but then it returned to text mode when the program ended.

This could also be a problem if a wave audio sound was loaded, played and the program ended. The sound would stop immediately and it might seem as though the sound wasn’t playing correctly.

Here are some delays you can add to the end of your program:

REPEAT
UNTIL FALSE

This will create an infinite loop. Only pressing the ESC key will end the program.

REPEAT
INPUT
UNTIL RESULT

This will loop until a key is pressed. INPUT will return a string character in RESULT. If it is an empty string (length of zero), the loop will continue. If a key is pressed (associated with a string character), then the loop will end.

TIME
OLDTIME=RESULT
REPEAT
TIME
RESULT-OLDTIME
RESULT>1000
UNTIL RESULT

This will loop for 1000 milliseconds (one second).

Ozapell Basic on Steam

 

Advertisements
Ozapell Basic Learn Programming #11

Ozapell Basic Learn Programming #10

Sprite Collision Detection is easy with Ozapell Basic!

Switch to graphics mode:

MODE=1
SETMODE

Assuming a bitmap image file called BITMAP.BMP is in the Documents -> Ozapell Basic folder:

BITMAP=0
ARRAY=BITMAP
FILENAME=”BITMAP”
BITMAPLOAD
BITMAPWIDTH=WIDTH

The array BITMAP now contains all of the pixels for the image loaded. RESULT will return the number of entries (pixels) in the array. WIDTH will contain the width in pixels for the image.

ARRAY=BITMAP
WIDTH=BITMAPWIDTH
X=0
Y=0
ARRAYDRAW

This will draw the image in BITMAP at the upper, leftmost corner of the screen (0,0).

ARRAY=BITMAP
WIDTH=BITMAPWIDTH
X=0
Y=0
OWNER=100
ARRAYDRAWOWNER

Draws the image in BITMAP at 0,0 plus every pixel drawn is associated with the value in OWNER. (Pixels given the a negative value are transparent and will not be associated with an owner value.)

ARRAY=BITMAP
WIDTH=BITMAPWIDTH
X=0
Y=0
ARRAYDRAWCOLLIDE

RESULT will contain the highest owner value associated with any pixel that would be drawn over if ARRAYDRAW was used instead of ARRAYDRAWCOLLIDE. This value can be used to determine collisions such as walls, enemies and bonuses.

OWNER=100
OWNERCLEAR

All pixels (and owner data) with the owner value given in OWNER will be cleared from the screen.

Ozapell Basic on Steam

Ozapell Basic Learn Programming #10

Ozapell Basic Learn Programming #9

The latest version of Ozapell Basic adds support for wave audio.

Assuming there is a valid audio file called AUDIO.WAV in the Documents -> Ozapell Basic folder, then the following code will load it:

FILENAME=”AUDIO”
AUDIOARRAY=0
ARRAY=AUDIOARRAY
WAVEFILELOAD

Now the array AUDIOARRAY contains the audio data. This data can be analyzed or manipulated. RESULT will contain the number of entries (samples) in the array.

Currently, the only wave audio format supported is Uncompressed PCM format, 16 bits per sample (CD quality), 44,100 samples per second (CD quality) and 1 channel (mono).

To copy the audio data from an array to a sound buffer:

ARRAY=AUDIOARRAY
BUFFER=0
WAVEFILEBUFFER

There are 32 sound buffers (0 to 31).

Audio data under 15 seconds in length will be considered a sound effect. 16 sound buffers will be created for these sound effects to allow for up to 16 simultaneous plays of the same sound.

Audio data over 15 seconds in length (with a current maximum of 5 minutes) will only be provided one sound buffer. Only one instance of this sound can be played from this buffer at one time. Playing the sound while it is already playing will cause it stop and start over from the beginning.

To play a sound:

BUFFER=0
WAVEFILEPLAY

To loop a sound:

BUFFER=0
WAVEFILELOOP

To stop all sound buffers for a specific sound:

BUFFER=0
WAVEFILESTOP

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #9

Ozapell Basic Learn Programming #8

Graphics are simple with Ozapell Basic.

First, the program must switch from text mode to graphics mode. In text mode, the screen is mapped as 40 x 22 text characters. In graphics mode, the screen is mapped as 320 x 180 pixels.

These two lines switch to graphics mode:

MODE=1

SETMODE

Ozapell Basic will automatically revert back to text mode when the program ends.

The following four lines can be used to draw a pixel on the screen:

TEXTCOLOR=COLORGREEN

X=32

Y=32

COLORXY

In this example, a green pixel will be displayed at coordinates 32,32 on the screen. The upper-leftmost corner is 0,0. The bottom-rightmost corner is 319,179.

When working with graphics mode, be sure to end your program waiting for a keypress. Otherwise, Ozapell Basic will return to text mode and the graphics will not be visible. Here’s a simple example:

REPEAT

INPUT

UNTIL RESULT

In this example, the program will loop until a key is pressed on the keyboard.

A bitmap image file can be loaded and displayed in graphics mode.

Here is an example of loading a bitmap file called DEMO.BMP. Note: all data files must be copied into the Ozapell Basic folder found in the Documents folder to be accessible by Ozapell Basic.

FILENAME=”DEMO”

MYBITMAP=0

ARRAY=MYBITMAP

BITMAPLOAD

MYBITMAPWIDTH=WIDTH

In this example, DEMO.BMP is loaded into the array called MYBITMAP. (Note: Ozapell Basic does not accept extensions or file paths. Only the filename without the extension is used.) RESULT will contain the number of pixels in the image which is also the number of entries in the array. WIDTH will contain the width of the image loaded. This value is used to display the image later.

To draw the image on the screen while in graphics mode, ARRAYDRAW is used:

ARRAY=MYBITMAP

WIDTH=MYBITMAPWIDTH

X=0

Y=0

ARRAYDRAW

In this example, the image contained in the array MYBITMAP is displayed in the upper-leftmost corner of the screen.

By default, all display subroutines draw on the screen immediately which is slower. For faster graphics or to compose complex scenes before displaying the end result, use the offscreen buffer. SCREENHIDE stops displays to the screen and, instead, displays all output to the offscreen buffer. SCREENSHOW displays the offscreen buffer to the screen and returns to the default of displaying immediately to the screen.

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #8

Ozapell Basic Learn Programming #7

Arrays are similar to normal variables except they contain one or more entries. Each entry is treated like a variable.

Here is an example of creating an array in Ozapell Basic:

MYARRAY=0

ARRAY=MYARRAY

RANGE=100

DIM

The variable MYARRAY is declared and given the value of 0. The ARRAY variable is given MYARRAY. RANGE will specify how many entries will be in the array. DIM changes the variable MYARRAY into an array containing 100 entries with the value of 0 in each.

If MYARRAY had been declared with a different initial value, then every entry would contain that value instead.

Here’s an example of an array working with strings:

MYSTRINGARRAY=”HELLO WORLD”

ARRAY=MYSTRINGARRAY

RANGE=20

DIM

This code will result in MYSTRINGARRAY containing 20 entries with the string “HELLO WORLD” in them.

Here is an example of an array of colors:

COLORS=COLORBLACK

RANGE=3

ARRAY=COLORS

DIM

COLORS=COLORGREEN

COLORS=COLORBLUE

COLORS=COLORRED

In this example, an array called COLORS is created with 3 entries containing the color black. Then each entry is initialized with a different color.

Arrays in Ozapell Basic will automatically advance to the next entry when the array is read or new data is written to it. If the array reaches the end, it will automatically circle back to the first entry.

The array index can also be set manually. This would be useful for random access to the array’s data.

Assuming the previous array COLORS has been declared…

ARRAY=COLORS

INDEX=0

ARRAYINDEX

This example will change the index (next entry to be used) in the array to 0. In Ozapell Basic, all arrays are indexed starting from 0. The first entry in every array is 0, the second entry is 1, etc.

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #7

Ozapell Basic Learn Programming #6

A subroutine is a section of code that can be executed once or many times. The code in a subroutine is encapsulated. If properly designed, a subroutine could be used by other programs after its first development. Make code reusable to save time.

In Ozapell Basic, a new subroutine is declared using the SUB statement.

The main section of an Ozapell Basic program is always at the top of the source code. All subroutines declared must be after the main section. The compiler determines that the main section has ended when it encounters the first SUB declaration or the end of the source code.

Example:

FOR X=1 to 10

PRINTIT

NEXT X

SUB PRINTIT

PRINTLINE “HELLO”

In this example, the subroutine PRINTIT is declared. The compiler determines the end of the subroutine when it either encounters the next SUB declaration or reaches the end of the source code.

PRINTIT is called by writing its name alone as a line of code. Each time PRINTIT is called, the code will execute. In this example, the text string “HELLO” will be assigned to the variable TEXT and the built-in display routine PRINTLINE will show HELLO on the screen.

PRINTLINE is a built-in subroutine that uses the variable TEXT. New subroutines can use any variable available as long as the variable is declared before calling the subroutine.

INPUT is a built-in subroutine returning a key pressed in the RESULT variable. New subroutines can declare and return any variables necessary.

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #6

Ozapell Basic Learn Programming #5

In addition to the FOR NEXT statement(s) discussed previously, there are two more logical flow controlling statements.

The first is WHILE ENDWHILE. If the variable or data provided is TRUE, the code will execute. If it is FALSE, then WHILE will jump to the ENDWHILE and the loop will end.

Example:

VALID=TRUE

WHILE VALID

PRINTLINE “VALID”

VALID=FALSE

ENDWHILE

In this example, the loop will execute only once. If it had been WHILE TRUE, there would have been an infinite loop which could only be stopped by pressing [ESC] on the keyboard.

The second logical flow control statement(s) is REPEAT UNTIL. Unlike WHILE ENDWHILE which may never execute, REPEAT UNTIL will always execute its loop at least once. When the variable or data provided at UNTIL is TRUE, the loop will end.

Second example:

DONE=TRUE

REPEAT

PRINTLINE “OUTPUT”

UNTIL DONE

In this example, the loop will execute only once. If DONE had been assigned FALSE, the loop would be infinite and would require the pressing of [ESC] on the keyboard to end.

Ozapell Basic on Steam

 

Ozapell Basic Learn Programming #5