Code archives/Games and Demos/Spider!

This code has been declared by its author to be Public Domain code.

Download source code

Spider! by Blip
(Posted 8 hours ago)
A spider roams and creates webs
Graphics3D 800,600,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

camera=CreateCamera()
PositionEntity camera,0,10,-9
RotateEntity camera,50,0,0
CameraClsColor camera,10,10,20

light=CreateLight()
RotateEntity light,60,30,0

; A dusty cellar floor, with its top at y=0
floor=CreateCube()
ScaleMesh floor,7,0.1,7
PositionEntity floor,0,-0.1,0
EntityColor floor,60,50,45

; The middle of the cellar, so the spider can turn back towards it
middle=CreatePivot()

; The spider: a pivot we move, carrying a body, a head and red eyes
spider=CreatePivot()
body=CreateSphere(12,spider)
ScaleEntity body,0.35,0.25,0.45
PositionEntity body,0,0.35,-0.25
EntityColor body,25,20,20
head=CreateSphere(12,spider)
ScaleEntity head,0.2,0.17,0.2
PositionEntity head,0,0.33,0.3
EntityColor head,35,20,20
For e=-1 To 1 Step 2
	eye=CreateSphere(6,spider)
	ScaleEntity eye,0.04,0.04,0.04
	PositionEntity eye,e*0.07,0.4,0.48
	EntityColor eye,255,0,0
	EntityFX eye,1
Next

; Eight legs: each is a hip pivot with a long thin cube sticking out of it
Dim hip(7)
Dim hip_yaw#(7)
For i=0 To 7
	side=1
	If i>3 Then side=-1
	hip(i)=CreatePivot(spider)
	PositionEntity hip(i),side*0.12,0.35,0.2-(i Mod 4)*0.15
	hip_yaw(i)=-side*(35+(i Mod 4)*35)
	leg=CreateCube(hip(i))
	ScaleMesh leg,0.03,0.03,0.4
	PositionMesh leg,0,0,0.4
	EntityColor leg,25,20,20
Next

; Up to four webs; the oldest goes when a fifth is spun
Dim web(3)
next_web=0
web_wait=200
pause=0
turn#=0
phase#=0
spinning=False
web_step=0

While Not KeyDown(1)

	moving=False

	If spinning Then
		; Add one thread every other frame, turning round on the spot
		web_step=web_step+1
		If web_step Mod 2=0 Then
			n=web_step/2
			If n<8 Then
				a#=n*45
				AddLine web(next_web),0,0,0,1.4*Cos(a),0,1.4*Sin(a)
			Else
				j=n-8
				r#=0.25+j*0.04
				AddLine web(next_web),r*Cos(j*45),0,r*Sin(j*45),(r+0.04)*Cos(j*45+45),0,(r+0.04)*Sin(j*45+45)
			EndIf
			If n=36 Then
				spinning=False
				next_web=(next_web+1) Mod 4
				web_wait=Rand(300,600)
			EndIf
		EndIf
		TurnEntity spider,0,4,0
		moving=True
	ElseIf pause>0 Then
		; Frozen... watching you
		pause=pause-1
	Else
		; Creep forwards, drifting left and right
		If Rand(60)=1 Then turn=Rnd(-2,2)
		If EntityDistance(spider,middle)>5 Then
			turn=DeltaYaw(spider,middle)
			If turn>3 Then turn=3
			If turn<-3 Then turn=-3
		EndIf
		TurnEntity spider,0,turn,0
		MoveEntity spider,0,0,0.025
		moving=True
		If Rand(250)=1 Then pause=Rand(30,120)

		; Time to spin a web where it stands?
		web_wait=web_wait-1
		If web_wait<=0 Then
			If web(next_web)<>0 Then FreeEntity web(next_web)
			web(next_web)=CreateLineList()
			PositionEntity web(next_web),EntityX(spider),0.02,EntityZ(spider)
			EntityColor web(next_web),220,220,235
			spinning=True
			web_step=0
		EndIf
	EndIf

	; Legs step in two alternating sets of four; they freeze mid-step when it stops
	If moving Then phase=phase+12
	For i=0 To 7
		group=(i+i/4) Mod 2
		swing#=Sin(phase+group*180)*15
		lift#=Sin(phase+group*180+90)*15
		If lift<0 Then lift=0
		RotateEntity hip(i),25-lift,hip_yaw(i)+swing,0
	Next

	RenderWorld
	Text 0,0,"Esc: exit"
	If spinning Then Text 0,20,"Spinning a web..."
	Flip

Wend

End

Comments

Blip
(Posted 8 hours ago)
Scary!

Sign in to comment.

Code Archives Forum