In addition to Orto's post, using a "find free object" function (or simillar) allows for easier object management when loading levels for a game, which is especially handy when hundreds or even just dozens of objects are being used. It makes saving the level data easier as object numbers are automatically assigned as they are loaded in.
`Driving game
`By 29 Games
`13 July 2011
`controls: arrow keys and spacebar
`This program has been designed so that the map is completed controlled by the data statements
`so that to create a new map the code itself does not need to be edited
`I have highlighted where there are things that make no sense what-so-ever (see the o_time variable on line 30)
sync on
sync rate 60 :`anything more than 60 is just showing off
hide mouse
autocam off
randomize timer()
cls :`this is a hang up from the fact this was originally written in DBC (stops screen flicker)
`some constants
accl# = 0.2 :`forward acceleration of the car
brak# = 0.2 :`braking acceleration of the car (braking's for wimps)
turn# = 2.0 :`turning speed
max_spd# = 3.0 :`maximum forward speed of the car (excluding boost)
cam_ran = 30 :`horizontal distance the camera is behind the car
cam_hgt = 10 :`how high the camera is from the car
cam_tip = 10 :`angle of the camera pointing downward
fric# = 0.1 :`friction of the car against the road (acts to slow the car down)
f_time# = 999.9 :`initial fastest time
o_time# = f_time# :`this doens't actually appear in the rest of the code and I have no idea what it's for
scr = 0 :`initial score (i.e. how many cubes have been collected at the start of the game)
sw = screen width()
sh = screen height()
`this loop reads the data for the buildings and creates and positions them
do
read w,d, x,z :`w = width, h = height, x = x position and z = z postion of the building
if w = 0 :`if the width of the building = 0 then exit the loop
exit :`this allows the map to be completely controlled by the data
endif
inc ob :`increment the object number
h = 2*(rnd(100)+50) :`randomize the height to give a pleasing city look (and saves space in the data statements)
make object box ob, w,h,d :`make a box to represent the building
color object ob, rgb(100,h,100) :`color the building, which is partly based on its height
ghost object on ob :`so you can see the cubes you need to collect and I think it looks cool
position object ob, x,h/2,z :`position the building
loop
n_blk = ob :`the number of buildings in the game (used for the collision detection)
`this loop reads the data for the cubes
do
read x,z :`x = x position and z = z position of the cube
if x = 0 :`if the x position of the cube = 0 then exit the loop
exit :`this allows the map to be completely controlled by the data
endif
inc ob :`increment the object number
inc m_scr :`m_scr shows what the maximum possible score can be (i.e. equal to the number of cubes)
make object cube ob, 10 :`make the cube
color object ob, rgb(255,255,100)
xrotate object ob, 45
zrotate object ob, 45
fix object pivot ob
position object ob, x,5,z :`position the object
loop
`start point marker cone and car starting position
read xp#,zp#,face# :`xp# = x position of car, zp# = z position of car, face# = direction car faces
inc ob :`increment the object number
make object cone ob, 100 :`create a marker object to show the start position
set object collision off ob
color object ob, rgb(50,255,50)
position object ob, xp#,50,zp#
inc ob :`ob is now the ident number of the car
make object box ob,7,2,10
position object ob, xp#,1,zp# :`position the car at the starting point and point in the right direction
`main game loop
do
`save the old position of the car and it's old facing (used in collision)
o_xp# = object position x(ob)
o_zp# = object position z(ob)
o_face# = face#
`controls
`arrow keys to accelerate forward, brake (wimp), turn left and right
if upkey() = 1
spd# = spd# + accl#
endif
if downkey() = 1
spd# = spd# - brak#
bst# = 0.0
endif
if leftkey() = 1
face# = wrapvalue(face# - turn#)
endif
if rightkey() = 1
face# = wrapvalue(face# + turn#)
endif
`press space key to boost (i.e. extra speed)
if spacekey() = 1
bst# = bst# + 0.5 :`boost speed
endif
`calcualte speed
spd# = spd# + bst# - fric# :`apply boost speed and friction to calculate final speed
bst# = bst# - fric# :`reduce boost speed due to friction
bnc# = bnc# - 0.5 :`reduce bounce speed, techically due to friction but of a different value (see collision section)
`limit speed
if spd# <= 0.0
spd# = 0.0 :`stops friction from pushing the car backward
endif
if spd# > max_spd#+bst#
spd# = max_spd#+bst# :`limits the maximum speed of the car
endif
if bst# > 5.0
bst# = 5.0 :`limits the maximum boost speed (the 5.0 should really be put into a variable at the start of the program)
endif
if bst# <= 0.0
bst# = 0.0 :`prevents boost speed from becoming negative and pulling the car backward
endif
if bnc# <= 0.0
bnc# = 0.0 :`stops bounce speed from becoming negative and pulling the car backward
endif
`move the car, rotate the cara and position camera
move object ob, spd# - bnc#
yrotate object ob, face#
set camera to follow object position x(ob), cam_hgt, object position z(ob), face#, cam_ran, cam_hgt, 5, 0
xrotate camera cam_tip
`spin the yellow cubes
`this, for some bizarre reason, was originally between the acclerate forward and brake controls so I moved it here.
for i = n_blk + 1 to ob - 1
turn object left i,2
next i
`collision
ob_num = object collision(ob,0) :`ob_num is the object ident that the car has collided with
`car has hit a building (n_blk is the maximum number of buidlings in the map
if ob_num > 0 and ob_num =< n_blk
bnc# = spd#*1.5 :`set the speed that the car bounces off the building
spd# = 0.0 :`set forward speed to zero
position object ob, o_xp#, 1, o_zp# :`place car to its old position
face# = o_face# :`rotate car back to its old facing
endif
`car has hit one the cubes
if ob_num > n_blk
inc scr :`increase the score by one (printed to the screen as number of cubes collected rather than a score)
hide object ob_num :`hide the cube
set object collision off ob_num :`turn the collision off
if scr = 1
start_time# = timer() :`if score = 1 then start the timer for the game
endif
endif
`if the player has collected all the cubes the record the time it took
if scr > 0 and scr < m_scr
time# = (timer() - start_time#)/1000.0 :`time = time taken to collect all the cubes
endif
`outro loop
if scr = m_scr
if time# < f_time#
nft = 1 :`nft = new fastest time flag
f_time# = time# :`set fastest time
endif
`press return to play again
if returnkey() = 1
nft = 0 :`set new fastest time flag to zero
`show all the cube and turn the collision back on
for i = n_blk + 1 to ob - 2
show object i
set object collision on i
scr = 0 :`set score back to zero (should be outside of the for to next loop, don't know how it ended up here)
time# = 0.0 :`set current race time to zero (should be outside of the for to next loop, don't know how it ended up here)
next i
endif
endif
`print stuff to the screen
set cursor 0,0
print screen fps()
print "arrows keys to move, spacebar to boost"
print
print "CURRENT TIME : ", time#
print "FASTEST TIME : ", f_time#
print "CUBES COLLECTED : ", scr , " OF ", m_scr
`if all he cubes have been collected then print "game over" info to screen
if scr = m_scr
if nft = 0
center text sw/2, sh/2-75, "FINISHED"
else
center text sw/2, sh/2-75, "!! NEW FASTEST TIME !!"
endif
center text sw/2, sh/2-50, "PRESS ENTER TO RESET CUBES"
endif
sync
loop
`building data
`you can add and delete any of the building data without having to edit any of the code
data 100,300,250,150 :`width, height, x position, z position
data 100,300,450,150
data 100,100,650,250
data 100,300,850,350
data 100,200,1050,300
data 100,180,1250,290
data 80,80,1360,340
data 220,100,210,450
data 160,100,420,450
data 100,100,650,450
data 100,100,1250,450
data 180,100,1410,450
data 100,200,1050,600
data 340,100,170,650
data 140,100,430,650
data 100,100,650,650
data 100,180,850,690
data 200,100,1220,650
data 260,100,1470,650
data 200,100,200,850
data 300,300,550,950
data 100,100,850,850,
data 140,100,1070,850
data 180,100,1250,850
data 140,100,1430,850
data 100,100,50,1050
data 100,100,250,1050
data 100,140,850,1070
data 100,160,1050,1080
data 100,100,1250,1050
data 100,300,1450,1150
data 100,140,850,1230
data 100,120,1050,1240
data 100,300,150,1350
data 200,100,400,1250
data 100,160,650,1280
data 100,100,1250,1250
data 200,100,400,1450
data 100,320,650,1540
data 100,200,850,1500
data 300,100,1150,1450
data 200,100,400,1650
data 100,100,1050,1650
data 0,0,0,0 :`this marks the end of the building data and is used to exit the loop (do not delete)
`collectable cube data
`you can add and delete any of the collectable cube data without having to edit any of the code
data 350,250 :`x position and z position
data 350,650
data 350,950
data 350,1350
data 850,1350
data 1350,1250
data 1050,1170
data 850,950
data 950,550
data 1310,390
data 0,0 :`this marks the end of the collectable data and is used to exit the loop (do not delete)
`car staring position and facing
`this can be edited so that the car (and start point marker cone) can be moved without having to edit the code
data 350.0,-200.0,0.0 :`x position, z position, facing
The demo is a driving game (one of my 20 line challenges) and instead of using a "find free object" function I just simply increment the object number "ob" and then assign this to the objects as they are created. This means that I don't have to store any information about how many objects are in the level.
I've noticed that most people use some kind of "find free object" function. Some people write their own but I believe there is one that comes with IanM's utility pluggins. Which ever way you do it, it simply makes handling large numbers of objects easier.
one of these days I'll come up with a better signature