`Racing Game Challenge
 
`Standad Setup
Sync on : Sync rate 60
Set display mode 1024,768,16 : Hide mouse
Autocam off : set camera range 1,99999999999
 
`Constants
#constant StartLoop        for DL=1 to 0x7fffffff
#constant EndLoop          next DL
 
`Game Variables
grav#=15.0
backcl=rgb(240,102,0)
fogdist=6000
amb=30
 
`Player Variables
id=1
maxspeed#=70.0
scspeed#=20.0
trac#=0.90 : itrac#=trac#
atrac#=0.98
tracd#=0.0006
traci#=0.01
mtrac#=0.9999
acc#=2.0
turnd#=15.0
mturn#=3.0
fxang#=20.0
 
`Create Player
Create_Car(id)
Color_Car(id,rgb(129,105,105))
make object plain id+1,50,100 : xrotate object id+1,90 : fix object pivot id+1
color object id+1,rgb(0,0,0) : set object ambient id+1,0
set object rotation zyx id+1
 
`Create powerslide particles
create bitmap 1,128,128 : set current bitmap 1
for d=1 to 500
ink rgb(rnd(128),rnd(128),rnd(128)),0 : dot rnd(128),rnd(128)
next d
for d=1 to 100
ink rgb(0,0,0),0 : dot rnd(128),rnd(128)
next d
ink rgb(128,128,128),0
circ(64,40,64,64) : circ(50,70,64,64) : circ(70,70,64,64)
blur bitmap 1,6 : get image 50,0,0,128,128
set current bitmap 0 : delete bitmap 1
particles=20
dim particles(1) : particles(1)=particles
dim particlex#(particles)
dim particley#(particles)
dim particlez#(particles)
dim particlexvel#(particles)
dim particleyvel#(particles)
dim particlezvel#(particles)
dim particleangx#(particles)
dim particleangy#(particles)
dim particleangz#(particles)
dim particlelife(particles)
for p=1 to particles(1)
particlex#(p)=emx#
particley#(p)=emy#
particlez#(p)=emz#
next p
for t=1 to particles(1)
make object cube 50+t,100
hide object 50+t : texture object 50+t,50
set object transparency 50+t,1 : set object ambient 50+t,0
next t
 
`Create world
create bitmap 1,128,128 : set current bitmap 1
cls rgb(0,128,0)
for d=1 To 10000
ink rgb(0,rnd(255),rnd(50)),0
dot rnd(128),rnd(128)
next d
blur bitmap 1,4
get image 10,0,0,128,128
set current bitmap 0 : delete bitmap 1
make matrix 1,10000,10000,50,50
for x=0 to 49
for z=1 to 49
gh#=50-(abs(sin((x*20))*(200*(x/10))))
gh#=gh#+(50.0-(abs(sin((z*20))*(200*(x/10)))))
gh#=gh#/2.0
set matrix height 1,x,z,gh#
next x
next z
for z=1 to 49
for x=1 to 49
h8#=get matrix height(1,x,z-1)
h4#=get matrix height(1,x-1,z)
h#=get matrix height(1,x,z)
h2#=get matrix height(1,x,z)
x1#=(x-1)*25.0 : y1#=h#
x2#=(x+0)*25.0 : y2#=h4#
dx#=x2#-x1#
dy#=y2#-y1#
ax#=atanfull(dx#,dy#)
ax#=wrapvalue(90-ax#)
z1#=(z-1)*25.0 : y1#=h2#
z2#=(z+0)*25.0 : y2#=h8#
dz#=z2#-z1#
dy#=y2#-y1#
az#=atanfull(dz#,dy#)
az#=wrapvalue(90-az#)
nx#=sin(wrapvalue(ax#+45))
ny#=cos(ax#)
nz#=sin(wrapvalue(az#+45))
set matrix normal 1,x,z,nx#,ny#,nz#
next x
next z
prepare matrix texture 1,10,1,1
update matrix 1
fog on
backdrop on
fog distance fogdist
fog color backcl
color backdrop backcl
set ambient light amb
color ambient light backcl
 
`*Main Loop*
StartLoop
 
`Controls Elements
Gosub _Control_Player
Gosub _Control_Effects
Gosub _Control_Camera
Gosub _Interfaces
 
`*End Loop*
If escapekey()=1 then end
Sync
EndLoop
 
`*Subroutines*
 
`Control Player
_Control_Player:
 
`Controls
oldx#=x#
oldy#=y#
oldz#=z#
if y#<truey#+1.0
   if upkey()=1 then inc speed# : xspeed#=sin(ay#)*speed# : zspeed#=cos(ay#)*speed# : trac#=trac#+traci#
   if upkey()=0 then trac#=trac#-tracd# : dec speed#
      turn#=movespeed#/turnd#
   if leftkey()=1 then ay#=wrapvalue(ay#-turn#)
   if rightkey()=1 then ay#=wrapvalue(ay#+turn#)
endif
 
`Movement + Physics
if trac#>=mtrac# then trac#=mtrac#
if trac#<=itrac# then trac#=itrac#
if speed#>=maxspeed# then speed#=maxspeed#
if speed#<=0.0 then speed#=0.0
if y#<truey#+1.0
   xspeed#=xspeed#*trac#
   zspeed#=zspeed#*trac#
else
   xspeed#=xspeed#*atrac#
   zspeed#=zspeed#*atrac#
endif
x#=x#+xspeed#
z#=z#+zspeed#
moveang#=atanfull(x#-oldx#,z#-oldz#)
movespeed#=sqrt((x#-oldx#)^2+(z#-oldz#)^2)
if upkey()=0 and downkey()=0
   if leftkey()=0 and  rightkey()=0
      if movespeed#>=scspeed#
         if y#<truey#+1.0
            ay#=curveangle(moveang#,ay#,fxang#)
         endif
      endif
   endif
endif
 
`Ground Tilting and jumping off
truey#=get ground height(1,x#,z#)+30
if y#<=truey# then y#=truey#
y#=y#+yspeed#
if y#=truey#
   yspeed#=y#-oldy#
else
   yspeed#=-grav#
endif
out#=1
frontx#=newxvalue(x#,ay#,out#)
frontz#=newzvalue(z#,ay#,out#)
backx#=newxvalue(x#,ay#,out#*-1)
backz#=newzvalue(z#,ay#,out#*-1)
leftx#=newxvalue(x#,wrapvalue(ay#-90),out#)
leftz#=newzvalue(z#,wrapvalue(ay#-90),out#)
rightx#=newxvalue(x#,wrapvalue(ay#+90),out#)
rightz#=newzvalue(z#,wrapvalue(ay#+90),out#)
front#=get ground height(1,frontx#,frontz#)
back#=get ground height(1,backx#,backz#)
left#=get ground height(1,leftx#,leftz#)
right#=get ground height(1,rightx#,rightz#)
ax#=wrapvalue(curveangle((back#-front#)*30,object angle x(id),5.0))
az#=wrapvalue(curveangle((right#-left#)*30,object angle z(id),5.0))
 
`Update Player
position object id,x#,y#,z#
if y#<truey#+1.0 then rotate object id,ax#,ay#,az#
 
`Update shadow
position object id+1,x#,truey#-29,z#
rotate object id+1,ax#,ay#,az#
 
`Check if powersliding
if ay#<wrapvalue(moveang#-70) and ay#>wrapvalue(moveang#-110) and y#<truey#+1.0 and speed#>0.0 or ay#>wrapvalue(moveang#+70) and ay#<wrapvalue(moveang#+110) and y#<truey#+1.0 and speed#>0.0
   powerslide=1
else
   powerslide=0
endif
 
Return
 
 
`Control Effects
_Control_Effects:
 
`Powerslide particles
scx#=x#-sin(ay#)*50
scz#=z#-cos(ay#)*50
scy#=y#-10.0
if powerslide=1 then powerparts=0 else powerparts=1
   particle(scx#,scy#,scz#,particles(1),2.0,4.0,2.0,20,80.0,80.0,0.01)
   control_particle(0,-0.3,0,0.95,0.95,0.95,1.0,1.0,0.0,1,1,1,powerparts)
 
Return
 
 
`Control Camera
_Control_Camera:
 
`Camera
ca#=curveangle(ay#,ca#,20.0)
   cx#=x#-sin(ca#)*400.0
   cz#=z#-cos(ca#)*400.0
   cy#=curvevalue(y#+200.0,cy#,10.0)
      position camera cx#,cy#,cz#
      point camera x#,y#,z#
 
Return
 
 
`Interfaces
_Interfaces:
 
speed$="Speed: "
Border_text(10,10,speed$,rgb(255,255,255),rgb(255,0,255))
Border_text(10+text width(speed$),10,str$(int(Movespeed#))+" mph",rgb(255,255,255),rgb(255,0,0))
 
Return
 
`*Functions*
Function Create_Car(id)
create bitmap 1,100,100 : set current bitmap 1
cls rgb(0,0,0) : ink rgb(128,128,128),0
for x=1 to 100 : for y=1 to 100
if sqrt((x-50)^2+(y-50)^2)<50 then box x,y,x+1,y+1
next x : next y
get image id,0,0,100,100 : set current bitmap 0 : delete bitmap 1
make object box id,50,20,100 : make mesh from object id,id
add limb id,id,id : delete mesh id
make object box id+1,50,15,50 : xrotate object id+1,343.3 : glue object to limb id+1,id,id
make object plain id+2,20,20 : make object plain id+3,20,20
make object plain id+4,20,20 : make object plain id+5,20,20
for w=2 to 5 : yrotate object id+w,90 : glue object to limb id+w,id,id : next w
color object id,col : color object id+1,col
for c=1 to 5 : make mesh from object id+c,id+c : add limb id,id+c,id+c
delete mesh id+c : delete object id+c : next c
offset limb id,id+1,0,10.0,-23.9
offset limb id,id+2,-26,-10.0,30 : offset limb id,id+3,26,-10.0,30
offset limb id,id+4,-26,-10.0,-30 : offset limb id,id+5,26,-10.0,-30
rotate limb id,id+3,0,180,0 : rotate limb id,id+5,0,180,0
for w=2 to 5 : texture limb id,id+w,id : next w
set object cull id,0 : set object transparency id,1 : set object rotation zyx id
Endfunction
Function Color_Car(id,col)
color limb id,id,col : color limb id,id+1,col
endfunction
 
`Particles
function particle(px#,py#,pz#,particles,intx#,inty#,intz#,life,sclx#,scly#,sclz#)
t=0:p=0
repeat
t=t+1
if particlelife(t)=0
p=p+1
particlelife(t)=life + rnd(life/5)
particlex#(t)=px#
particley#(t)=py#
particlez#(t)=pz#
particlexvel#(t)=rnd(intx#)-(intx#/2)
particleyvel#(t)=rnd(inty#)-(inty#/2)
particlezvel#(t)=rnd(intz#)-(intz#/2)
show object 50+t
scale object 50+t,sclx#,scly#,sclz#
endif
if p>=particles then exit
until t>=particles(1)
endfunction
Function Control_Particle(velx#,vely#,velz#,fricx#,fricy#,fricz#,rotx#,roty#,rotz#,rot,cam,ghost,hide)
for t=1 to particles(1)
if particlelife(t)>1
particlelife(t) = particlelife(t) - 1
position object 50+t,particlex#(t),particley#(t),particlez#(t)
particlexvel#(t)=particlexvel#(t)*fricx#
particleyvel#(t)=particleyvel#(t)*fricy#
particlezvel#(t)=particlezvel#(t)*fricz#
particlexvel#(t)=particlexvel#(t)-velx#
particleyvel#(t)=particleyvel#(t)-vely#
particlezvel#(t)=particlezvel#(t)-velz#
particlex#(t) = particlex#(t) + particlexvel#(t)
particley#(t) = particley#(t) + particleyvel#(t)
particlez#(t) = particlez#(t) + particlezvel#(t)
`if particley#(t)<=0 then particlelife(t)=0
if cam=1 and rot=1
particleangx#(t)=camera angle x()
particleangy#(t)=camera angle y()
particleangz#(t)=wrapvalue(object angle z(50+t)+rotz#+rnd(particleangx#(t)/5))
endif
if cam=1 and rot=0
particleangx#(t)=camera angle x()
particleangy#(t)=camera angle y()
particleangz#(t)=0
endif
if cam=0 and rot=1
particleangx#(t)=wrapvalue(object angle x(50+t)+rotx#+rnd(particleangx#(t)/5))
particleangy#(t)=wrapvalue(object angle y(50+t)+roty#+rnd(particleangy#(t)/5))
particleangz#(t)=wrapvalue(object angle z(50+t)+rotz#+rnd(particleangz#(t)/5))
endif
if cam=0 and rot=0
particleangx#(t)=0
particleangy#(t)=0
particleangz#(t)=0
endif
rotate object 50+t,particleangx#(t),particleangy#(t),particleangz#(t)
if ghost=1 then ghost object on 50+t else ghost object off 50+t
if hide=1 then hide object 50+t else show object 50+t
endif
if particlelife(t)=1
particlelife(t)=0
hide object 50+t
endif
next t
 
endfunction
 
`Bordered text
Function Border_Text(x,y,text$,col1,col2)
ink col2,0
text x-1,y-1,text$
text x+1,y-1,text$
text x-1,y+1,text$
text x+1,y+1,text$
ink col1,0
text x,y,text$
Endfunction
 
`Circle
Function circ(x1,y1,r,t)
for x=1 to r
   for y=1 to r
      if sqrt((x-(r/2))^2 + (y-(r/2))^2)<(r/2) and sqrt((x-(r/2))^2 + (y-(r/2))^2)>(r/2)-t
         box (x1+x)-(r/2),(y1+y)-(r/2),((x1+x)-(r/2))+1,((y1+y)-(r/2))+1
      endif
   next x
next y
Endfunction