What is an object?
For an official definition see: http://searchsoa.techtarget.com/definition/object
This sounds like gobblty-gook to a proceedural programmer.
An object is really just a sub-program and group of sub-routines that can be called from the main program.
Yab Being a BASIC language, and therefore to a yab programmer, the idea of objects seems foreign, but as a yab programmer, one uses objects.
All of the GUI bindings in yab are objects.
WINDOW OPEN x1,y1 TO x2,y2, ID$, Title$
This is a call to create a window object, it creates a new window object, gives it its boundries, and identifies it.
WINDOW SET WindowID$, "MoveTo", x,y
Is a call to the window object named WindowID$, this one moves the window object to a new location.
OK, we have established that yab programmers use objects. These are, however part of the yab interpreter. now, lets define and use an object in yab.
Our object will be a di for a game that needs one to roll the dice.
Since objects are often re-used, that is the whole ides of objects, it makes sense to use a yab library file for our object.
First is the class, a definition of what a di object is and what it can do.
A di object is usually a representation of a six faced physical object.
The number of faces can be different depending on the need for the particular game, but four is the minimum.
Each face can have a letter, number, word, color, picture, or a number of dots, etc.
A di can:
Show one of the faces.
Be shaken and rolled ( randomly pick another face and show it.)
Be interogated for the number of the face.
now for the code:
name the file di.yab and place it in the yab library folder:
- (PM Haiku)
- ( pre PM haiku)
//copy the lines below for the di object (di.yab)
doc an object programmed in yab
doc to demonstrate the idea of programming an
doc object in a proceedural language.
doc by Jim Saxton, April 10, 2014, MIT license
// we need a subroutine to make a new di object
// num, field() and field$() do not need to be known by the calling program.
export sub di_new$(sides, id$,x,y, view$)
if num = 0 then
bitmap 680,680, "Di"
err = draw image 0,0 to 675,450, "/boot/home/Dice.png", "Di"
dim field$(num,2) // id$, view$
dim field (num, 3) // sides,x,y
// set the default number of sides if sides < 4 sides = 6
VIEW x,y TO x+225,y+225, id$, view$
for n=1 to field(num,1)
// This may be modified to allow for more sides than 6.
// If so, the image file and Di bitmap need to be changed as well.
// The bitmap needs to be larger than the image or the bitmap get will
// error with "out of bounds" once your di face is at an edge of the image.
bitmap get x,y to x+225, y+225,str$(n),"Di"
// seed the random generator
x1 = ran(num+1)
// roll the di and chose a side
field (num,0) = int(ran(sides))+1
//The info for the new di object is saved, return "ok"
export sub di_get_face(id$)
for x=1 to s
if (field$(x,1)=id$) num = x
export sub di_show_face(id$)
n = di_get_face(id$)
DRAW BITMAP 0,0, str$(n), "copy", id$
export sub di_roll(id$)
for x=1 to s
if (field$(x,1)=id$) num = x
field (num,0) = int(ran(field(num,1)))+1
This object was set-up for a six-sided di, but could be easilly expanded to all sorts of di, say for D&D, etc.
The object relies on a hard-coded path to /boot/home/Dice.png. This could deasilly be changed to use a path relative to the program location.
I used http://upload.wikimedia.org/wikipedia/commons/4/4c/Dice.png and slightly re-sized it to 675 X 450 for the dice face image.
each di face is 225 X 225
To test the object:
Copy the lines below into "Roll-Dice.yab"
doc a test program
doc to test the di.yab object.
doc It never uses one of the exported subs, di_get_face,
doc but di_show_face does.
doc Jim Saxton, April 10, 2014, MIT license
// set DEBUG = 1 to print out all messages on the console
DEBUG = 0
// Main Message Loop
nCommands = token(message$, msg$(), "|")
for everyCommand = 1 to nCommands
if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)
leavingLoop = true
// Setup the main window here
window open 100,100 to 570,365, "MainWindow", "Main Window"
BUTTON 10,238 TO 460,263, "roll", "R O L L", "MainWindow"
// Close down the main window
window close "MainWindow"
Here you can download the complete code: Archive
Made available by BeSly, the Haiku, BeOS and Zeta knowledge base.