OBJECTS
At the beginning there's an empty world. Game characters, enemies, items and other objects can be loaded into that world. They are independent entities that can interact each other. For example, they can collide. Objects can be moved, and turned, to name only few actions.
DeleteObject
Frees an object from memory. After that you can no longer use its variable to refer to it. All collisions and particle emission bound to that object will also be deleted. Warning! Do not delete master object that has been used as source for cloning. By doing that all the clones will cause the program to crash. Objects that need to be cloned (master objects) should be only hidden and never used.
Usage:
DeleteObject obj
ClearObjects
This command destroys all objects leaving nothing behind. This is handy when you want to, for instance, clean up the game and return to main menu – or prepare for next level.
MoveObject
Moves an object forward relative to its angle. Negative values make object move backwards. Optionally you can also define sidestepping value that moves an object sideways.
Usage:
MoveObject obj, forw# [, side#]
Example:
FrameLimit 40
soldier=LoadObject("Media\soldier.bmp",72)
AddText "Arrows to move"
Repeat
'Update controls
If LeftKey() Then TurnObject soldier,5
If RightKey() Then TurnObject soldier, -5
If UpKey() Then MoveObject soldier, 2
If DownKey() Then MoveObject soldier, -2
DrawScreen
Until EscapeKey()
TranslateObject
This command also moves an object not relative to its angle, but game world axis. An object may, for example, spin in the air, while it's trajectory remains unmodified. See the example.
Usage:
TranslateObject obj, horizontal# [, vertical#]
Example (longish):
Const gravity=0.05
'Prevent the game from running too fast
FrameLimit 40
'Load the ammunation, but hide it
fireball=LoadObject("Media\fireball.bmp",18)
ShowObject fireball,OFF
'load the cannon
cannon=LoadObject("Media\cannon.bmp",180)
'load the turret
turret=LoadObject("Media\turret.bmp")
PositionObject turret, -160, -35
PositionObject cannon, -160, -35AddText "Use arrows to aim"
AddText "SPACE to shoot"
'Make "sky"
ClsColor cbdarkblue
'Initialize the game engine
permission=True
Repeat
'update controls
If LeftKey() Then angle=angle+2
If RightKey() Then angle=angle-2
If angle<10 Then angle=10
If angle>170 Then angle=170
'update SHOOTING
If KeyHit(cbkeyspace) And permission=True
ShowObject fireball,ON 'show the fireball
'position and tilt it according to the cannon
CloneObjectPosition fireball,cannon
CloneObjectOrientation fireball,cannon
'move a little bit away from the center
MoveObject fireball,16
permission=False
'Calculate the movement (speed 2)
plusx#=Cos(angle)*3
plusy#=Sin(angle)*3
EndIf
'Update moving fireball
If permission=False Then
TranslateObject fireball,plusx#,plusy#
plusy#=plusy-gravity
'rotate the fireball to left
TurnObject fireball,20
'if the fireball goes outside screen, remove it
If ObjectX(fireball)< -200 Or ObjectX(fireball)>200 Or ObjectY(fireball)>150 Or ObjectY(fireball)< -50 Then
ShowObject fireball,OFF
permission=True
EndIf
EndIf
'Turn the cannon
RotateObject cannon,angle
'draw ground
Color cbgreen
Box 0,200,400,100
Color cbwhite
Line 0,200,400,200
DrawScreen
Until EscapeKey()
PositionObject
Sets the absolute position of an object.
Usage:
PositionObject obj, X#, Y#
Example:
FrameLimit 40
'Load three cows
cow1=LoadObject("Media\cow.bmp")
cow2=LoadObject("Media\cow.bmp")
cow3=LoadObject("Media\cow.bmp")
'Position two of them to different place
PositionObject cow2, -100,0
PositionObject cow3,0,100
Color cbblack
AddText "Arrows move camera"
ClsColor cbwhite
Repeat
'Control camera in one line!
TranslateCamera RightKey() -LeftKey(),UpKey() -DownKey()
DrawScreen
Until EscapeKey()
ScreenPositionObject
Does the same thing that PositionObject, but relative to screen coordinates (0,0 is the top left corner). You can, for example, position a mouse pointer – which is an object – to screen.
Usage:
ScreenPositionObject obj, X#, Y#
TurnObject
Turns an object. The angle is measured in degrees. Notice that you only specify a value how much will be turned. Not the absolute angle at to turn. See the MoveObject-command example.
Usage:
TurnObject obj, Angle#
RotateObject
Turns an object to specified angle. Absolute angle.
Usage:
RotateObject obj, Angle#
Example:
FrameLimit 40 'limit game speed
soldier=LoadObject("Media\soldier.bmp",36)
'make another object
soldier2=CloneObject(soldier)
PositionObject soldier2,0, -100
ClsColor cbwhite
Repeat
'update this code every 1.5 seconds
If TIMER()>moment+1500 Then
'increase soldier angle
RotateObject soldier,ObjectAngle(soldier)+10
'rotate to random angle
RotateObject soldier2,Rand(360)
moment=TIMER()
EndIf
DrawScreen
Until EscapeKey()
PointObject
Points an object to another object. For example an enemy to you.
Usage:
PointObject obj, target_obj
Example:
FrameLimit 40 'limit game speed
aim=LoadObject("Media\crosshair.bmp")
soldier=LoadObject("Media\soldier.bmp",360)
Color cborange
AddText "Guide the soldier with the arrows..."
ClsColor cbdark
Repeat
'Move object
TranslateObject soldier,RightKey()*2,0
TranslateObject soldier, -LeftKey()*2,0
TranslateObject soldier,0,UpKey()*2
TranslateObject soldier,0, -DownKey()*2
'Keep the aim at the cross
PointObject soldier,aim
DrawScreen
Until EscapeKey()
CloneObjectPosition
Positions an object to another object's position.
Usage:
CloneObjectPosition, obj, target_obj
CloneObjectOrientation
Turns an object to exact same angle than the other.
Usage:
CloneObjectOrientation, obj, target_obj
ObjectOrder
Sends an object to back or brings it front in the drawing order. Notice that floor-objects always render back. 1 to bring front, -1 to send back
Usage:
ObjectOrder obj, order
MaskObject
Sets the transparent colour of an object. For more information check MaskImage-help.
Usage:
MaskObject obj, red, green, blue
ShowObject
Either shows an object or hides it. Hidden objects will not be drawn and they lack collision checks.
Usage:
ShowObject obj, ON/OFF
DefaultVisible
Object auto-hide during load. See ShowObject. Defaults OFF.
Usage:
DefaultVisible ON/OFF
PaintObject
With this command you can apply a skin to an existing object i.e. paint it with a different object or image.
If you paint with an image, be sure to but a minus before image_variable. For example: PaintObject obj, -imgMyskin
If you paint a floor object, you must specify an image_variable (no minus there, though).
If you use this on particle emitters, you must specify an image_variable.
You can also repaint a map with a different tileset. The new tileset must be pre-loaded image i.e. an image variable. This way you don't have to load it from hard disk, because it's slow.
Usage:
PaintObject obj, texture
Example:
'load a map
map=LoadMap("Media\cdm2.til","Media\tileset.bmp")
'load an alternative tileset
newset1=LoadImage("Media\negative.bmp")
newset2=LoadImage("Media\tileset.bmp")
'add some info onto screen
Color cborange
AddText "Press SPACE to change tileset"
Repeat
'change the look?
If KeyHit(cbkeyspace) Then
set=Not set
If set Then
PaintObject map,newset1
Else
PaintObject map,newset2
EndIf
EndIf
'don't forget this!
DrawScreen
Until EscapeKey()
GhostObject
This command sets the transparency of an object. Please notice that CoolBasic uses software engine for transparency calculations. No hardware acceleration. It's very CPU-intensive process, so use this command carefully – although it makes objects look cool :) Zero colour will not be drawn (black parts are fully transparent)
Usage:
GhostObject obj, transparency
Example:
'load a map
map= LoadObject("Media\map.bmp")
cow= LoadObject("Media\cow.bmp",72)
alpha=100
AddText "Move the cow with arrows"
AddText "A/Z to alter transparency"
Repeat
'update controls
TranslateObject cow,RightKey()-LeftKey(),UpKey()-DownKey()
TurnObject cow,5
'alter transparency
alpha= alpha+(KeyDown(cbkeya) -KeyDown(cbkeyz))
GhostObject cow,alpha
DrawGame 'draw all objects so the text can be rendered visible
Text 0,60,FPS()+" "+alpha 'show how fast the game is able to render
DrawScreen()
Forever
MirrorObject
Will mirror object horizontally or flip vertically. This is relatively CPU-intensive task, and should not be done realtime. Prefer to pre-calculate flipped/mirrored objects at program start.
Usage:
MirrorObject obj, method
Example:
'load both (animated) objects
run=LoadAnimObject("Media\zerorun1.bmp",47,51,0,12)
MaskObject run,cbmagenta
run2=CloneObject(run)
'mirror the second one (mirror&flip)
mirrorobject run2,2
'move them a bit further from each other
PositionObject run,0,50
PositionObject run2,0, -50
'animate them (continuous)
LoopObject run,2,11
LoopObject run2,2,11
'the game loop itself
Repeat
DrawScreen
Forever
ObjectRange
Sets the radius of collision circle/box around the object. By default the collision range matches the object's size. If the object uses circle-based collision, you only need to specify the first radius. On box-based collisions you can optionally also define its height.
Usage:
ObjectRange obj, diameter [,vertical_diameter]
ObjectInteger, ObjectFloat, ObjectString
These commands can write and read data to objects. You can, for example, store their name, health or other attributes. You can use these commands as functions to retrieve data.
Usage:
Object** obj, value
or
value = Object**(obj)
Example:
'reduce health
ObjectInteger obj, ObjectInteger(obj)-1
ObjectPickable
Selects the pick-mode of an object. Objects cannot be picked by default. After set, CameraPick
and ObjectPick can recognise this object. Box-pick defaults to object' original dimensions. There's an invisible box that outlines the object. You can modify the size of this box with the ObjectRange-command. Circle-based collision, in the other hand, can be more accurate and preferable way for some types of objects. Circles have only one radius. There's also a pixel-accurate method available, but it's also the heaviest. Tilemaps always use tile-based collision – which is determined by its hit-layer. For tilemaps you only need to command ObjectPickable map, ON.
Usage:
ObjectPickable obj, mode
Example:
FrameLimit 40
'Enable graphics commands to cast shapes to
'the game world
DrawToWorld ON
pacman1=LoadObject("Media\pacman.bmp",360)
pacman2=CloneObject(pacman1)
pacman3=CloneObject(pacman1)
ObjectPickable pacman1,1 'box-pick
ObjectPickable pacman2,2 'circle-pick
ObjectPickable pacman3,3 'pixel-perfect
PositionObject pacman1, -100,0
PositionObject pacman3, 100,0
'Load the shooter
guy=LoadObject("Media\guy.bmp",180)
PositionObject guy,0, -100
Color cbblack
AddText "Guide via arrows"
Locate 70,80
AddText "box-pick"
Locate 160,80
AddText "circle-pick"
Locate 260,80
AddText "pixel-perfect"
Color cbblack
ClsColor cbwhite
Repeat
'Update controls
If LeftKey() Then TurnObject guy,2
If RightKey() Then TurnObject guy, -2
If UpKey() Then MoveObject guy,2
If DownKey() Then MoveObject guy, -2
'Make them spin, add some excitement
TurnObject pacman1,1
TurnObject pacman2,1
TurnObject pacman3,1
'Pick
ObjectPick guy
DrawGame
'Something WAS picked, let's see what it is
e=PickedObject()
Color cbblack
If e Then
Color cbred
If e=pacman1 Then
Text 0,30,"pacman on the left"
ElseIf e=pacman2 Then
Text 0,30,"pacman at the center"
ElseIf e=pacman3 Then
Text 0,30,"pacman on the right"
EndIf
Circle PickedX() -5,PickedY()+5,10,ON
EndIf
'draw the check-zones
Box ObjectX(pacman1) -32,ObjectY(pacman1)+32,64,64,OFF
Circle ObjectX(pacman2) -32,ObjectY(pacman2)+32,64,OFF
DrawScreen
Until EscapeKey()
ObjectPick
Let an object do the pick according to its angle. The object makes a ray straight forward. The picked object will be the one who hits the ray first. Hidden objects can not be picked. See ObjectPickable for example code.
Usage:
ObjectPick obj
PixelPick
Let an object do the pick according to its angle. The object makes a ray straight forward. The difference to ObjectPick is that the hit can occur inside an object. Pixel-perfectly. This command is designed to be used to determine picking for images – not other 'normal' objects. The ideal usage of PixelPick is the 'instant bullet hit'. The bullet moves at infinite speed. The target object must be introduced ObjecPickable obj, 3. Hidden objects can not be picked. The optional quality-value defaults to 1 i.e. the best accuracy. Try numbers 2-10.
Usage:
PixelPick obj [, accuracy]
Example:
Const gravity= -0.02
'declare variables as decimal
Dim target_movex As Float, target_movey As Float
Dim movex As Float, movey As Float
'change the display mode (low reolution, zoomed display)
SCREEN 320,240,16,cbsizable
SetWindow "",3
DrawToWorld ON
'load the image-map (NO rotarion detail)
map=LoadObject("Media\level.bmp")
ObjectPickable map,3
'load the ship
ship=LoadObject("Media\ship.bmp",72)
Repeat
oldx#=ObjectX(ship) 'memorize the old coordinates
oldy#=ObjectY(ship)
'turn the ship
If LeftKey() Then TurnObject ship,5
If RightKey() Then TurnObject ship, -5
'thrust/update physics
If UpKey() Then
target_movex=Cos(ObjectAngle(ship))*3
target_movey=Sin(ObjectAngle(ship))*3
If target_movex< movex Then movex=movex-0.05
If target_movex> movex Then movex=movex+0.05
If target_movey< movey Then movey=movey-0.05
If target_movey> movey Then movey=movey+0.05
EndIf
'move the ship according to physics
movey=movey+gravity
If movey< -4 Then movey= -4
TranslateObject ship,movex,movey
'make the camera follow the ship
CloneCameraPosition ship
'COLLISION FOR THE LEVEL
CameraPick 160,120
If PickedObject()=map Then
PositionObject ship,oldx,oldy
movey=0
EndIf
DrawGame 'for drawing
'figure out where the pick intersects with the level
PixelPick ship
If PickedObject() Then
Circle PickedX() -5,PickedY()+5,10
EndIf
DrawScreen
Forever
ObjectLife
This is also a command/function –hybrid. You can either set an object's life or check how much life time an object has left. The lifetime is not measured in milliseconds, but in loop iterations where UpdateGame (or DrawScreen) is called. After an object's life time expires, it'll be automatically destroyed. Do not set life for master objects (when there are clones made out of it). You can calculate the required life time in seconds by taking the FrameLimit-value as described in the example below.
Usage:
ObjectLife obj, life
or
lifeLeft = ObjectLife(obj)
Example:
FrameLimit 40
pacman=LoadObject("Media\pacman.bmp")
ObjectLife pacman,200 '5*40=200
AddText "This object has 5 seconds to live..."
Repeat
DrawScreen
Until EscapeKey()