Outrun style Racing code ^.^

Miscellaneous Forums/Blitz Showcase/Outrun style Racing code ^.^

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Heres an updated bit of code... the basics of the game are there now.

framework brl.glmax2d
Import brl.random

Global scx=1024,scy=768
Graphics scx,scy,16,60
bglSetSwapInterval(1)

Global pathsize=1024	 	'This is the paths TRUE length
Global multiplier#=50		'This is how much this path is interpolated. (pathsize is multiplied by this)
Global pathlength#=pathsize*multiplier
						'This is the virtual paths length.

Global pathc2#[pathsize+1]
Global pathd2#[pathsize+1]
Global pathflag2[pathsize+1]	'Set flags for what is on the track:
							'1=streetlamps,2=left wall,3=right wall,4=tunnel
Global drawamt=200		'Amount of track to draw in one go
Global drawbit[drawamt+1]	'Buffer used to store which bits of track need to be drawn
Global maxcars=50		'500 cars at one time on the track!?!?! lower this for less ;)
Global carx#[maxcars+1]	'Where on the track the car is (left-right. Values -1 to +1)
Global cary#[maxcars+1]	'Not used yet- Maybe add jumping?
Global carpos#[maxcars+1]	' where along the path each car is stored
Global drawcar[maxcars+1] 	'Flag for each car- does it get drawn?
Global drawx[maxcars+1]	'X coordinate for drawing car. Set when you set Drawcar[car]=1
Global drawy[maxcars+1]	'y coordinate for drawing car. Set when you set Drawcar[car]=1
Global drawsz#[maxcars+1]	'size for drawing car. Set when you set Drawcar[car]=1
Global carspeed#[maxcars+1]  'Speed of car. Best results are between 1 and 2. values 0-2 work good.
Global player=1			'Player's car number

'Position the cars randomly
For n=2 To maxcars
carx#(n)=Rnd(-.8,.8)
carspeed(n)=rnd(.6,1.1)
carpos(n)=Rand(pathlength)
Next

'Generate the level data using random info
c#=0
d#=0

targd#=Rand(-5,5)
targc#=Rand(-5,5)
For n=multiplier*10 To pathlength
If Rand(0,multiplier)>multiplier-(multiplier/100) Then pathflag2(n/multiplier)=Rand(1,4)

If Rand(0,100)>99 Or cnt>multiplier*10 Then cnt=0;tc#=targc;targc#=Rand(-3,3);If Rand(0,10)>8 Or tc<>0 Then targc=0;cnt=-1
If cnt>-1 Then cnt=cnt+1
If Rand(0,100)>90 Then targd#=Rand(-3,4);If Rand(0,10)>2 Then targd=0
If n=0 Or n>pathlength-100 Then targd=0;targc=0

mc#=(c#-targc#)/10.0;If Abs(mc)>.1 Then mc=Sgn(mc)*.1
c#=c#-mc

md#=(d#-targd#)/10.0;If Abs(md)>.1 Then md=Sgn(md)*.1
d#=d#-md
d=d*.9
c=c*.9
pathc2[n/multiplier]=C
pathd2[n/multiplier]=d*.01
Next

dmult#=0.985
ymult#=1
pathpos#=0
Repeat

For n=1 To maxcars
carpos(n)=carpos(n)+carspeed(n)
carpos(n)=carpos(n) Mod pathlength
If carpos(n)<0 Then carpos(n)=carpos(n)+pathlength
carpos(n)=carpos(n)
drawcar(n)=0
Next


pathpos=(pathpos+1) Mod pathlength
pathpos2#=pathpos
Cls
pos#=pathpos
y#=scy
x#=scx/2.0
z#=scx/2.0

d#=10
D2#=10*120
oy#=y
miny#=scy
x=(scx/2.0)-carx(player)*scx
mv#=((pathc(pathpos)-pathc(opathpos))/10.0)
carx(player)=carx(player)+(KeyDown(key_right)-KeyDown(key_left))*.02
carspeed(player)=carspeed(player)+(KeyDown(key_up)-KeyDown(key_down))*.01
cnt=0
dc#=0
xc#=0
Repeat
ok=0
d#=d#/(1.03*1.03*dmult)
D2=D2/(1.029*1.03*dmult)
d4#=d#;If d4<1 Then d4=1
drawbit(cnt)=0
If y<=miny Then miny=y;drawbit(cnt)=1

'Change X value to make the tracks corners work properly

x=x+Float(pathc(pos)*(10.0-d)/5.0)*d4*2
x=x-Float(pathc(pathpos2)*(10.0-d)/5.0)*d4*2



'Change y value to make the tracks corners work properly and give perspective
dc#=dc#+pathd(pos)
y=y+dc
y=y-d*1.5*ymult

cnt=cnt+1;If cnt>drawamt Then ok=1	'set the OK flag if weve gone through enough track.
pos=(pos+1) Mod pathlength
Until ok=1 Or KeyDown(key_escape)

SetColor 70,50,200;DrawRect 0,0,scx,miny-1 'draw sky


'Okay, now lets do it all again backwards and draw stuff at the same time.
od3#=0
Repeat
cnt=cnt-1
	pos=(pos-1);If pos<0 Then pos=pos+pathlength
	ok=0
	d#=d#*(1.03*1.03*dmult)
	D2=D2*(1.029*1.03*dmult)
	d4#=d#;If d4<1 Then d4=1
	x1=x-(pathc(pos))-d2
	x2=x+(pathc(pos))+d2
	dq=0
	If Abs(y-oy)>.01 Then dq=1
	y2#=Abs(y-oy)
	If drawbit(cnt)=1 Then

		'grass colouring
			If (Int(pos/8) Mod 2)=0 Then SetColor 0,180,0 Else SetColor 0,200,0
		'draw the grass
		If dq=1 Then
			Drawquad 0,y,x1,y,ox1,oy,0,oy
			Drawquad scx,y,x2,y,ox2,oy,scx,oy
		EndIf

		'Draw track "edges"
		If (Int(pos/2) Mod 2)=0 Then SetColor 255,0,0 Else SetColor 255,255,255
		drawquad x1-d*16,y,x1,y,ox1,oy,ox1-d*16,oy
		drawquad x2,y,x2+d*16,y,ox2+d*16,oy,ox2,oy
'		drawquad x1-d*16,y,x1,y,ox1+od#*16,oy,ox1,oy
'		drawquad x2+d*16,y,x2,y,ox2+od*16,oy,ox2,oy

		
		'tarmac colouring
		If (Int(pos/7) Mod 2)=0 Then SetColor 120,120,120 Else SetColor 80,80,80
		
		'draw tarmac
		If dq=1 Then
			drawquad x1-1,y,x2+1,y,ox2+1,oy,ox1-1,oy
		EndIf
		'Draw white strip
		If (Int(pos/9) Mod 2)=0 And dq=1 Then 
			SetColor 255,255,255
			Drawquad x-d,y,x+d,y,ox+od#,oy,ox-od#,oy
		EndIf


		SetColor 255,255,255
		For n=1 To maxcars
			If Int(carpos[n])=Int(pos) Then 'Draw car
				drawcar(n)=1;drawx(n)=x+carx(n)*(d2);drawy(n)=y;drawsz#(n)=d*10	'set flag and store where to draw the cars!?
				DrawRect drawx(n)-drawsz(n)/2.0,drawy(n)-drawsz(n)/2.0,drawsz(n),drawsz(n)/2.0
			EndIf
		Next

	EndIf

	'Draw tunnels and stuff outside of the "should i draw" check above
	d3#=d
	If pathflag(pos)=1 then
		SetColor 255,255,255
		If Int(pos Mod 20)=10 Then DrawRect x2+(d3*40),y-(d3*100),d3*10,d3*100
		If Int(pos Mod 20)=0 Then DrawRect x1-(d3*40),y-(d3*100),d3*10,d3*100'DrawLine x1,y-(d*100),x1,y
	endif
	
	drawtunnel=pathflag(pos)-1;If drawtunnel<0 Then drawtunnel=0
	If drawtunnel>0 Then 
		SetColor 10,100,20
		If drawtunnel=1 Or drawtunnel=3 Then
			 Drawquad x2+(d3*25),y-(d3*100),ox2+(od3#*25),oy-(od3*100),ox2+(od3*25),oy,x2+(d3*25),y
		EndIf
		If drawtunnel=2 Or drawtunnel=3 Then 
			 Drawquad x1-(d3*25),y-(d3*100),ox1-(od3#*25),oy-(od3*100),ox1-(od3*25),oy,x1-(d3*25),y
		EndIf
		If drawtunnel=3 Then
			DY1=y-(d3*100)+2
			DY2=Oy-(Od3*100)+2
			SetColor 0,60,0
			DRAWQUAD X1-(D3*25),DY1,X2+(D3*25),DY1,oX2+(oD3*25),DY2,ox1-(od3*25),dy2
		EndIf
	EndIf
	
	
	
	'Store old values. 
	od3#=d3#
	ox1=x1
	ox2=x2
	ox=x
	oy=y
	od#=d#
	
	
	
	'Change X value to make the tracks corners work properly
	x=x-Float(pathc(pos)*(10.0-d)/5.0)*d4*2
	x=x+Float(pathc(pathpos2)*(10.0-d)/5.0)*d4*2


	
	'Change y value to make the tracks corners work properly and give perspective
	dc#=dc#-pathd(pos)
	y=y-dc
	y=y+d*1.5*ymult

	
	If cnt<1 Then ok=1  'set the OK flag if weve drawn enough track.
Until ok=1 Or KeyDown(key_escape)

opathpos=pathpos
DrawText pathpos,10,10
pathpos=carpos(player)-10
If pathpos<0 Then pathpos=pathpos+pathlength
bglSwapBuffers()
FlushMem
Until KeyDown(key_escape)
End


Function drawquad(x1#,y1#,x2#,y2#,x3#,y3#,x4#,y4#)
Local poly#[]=[Float(x1),Float(y1),Float(x2),Float(y2),Float(x3),Float(y3),Float(x4),Float(y4)]
DrawPoly poly
End Function

Function pathc#(pos#)
pos=pos/multiplier
pos=pos Mod pathsize
Local pos2=(pos+1) Mod pathsize
val1#=pathc2#(Floor(pos))
val2#=pathc2#(Floor(pos2))
val3#=pos#-Floor(pos#)
Return interpolate#(val1#,val2#,val3#)
End Function

Function pathd#(pos#)
pos=pos/multiplier
pos=pos Mod pathsize
Local pos2=(pos+1) Mod pathsize
val1#=pathd2#(Floor(pos))
val2#=pathd2#(Floor(pos2))
val3#=pos#-Floor(pos#)
Return interpolate#(val1#,val2#,val3#)
End Function

Function pathflag(pos)
Return Floor(pathflag2((pos/multiplier) Mod pathsize))
End function

Function interpolate#(val1#,val2#,decimal#)
Return Float(val1)+Float(Float(val2)-Float(val1))*decimal
End Function



I dont realy have time to do much with this but im trying to get to grips with max so i can get on with a larger project. If you see any ways to improve the efficiency, let us know :)