Hello,
I bought DBP a long time back but just started into it this month. I'm trying to put together a driving game. Here is some of the code i have picked up from the forums here and pieced together. My question is how would i make the car stop when the fuel gauge reached empty and go again when the fuel is refilled?
Please and Thank you
SET DISPLAY MODE 1280, 1024, 32
sync on
sync rate 60
backdrop on
autocam off
set camera range 0.5, 30000
phy start
phy set continuous cd 1
` movement
g_fSpeed# = 0.05
g_fTurn# = 0.3
` set the directory to media
set dir "media"
` load base and detail texture
load image "texture.bmp", 101
load image "detail.tga", 102
make object terrain 1 ` create the terrain object
set terrain heightmap 1, "map.bmp" ` set the heightmap
set terrain scale 1, 12, 0.6, 12 ` set the scale
set terrain split 1, 8 ` split value by 16 * 16
set terrain tiling 1, 1 ` detail map tiling
set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 1, 101, 102 ` base and detail texture
build terrain 1 ` finally build the terrain
phy make rigid body static terrain 1
`Load buggy
load object "beach.x", 2
position camera 1700,23,1700
a# = get terrain ground height( 1, camera position x( ), camera position z( ) )
` now position the camera slightly above the terrain
position camera camera position x( ), a# + 3, camera position z()
position object 2, camera position x(), camera position y(), camera position z()
load image "beachBu2.tga",2
load image "dbprocubemap.dds",3
texture object 2,2 : set blend mapping on 2,1,3,2,16
offset limb 2,0,0,-0.5,0
rotate limb 2,0,0,-90,0
`Create vehicle physics body
width# = 0.75
height# = 0.4
length# = 2.2
wheelX# = 1.1
wheelY# = 0.3
wheelZ# = 1.5
radius# = 0.75
wheelHeight# = 0.2
global prevforce as float
global prevforce2 as float
global wheelmult as float
global vel as float
global lasttimer as dword : lasttimer = timer()
global curtimer as dword
global phyupdatetime as dword : phyupdatetime = 10 `minimum time before next physics update in miliseconds
global physicsframecounter
global physicsfps
global physicsfpstimer
global camang as float
global camdist as float : camdist = 7.5
`Be aware the vehicle width and length orientation
phy create vehicle 2
phy add vehicle body 2, length#, height#, width#, 0.0, height#, 0.0
phy add vehicle body 2, width#, height#, width#, 0.0, height#*2, 0.0
phy add vehicle wheel 2, 5, wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 0, 0
phy add vehicle wheel 2, 7, wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 0, 1
phy add vehicle wheel 2, 3, -wheelZ#, wheelY#, -wheelX#, radius#, wheelHeight#, 1, 0
phy add vehicle wheel 2, 9, -wheelZ#, wheelY#, wheelX#, radius#, wheelHeight#, 1, 1
phy set vehicle max motor 2, 100.0
steeringDelta# = 0.1 : phy set vehicle steering delta 2, steeringDelta#
phy set vehicle static friction 2, 50
phy set vehicle max steering angle 2, 0.4
phy set vehicle auto 2, 0
phy set vehicle wheel multiplier 2,0
phy set vehicle suspension spring 2,10000
phy set vehicle mass 2, 1200
phy build vehicle 2
phy set rigid body angular damping 2, 1
phy set rigid body mass offset local 2, -.25, -.75, 0
`follower object invisible
make object sphere 3,10 : hide object 3
` reset the directory
set dir "..\.."
` position the camera
hide mouse
` main program loop
color backdrop rgb(175,200,255)
fog on
fog color 175, 200, 255
fog distance 1000
global buggynum : buggynum = 2
rem XY location of gas bar on the display
gx = 1130
gy = 212
rem How many pixels long do we want the gas bar
pixlength = 100
pixheight = 10
rem Minimum, Maximum, and Current Gas
MinHP# = 0
MaxHP# = 100
CurHP# = 100
do
phy set vehicle lateral tire force function 2, 0, 0.5, 2.0, 4.0, 1.0, 300.0 / ((vel * .1) + .01)
phy set vehicle lateral tire force function 2, 1, 0.5, 2.0, 4.0, 1.0, 300.0 / ((vel * .1) + .01)
phy set vehicle lateral tire force function 2, 2, 0.5, 2.0, 4.0, 1.0, 300.0 / ((vel * .1) + .01)
phy set vehicle lateral tire force function 2, 3, 0.5, 2.0, 4.0, 1.0, 300.0 / ((vel * .1) + .01)
set camera to follow object position x(2),limb position y(2,5),object position z(2),limb angle y(2,5) - 180,camdist,camdist /2.0, 20, 0
point camera object position x(2),object position y(2),object position z(2)
rem Control buggy
phy set vehicle motor force 2,0.0
gosub driveCar
information()
rem Update physics and screen
update terrain
phy update
rem gas meter*******************************************************************************************
gasgauge(gx,gy,MinHP#,MaxHP#,CurHP#,pixlength,pixheight)
rem Decrease the gas
if upkey ( )=1 or downkey ( )=1 and CurHP# > MinHP#
CurHP# = CurHP#-0.002
endif
rem Increase the gas using g key
if keystate(34)=1 and CurHP# < MaxHP#
CurHP# = CurHP#+0.05
endif
` final screen update
sync
loop
driveCar:
key = 0 : force#=0.0
if upkey ( ) = 1 then force#=300 : key = 1 : wheelmult = 50 : prevforce = 300
if downkey ( ) = 1 then force#=-200.0 : key = 1 : wheelmult = -50 : prevforce = -200
if spacekey ( ) `brake
phy set vehicle brake torque 2, 750
else
phy set vehicle brake torque 2, 0
endif
if key = 0 then force#=0.0
phy set vehicle motor force 2,force#
phy set vehicle wheel multiplier 2, wheelmult
`
rem steering
steeringAngle# = phy get vehicle steering angle ( 2 )
key = 0
if keystate ( 205 ) = 1
if steeringAngle# > ( -1.0 + steeringDelta# )
steeringAngle# = steeringAngle# - ( steeringDelta# / 2 )
endif
key = 1
endif
if keystate ( 203 ) = 1
if steeringAngle# < ( 1.0 - steeringDelta# )
steeringAngle# = steeringAngle# + ( steeringDelta# / 2 )
endif
key = 1
endif
if key = 0
if steeringAngle# > 0.0
steeringAngle# = steeringAngle# - steeringDelta# * 2
if steeringAngle# < 0
steeringAngle# = 0.0
endif
endif
if steeringAngle# < 0.0
steeringAngle# = steeringAngle# + steeringDelta# * 2
if steeringAngle# > 0
steeringAngle# = 0.0
endif
endif
endif
phy set vehicle steering angle 2, steeringAngle#
phy set vehicle steering value 2, -( steeringAngle# * 45 )
return
function information()
` show some information
` start printing at top of screen
velx as float
vely as float
velz as float
`vel as float
fwdrev as string
fwdrev = "none"
velx = phy get rigid body linear velocity x (2)
vely = phy get rigid body linear velocity y (2)
velz = phy get rigid body linear velocity z (2)
`velw =
vel = sqrt((velx * velx) + (vely * vely) + (velz * velz))` * (wheelmult / 50)
set cursor 0, 0
` show frame rate
print "fps = " + str$ ( screen fps ( ) )
print ""
` current camera position
print "Motor Force = " + str$( phy get vehicle motor force(2) )
print "Velocity = " + str$(vel)
print "Fwd/Rev = " + fwdrev
print ""
print "x = " + str$ ( object position x (2) )
print "y = " + str$ ( object position y (2) )
print "z = " + str$ ( object position z (2) )
print ""
` finally the polygon count
print "polygon count = " + str$ ( statistic ( 1 ) )
print ""
endfunction
function gasgauge(gx as integer, gy as integer, MinHP# as float, MaxHP# as float, CurHP# as float, pixlength as integer, pixheight as integer )
rem some conditions so that you don't go negative on certain values
if CurHP# < MinHP# then CurHP# = MinHP#
if CurHP# > MaxHP# then CurHP# = MaxHP#
if pixlength < 1 then pixlength = 1
if pixheight < 1 then pixheight = 1
rem Change our values from Integers to a percent so that we can see how much of the bar needs to be filled
pBar# = CurHP# / MaxHP#
rem Get the percentage of the bar to be filled
thebar = int(pixlength * pBar#)
rem Draw the bar
box gx,gy, gx + thebar, gy + pixheight
line gx + pixlength, gy, gx + pixlength, gy + pixheight
line gx,gy, gx, gy + pixheight
endfunction
Function rotcam(ang#,radius#,obj)
x#=object position x(obj)
y#=object position y(obj)
z#=object position z(obj)
position camera x#+(sin(ang#)*radius#),y#+(radius#/2.0),z#+(cos(ang#)*radius#)
point camera x#,y# + 1,z#
Endfunction