Entwicklung

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:

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:

either

/boot/home/config/settings/yab

     (PM Haiku)

or/boot/home/cinfig/lib/yab

     ( 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
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$)

    static num
    if num = 0 then
        bitmap 680,680, "Di"
        err = draw image 0,0 to 675,450, "/boot/home/Dice.png", "Di"
    end if

    num=num+1

    dim field$(num,2) // id$, view$
    dim field (num, 3) // sides,x,y

    // set the default number of sides     if sides < 4 sides = 6
        field$ (num,1)=id$
        field$ (num,2)=view$
        field (num,1)=sides
        field (num,1)=sides
        field (num,2)=x
        field (num,3)=y
        VIEW x,y TO x+225,y+225, id$, view$
        for n=1 to field(num,1)
            switch n
                case 1
                    x=0:y=0
                    break
                case 2
                    x=225:y=0
                    break
                case 3
                    x=450:y=0
                    break
                case 4
                    x=0:y=225
                    break
                case 5
                    x=225:y=225
                    break
                case 6
                    x=450: y=225
                    break

    // 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.

            end switch

            bitmap get x,y to x+225, y+225,str$(n),"Di"
    next

    // 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"
    return "ok"
end sub

//________________________________________________

export sub di_get_face(id$)
    s=arraysize(field$(),1)
    for x=1 to s
        if (field$(x,1)=id$) num = x
    next
    return field(num,0)
end sub

//________________________________________________

export sub di_show_face(id$)
    n = di_get_face(id$)
    DRAW BITMAP 0,0, str$(n), "copy", id$
end sub

//________________________________________________

export sub di_roll(id$)
    s=arraysize(field$(),1)
    for x=1 to s
        if (field$(x,1)=id$) num = x
    next
    field (num,0) = int(ran(field(num,1)))+1
    di_show_face(id$)
    return
end sub

//________________________________________________

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"

#!yab

import di

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
doc Jim Saxton, April 10, 2014, MIT license

// set DEBUG = 1 to print out all messages on the console
DEBUG = 0
sleep .01

OpenWindow()

di_show_face("first")
di_show_face("second")

// Main Message Loop
dim msg$(1)
    while(not leavingLoop)
        nCommands = token(message$, msg$(), "|")

        for everyCommand = 1 to nCommands
            if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)

            switch(msg$(everyCommand))
                case "roll":
                    di_roll("first")
                    di_roll("second")
                    break
                case "_QuitRequested":
                case "MainWindow:_QuitRequested":
                    leavingLoop = true
                    break
                default:
                    break
            end switch

    next everyCommand
wend

CloseWindow()

end

// Setup the main window here
sub OpenWindow()
    window open 100,100 to 570,365, "MainWindow", "Main Window"
        di_new$(6,"first",10,10,"MainWindow")
        di_new$(6,"second",235,10,"MainWindow")
        BUTTON 10,238 TO 460,263, "roll", "R O L L", "MainWindow"
        return
end sub

// Close down the main window
sub CloseWindow()
    window close "MainWindow"
    return
end sub


Here you can download the complete code: Archive

Tutorial By Jim Saxton, 10 April 2014 - MIT license
Made available by BeSly, the Haiku, BeOS and Zeta knowledge base.

The Playground Editor is a program with which you can create playing fields for games. To do this, you specify a size for the playing field, import graphics and place these graphics on a generated grid.

You can define different backgrounds, objects and switches (example: background = meadow, object = tree, switch = treasure). Once you have created your playing field, it can be saved as a project. This makes subsequent editing easier.

The saved project can be exported. The exported playing field is a text file with the entries of the playing field and the associated graphics. If you want to incorporate this into a game, you have to read out the text file and use the read out data to generate the playing field.

In the text file, 3 numbers per field of the playing field are separated by a colon. Each field is marked with a straight vertical line | separated:

BG01:OB02:SW00|BG01:OB03:SW04|…

The first number is the number for the background, the second for the item, and the third is the switch.

This makes the generation of playing fields much easier. Because, unlike in most common forms with w for wall, p for player and spaces for empty space, you can plan a complete world here. The switch can stand for many things, e.g. for a random function for opponents, finding objects or the destination of a level.

In the editor you can insert a comment for each object, which provides a better overview, especially with the switches.

Installation

Since the Playground Editor is not part of the Haiku standard installation, it must be installed from an external source.

To integrate the installation source into HaikuDeport enter the following command line in the terminal:

BeSly 32bit

pkgman add-repo https://software.besly.de/repo

BeSly 64bit

pkgman add-repo https://software.besly.de/repo64

Then open HaikuDepot and update the repository server via "refresh repositories" in the HaikuDepot menu.

After the update has been completed, the packages from our repository server are also listed in the package lists.

Search for PlaygroundEditor and install it.

Create a new project

pged menu

In the start menu of the program select "new project".

pged new project

In the next window you define the size of the desired playing field (vertical fields and horizontal fields) and its name.

pged editor

After confirming this information, the playing field will be created. This is initially a grid with the desired number of fields.

Insert graphics

Now go to "Tiles" to add playing field graphics (tilesets).

pged tilesets

To add graphics here, search in the left directory list for the corresponding graphic and add it to the desired area (backgrounds to "Backgrounds", objects to "Objects").

After adding the tilesets, go back to the editor. The tilesets that have been added are now displayed here.

Painting the playing field

pged editor drawing

There are three lists in the right area of the editor. One for the backgrounds, one for the objects and one for the switches.

To paint a playing field, select one of the graphics listed and paint it on the editor field on the grid area by pressing the left mouse button.

Descriptions of the tilesets

To ensure a better overview, every background, every object and every switch can be given a description. To do this, select one of the entries and use the right mouse button to select the "Add Description" option. A description can then be inserted and added in the upper right input field.

Export playing field

pged export map

To export the playing field go to "Export".

There are several ways to export the playing field. The playing field can be exported in whole or in part. To do this, select the appropriate storage option and press export.

The different options allow different processing of the playing field file. Everyone has different approaches and logic in programming. Therefore you can create a classic playing field here which is simple and simple, or you can use all functions and use the extensive range of functions.

pged map file

The exported playing field is output as a text file.

You can also export a screenshot of the playing field when exporting. This makes programming easier because you can see what is where better without looking at numbers and letters.

Subcategories