Programming AmigaOS in Basic

Sprites and Bobs

AmigaBasic and HiSoft Basic supports Sprites and Bobs (Blitter Objects) for animation. AmigaBasic can support multiple sprites, only limited by how much memory it has available. ACE Basic does not have any sprite/bob functionality, so you will need to use the graphics library functionality (see C Tutorial).

To use sprites and bobs, you need to use the OBJECT statements to create and manipulate them:

OBJECT.SHAPE id, definition - Create and define an object's shape (id can be 1 to n).
OBJECT.SHAPE id1, id2 - Copy a object to another object.
OBJECT.START id[,id2...] - Start objects into motion.
OBJECT.STOP id [id2 ...] - Stop or freeze the motion of objects.
OBJECT.VX id, value - Specify the velocity of object in x (horiz) direction. Velocity is in pixels/sec.
OBJECT.VY id, value - Specify the velocity of object in y (vert) direction.Velocity is in pixels/sec.
OBJECT.VX (id) - Return x velocity of object.
OBJECT.VY (id) - Return y velocity of object.
OBJECT.X id, value - Specify x co-ordinates of object.
OBJECT.Y id, value - Specify y co-ordinates of object.
OBJECT.X (id) - Return x co-ordinate of object.
OBJECT.Y (id) - Return y co-ordinate of object.
OBJECT.AX id, value - Specify x acceleration of object.
OBJECT.AY id, value - Specify y acceleration of object.
OBJECT.CLIP (x1, y1)-(x2, y2) - Defines a rectangle and tells Basic not to draw objects outside this area.
OBJECT.CLOSE [id] [,id2 ...] - Removes one or more objects from memory.
OBJECT.HIT id [,MeMask] [,HitMast] - Determines masks of source and object to hit for an object.
OBJECT.ON [id [, id2 ...] - Turns objects on (make visible).
OBJECT.OFF [id [id2 ...] - Turns objects off (make invisible).
OBJECT.PLANES id [, plane-pick][, plane-on-off] - Defnes a bob's plane pick and on-off values (0-255).
OBJECT.PRIORITY id, value - Determines objects priority of display over other objects (bobs). Value is -32768 to 32767.
COLLISION(id) - Returns object id or border value (-1 to -4) if object collided with another.
COLLISION ON|OFF|STOP - Enables, disables or suspends collision detection.

Creating Objects in Basic

To create a sprite or bob in Basic, you use the OBJECT.SHAPE command. You can provide an id number of each object and a definition string of the shape and colour of the object. To make it easier to define your object's AmigaBasic provides an Object Editor program which will generate a definition for you. Best run the editor in a low-res mode to easily work on the small sprites or bobs. You can then load the shape from a file in your program.
e.g.

 OPEN "sprite.dat" FOR INPUT AS 1
 OBJECT.SHAPE 1, INPUT$(LOF(1), 1)
 CLOSE 1 

Collision Detection

To determine whether sprites are objects hit one another you need to use commands such as OBJECT.HIT, ON COLLISION GOSUB, COLLISION ON|OFF|STOP and the COLLISION function. The Object.Hit statement defines the masks of the source object and the object to hit to say whether a collision has occured. For example, you do not want an object to hit an area of a rectangle when the object is actually a ball shaped object. There for you need to setup masks to define the area to hit.
The ON COLLISION GOSUB statement will tell the system where to jump to if a collision occurs, just provide a label to the line to jump to.

The Collision(n) function returns either the id of the object that was hit or one of the four borders of the window. If n=0 then it returns to object id of an object that has collided with another object, if n=-1 then it returns the id of the window in which the collision occured. If n >0 then it will it will return the object id of the object that collided with it. If the return value is negative, then a the object collided with a window border. -1 = Top border, -2 = left border, -3 = bottom border and -4 = right border.

Animation Example

WINDOW 1,"Animation", (10,10)-(270,70),15

WINDOW 1
LOCATE 4,4
COLOR 3,2
PRINT "Amiga Microsoft Basic"
COLOR 1,0
ON COLLISION GOSUB bounceoff

COLLISION ON

REM Open ball object from a file
OPEN "ball" FOR INPUT AS 1 OBJECT.SHAPE 1,INPUT$(LOF(1),1) CLOSE 1 REM
Define start position and velocities OBJECT.X 1,60 OBJECT.Y 1,50 OBJECT.VX 1,80 OBJECT.VY 1,80 REM Copy object 1 and set that up too OBJECT.SHAPE 2,1 OBJECT.X 2,50 OBJECT,Y 2,10 OBJECT.VX 2,40 OBJECT.VY 2,40 OBJECT.ON OBJECT.START WHILE 1 SLEEP WEND END BOUNCEOFF: SAVEID = WINDOW(1) WINDOW OUTPUT 1 IF WINDOW(7)=0 THEN RETURN COUNTDOWN=COUNTDOWN-1 IF COUNTDOWN<0 THEN COUNTDOWN=10 OBJECT.CLIP (0,0)-(WINDOW(2), WINDOW(3)) END IF BOUNCELOOP: REM If object hits borders or each other than change direction I=COLLISION(0) IF I=0 THEN BOUNCEEXIT J=COLLISION(I) VXI = OBJECT.VX(I) VYI = OBJECT.VY(I) IF (J=-1 AND VYI<0) OR (J=3 AND VYI>0) THEN OBJECT.VY I,-VYI ELSEIF (J=-2 AND VXI<0) OR (J=-4 AND VXI>0) THEN OBJECT.VX I,-VXI ELSEIF J>0 THEN VYJ = OBJECT.VY(J) VXJ = OBJECT.VX(J) ENDIF IF VYI*VYJ<0 THEN OBJECT.VY I,-VYI OBJECT.VY J,-VYJ END IF IF VXI*VXJ<0 THEN OBJECT.VX I,-VXI OBJECT.VX J,-VXJ END IF OBJECT.START J END IF OBJECT.START I GOTO BOUNCELOOP BOUNCEEXIT: WINDOW OUTPUT SAVEID RETURN

Next Page