Dodge (b3d)

Miscellaneous Forums/Blitz Showcase/Dodge (b3d)

Play this game in Blitz3D
See if you can get to the top.
You just have to dodge the cubes.

You can also do replays.
Post your best replay here!
;ARROW KEYS TO MOVE
;LEFT CONTROL TO JUMP
Global rndseed2

Type thing
	Field entity
	Field pivot
	Field speedx#,speedz#,speedy#
	Field accel#
	Field speedmax#
	Field fric#
	Field w#,h#
	Field trailentity[19]
End Type

Type globals
	Field screenw,screenh
	Field camera
	Field you.thing
	Field room
	Field roomw
	Field roomh
	Field score
	Field scorehigh
	Field grav#
	Field thtrails
	Field file
	Field replaying
	Field fileline$
	Field filelinei
End Type
Global gl.globals=New globals

gl_setup()
room_setup()
ths_setup()
camera_setup()

mainloop()

Function l___________GAME()
End Function


Function mainloop()
	timer=CreateTimer(35)
	
	Repeat
		WaitTimer timer
		
		If ths_update() Then
			reset()
		EndIf
		camera_update()
	
		RenderWorld
		twoddraw()
		Flip
		
	Forever
	
End Function

Function twoddraw()
	gl\score=EntityZ(gl\you\pivot)+gl\roomh
	If gl\score>gl\scorehigh Then gl\scorehigh=gl\score
	Color 255,255,255
	Text 0,0,gl\score+" / "+gl\roomh*2+"="+gl\score*100/(gl\roomh*2)+"%"
	Text 0,15,"Best: "+gl\scorehigh+"="+gl\scorehigh*100/(gl\roomh*2)+"%"
End Function

Function reset()
	Local th.thing
	
	
	l=0
	camera_update()
	Repeat
		l=l+1
		If l>100 Then Exit
		TranslateEntity gl\camera,Sin(l*2.5)*(l/50.0),-.3,-(l/50.0)
		PointEntity gl\camera,gl\you\pivot
		RenderWorld
		twoddraw()
		Flip
	Forever
	FlushKeys
	WaitKey
	If KeyHit(1) Then finish()
	
	If gl\replaying=0 Then
		CloseFile gl\file
		gl\file=WriteFile("replay.txt")
		seed=MilliSecs()
		SeedRnd2 seed
		WriteLine gl\file,seed
		gl\fileline=""
	EndIf
	
	For th=Each thing
		th_kill(th)
	Next
	ths_setup()
	camera_setup()
End Function


Function finish()
	If gl\replaying=0 Then WriteLine gl\file,gl\fileline
	CloseFile gl\file
	End
End Function


Function l___________GLOBALS()
End Function



Function gl_setup()

	gl\screenw=800
	gl\screenh=600
	Graphics3D gl\screenw,gl\screenh,0,1

	
	gl\camera=CreateCamera()
	CreateLight()
	
	gl\grav=.44
	
	gl\thtrails=4
	
	;gl\replaying=1
	If FileType("replay.txt")=1 Then gl\replaying=Input("choose: 1=replay 0=play : ")
	If gl\replaying<>1 Then gl\replaying=0
	
	SetBuffer BackBuffer()

	If gl\replaying Then
		gl\file=ReadFile("replay.txt")
		SeedRnd2 ReadLine(gl\file)
		gl\fileline=ReadLine(gl\file)
		gl\filelinei=1
	Else
		gl\file=WriteFile("replay.txt")
		seed=MilliSecs()
		SeedRnd2 seed
		WriteLine gl\file,seed
	EndIf
End Function


Function room_setup()
	gl\roomw=200
	gl\roomh=660

	gl\room=CreateCube()
	ScaleEntity gl\room,-gl\roomw,-500,-gl\roomh
	roomtex=CreateTexture(32,32)
	SetBuffer TextureBuffer(roomtex)
	Color 255,0,0
	Oval 0,0,32,32
	SetBuffer BackBuffer()
	ScaleTexture roomtex,.02,.02
	EntityTexture gl\room,roomtex
	PositionEntity gl\room,0,500,0
	
	EntityPickMode gl\room,2
End Function




Function camera_setup()
	RotateEntity gl\camera,90,0,0
End Function

Function camera_update()
	PositionEntity gl\camera,EntityX(gl\you\pivot),130,EntityZ(gl\you\pivot)-30
	PointEntity gl\camera,gl\you\pivot
End Function

Function l___________THINGS()
End Function

Function th_kill(th.thing)
	For ft=1 To gl\thtrails
		FreeEntity th\trailentity[ft]
	Next
	FreeEntity th\entity
	FreeEntity th\pivot
	Delete th
End Function
Function ths_setup()
	Local th.thing
	
	For f=1 To 106
		th=New thing
		th\pivot=CreatePivot()
		th\entity=CreateCube(th\pivot)
		x=Rnd2(-gl\roomw,+gl\roomw)
		z=Rnd2(-(gl\roomh-50),+gl\roomh)
		If f>1 Then
			th\speedmax=2
			th\accel=.01
			th\fric=.0001
			PositionEntity th\pivot,x,0,z
			th\w=2
			th\h=3
			ScaleEntity th\entity,th\w,(th\w+th\h)/2,th\h
			EntityColor th\entity,0,Rnd2(111,255),Rnd2(111,255)
		Else
			PositionEntity th\pivot,0,th\h,-(gl\roomh-10)
			EntityColor th\entity,100,200,0
			th\w=3
			th\h=3
			ScaleEntity th\entity,th\w,3,th\h
			th\speedmax=1
			th\accel=1
			th\fric=1
		EndIf
		
		
		For ft=1 To gl\thtrails
			th\trailentity[ft]=CopyEntity(th\entity)
			EntityAlpha th\trailentity[ft],1-Float(ft)/gl\thtrails
		Next
	Next
	
	gl\you=First thing
End Function


Function ths_update()
	Local th.thing

	For th=Each thing
		For ft=gl\thtrails To 2 Step -1
			PositionEntity th\trailentity[ft],EntityX(th\trailentity[ft-1]),EntityY(th\trailentity[ft-1]),EntityZ(th\trailentity[ft-1])
		Next
		PositionEntity th\trailentity[1],EntityX(th\pivot),EntityY(th\pivot),EntityZ(th\pivot)

		If th<>gl\you Then
			th_ai(th)
		Else
			th_controls(th)
		EndIf
		
		If EntityX(th\pivot)+th\speedx<-gl\roomw Then th\speedx=-th\speedx
		If EntityX(th\pivot)+th\speedx>+gl\roomw Then th\speedx=-th\speedx
		If EntityZ(th\pivot)+th\speedz<-gl\roomh Then th\speedz=-th\speedz
		If EntityZ(th\pivot)+th\speedz>+gl\roomh Then th\speedz=-th\speedz
		If EntityY(th\pivot)<th\h Then
			PositionEntity th\pivot,EntityX(th\pivot),th\h,EntityZ(th\pivot)
			th\speedy=0
		EndIf
		MoveEntity th\pivot,th\speedx,th\speedy,th\speedz
		If EntityY(th\pivot)=th\h Then
			th\speedx=moveto(th\speedx,0,th\fric)
			th\speedz=moveto(th\speedz,0,th\fric)
		EndIf
		If EntityY(th\pivot)>th\h Then th\speedy=th\speedy-gl\grav
		If th<>gl\you Then
			If Abs(EntityY(th\pivot)-EntityY(gl\you\pivot)) < th\h+gl\you\h Then
				If RectsOverlap(EntityX(th\pivot)-th\w,EntityZ(th\pivot)-th\h,th\w*2,th\h*2, EntityX(gl\you\pivot)-gl\you\w,EntityZ(gl\you\pivot)-gl\you\h,gl\you\w*2,gl\you\h*2) Then
					Return 1
				EndIf
			EndIf
		EndIf
		
	Next
	If EntityZ(gl\you\pivot)>+gl\roomh-(gl\you\h+1) Then
		finish()
	EndIf
	
End Function

Function th_ai(th.thing)
	th\speedx=keep(th\speedx+Sgn(EntityX(gl\you\pivot)-EntityX(th\pivot))*th\accel,-th\speedmax,+th\speedmax)
	th\speedz=keep(th\speedz+Sgn(EntityZ(gl\you\pivot)-EntityZ(th\pivot))*th\accel,-th\speedmax,+th\speedmax)
End Function



Function th_controls(th.thing)
	Local l,r,u,d,j
	Local number
	

	If EntityY(gl\you\pivot)=th\h Then
		If gl\replaying Then
			number=Asc(Mid(gl\fileline,gl\filelinei,1))-Asc("a")
			;DebugLog Mid(gl\fileline,gl\filelinei,1)
			gl\filelinei=gl\filelinei+1
			If gl\filelinei>Len(gl\fileline) Then
				If Eof(gl\file) Then WaitKey:End
				gl\filelinei=1
				gl\fileline=ReadLine(gl\file)
			EndIf
			d=number And %00001
			l=number And %00010
			u=number And %00100
			r=number And %01000
			j=number And %10000
		Else
			l=KeyDown(203)
			r=KeyDown(205)
			u=KeyDown(200)
			d=KeyDown(208)
			j=(KeyHit(29) Or KeyHit(56))
			number=d+(l Shl 1)+(u Shl 2)+(r Shl 3)+(j Shl 4)
			gl\fileline=gl\fileline+Chr(Asc("a")+number)
			If Len(gl\fileline)=50 Then
				WriteLine gl\file,gl\fileline
				gl\fileline=""
			EndIf
		EndIf
		If l Then th\speedx=keep(th\speedx-th\accel,-th\speedmax,+th\speedmax)
		If r Then th\speedx=keep(th\speedx+th\accel,-th\speedmax,+th\speedmax)
		If d Then th\speedz=keep(th\speedz-th\accel,-th\speedmax,+th\speedmax)
		If u Then th\speedz=keep(th\speedz+th\accel,-th\speedmax,+th\speedmax)
		If j Then
			If EntityY(th\pivot)=th\h Then
				th\speedy=5
			EndIf
		EndIf
	EndIf

	;CameraPick gl\camera,mouse\x,mouse\y
	;angleto(EntityX(th\pivot),EntityZ(th\pivot),PickedX(),PickedZ())
	
	If KeyHit(1) Then
		finish()
	EndIf
End Function

Function l___________NUMBER()
End Function

Function Keep#(num#,min#,max#)
	If num<min Then 
		Return min
	ElseIf num>max
		Return max
	Else
		Return num
	EndIf
End Function


Function MoveTo#(num#,dest#=0,speed#=.01,away=0)
	If away=0 Then
		If Abs(dest-num)<speed Then Return dest
		If num<dest Then
			Return num+speed
		Else
			Return num-speed
		EndIf
	Else
		If num<dest Then
			Return num-speed
		Else
			Return num+speed
		EndIf
	EndIf
End Function

Function SeedRnd2(seed)
	rndseed2=seed
End Function

Function Rnd2#(min#,max#)
	rndseed2=(rndseed2+3) Mod 500
	Return ((rndseed2*Pi*Cos(rndseed2*(rndseed2 And 5)) + (100-rndseed2)^2 + Sqr(rndseed2 Or 12) + (rndseed2 Mod 15)/15.0 ) Mod (max-min)) + min
End Function




put this in replay.txt to see my best run
replay.txt will be in the blitz\tmp folder if you havent saved this game as .bb
363798289
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeemmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmeeeeeeeeeeeemmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmeegggggemmmmmmmmmmmmmmm
mmmegggggeeeeeemmmmmmmmmmmmmmmmmmmmeeeeggggggggeee
eeeeaaaaaaabddccccggeeeeeeeeemmmmmmmeeeegggggggggg
ggggggggggggggggggeeeeeeeeeeeeeeeeeggggggggggggggg
gggggggggggggggggggggggggggggggggggggggggggggggggg
gggommmmmmmeemmmmmmmeeeeeggggggggggggggggeeeeeeeaa
aaaaccccccggeeemmmmmmmmeegggggggggggggeeeeeeeeeeee
eeeeeeeegggggggeeeeeemmmmmmmmmmmmmmmmmmemmmmmmmmmm
mmmiiiiiimmmmmeeeeeeeeeeeemmmmmmmmmmmmmmmmeegggggg
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eemmmmmmmmmmmmmmmmmmmmmmmeeeeegggggggggggggggggeee
eemmmmmmmmmmmmmiiiiiiijjjjjjjjjjjbbdccccccccgggggg
goiiiiiiiijjjjjjjjjjjjjjjjjjjjjjbbbbdddddddddccccc
cdbjjjjjjddddcccccccggggggggggggggggggmmmmmiiiiiia
aiiiiiiiijjjdcccddddddddljjjjlldddddddddddddcccccg
ggggggggggommmmmmmmmmgggcccccccccccccccccgggggggom
mmmmmmmmegggggggggccaiiaaaaaiimmmmmmmmeeeeeeeegggg
ggggggeeeeeeeeeeeeeeeeeeemmmmmmmeeeeeeemmmmmmmmmmm
mmmmmmmmmmeeeeeeeeeeeeeeeeemmmmmmmmmmmeeeeeeeeeeeg
gggeeeeeeeegggggggggggggggggggggggggommmmmmmmmmmmm
mmiiiiiimmmmmmmmmeeggggggggeeeeeeeeeeeeeeeeeeeeeeg
ggggggggggggggggggggggggggggggggggggggggggggeemmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmeeeeeeeeggggggg
eeeeeeemmmmmmmmmmmeeeeeeeeggggggggggeemmmmmiiiiijj
jjjjjjjjjjjjjjjjjjjjjjjbbjjjjjjjjjjjjjjjjjjjjjjjjj
jjjjjbbbddcccccccccgggmmmmmmmmmmmmmmiiiijjjddbbbbb
bbbbbbbbddddddddddddccccccccccccgggggggggggggggggg
ggggggggggggggggggggggggggggeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeggggggggggggggggggggggggggggggg
cccccciiiiiiiimmmmmmmmmmmmmmmeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeueeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeee


so how far can you get?