News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

3d snakes

Started by Draco9898, April 11, 2006, 11:28:16 PM

Previous topic - Next topic

Draco9898

Global NumOfSnakes
Global LinksPerSnake
Global TotalLinks

SetFPS 60
ScreenVsync 1
Global ScreenW#=640: Global ScreenH#=480
OpenScreen ScreenW#,ScreenH#,16,2

Dim SpikeBallImgs(6)
TempImage=GetFreeImage()
LoadImage "SpikeBallnew.png",TempImage
RenderToImage TempImage
For X=1 To 6
 SpikeBallImgs(X)=GetFreeImage()
 GetImage SpikeBallImgs(X),0,20,72,92
Next X
RGBMaskImage SpikeBallImgs(1),RGB(255,145,0)
RGBMaskImage SpikeBallImgs(2),RGB(145,255,0)
RGBMaskImage SpikeBallImgs(3),RGB(155,60,230)
RGBMaskImage SpikeBallImgs(4),RGB(55,60,230)
RGBMaskImage SpikeBallImgs(5),RGB(15,255,20)
RGBMaskImage SpikeBallImgs(6),RGB(155,160,230)
For X=1 To 6
 PrepareFXImage SpikeBallImgs(X)
Next X
RenderToScreen
CreateSprite 1
SpriteImage 1,SpikeBallImgs(1)
SpriteDrawMode 1,2
CenterSpriteHandle 1
PositionSprite 1,320,200

Type tSnake
X#,Y#,Z#
OldX#,OldY#,OldZ#
XVelo#,YVelo#,ZVelo#
RandomStep,NextRand
SinMul#,SinMul2#
CosMul#
Sprite
Image
EndType
Dim Snake(600) As tSnake
TotalLinks=GetArrayElements(Snake().tSnake,1)
NumOfSnakes=2
LinksPerSnake=(TotalLinks/NumOfSnakes)
For X=1 To TotalLinks
CreateSprite X
SpriteImage X,SpikeBallImgs(1)
SpriteDrawMode X,2
CenterSpriteHandle X
Next X
For X=1 To NumOfSnakes
If X=1
 Z=1
Else
 Z=1+((X-1)*LinksPerSnake)
EndIf
Snake(Z).Image=X
SpriteImage Z,SpikeBallImgs(Snake(Z).Image)
Snake(Z).X#=RndRange(0,640)
Snake(Z).Y#=RndRange(0,480)
Snake(Z).XVelo#=1
Snake(Z).YVelo#=1
Snake(Z).Z#=Rnd(2000)
Snake(Z).ZVelo#=-2
Next X


`PreStep snake
For X=1 To 2000
UpdateSnake()
Next X

CreateCamera 1
Do
CaptureToScene
ClsScene

 UpdateSnake()
 For X=1 To TotalLinks
  Z#=(Snake(X).Z#+Snake(Z).ZVelo#)
  If Z#>0
   ScaleSprite X,0+(Z#*0.0002)
  Else
   ScaleSprite X,0.01
  EndIf
  PositionSpriteXYZ X,Snake(X).X#,Snake(X).Y#,5000-Snake(X).Z#
  `PositionSprite X,Snake(X).X#,Snake(X).Y#
 Next X
 DrawAllSprites

DrawCamera 1
Sync
Loop

Psub UpdateSnake()
Theta#=Theta#+3
For X=1 To NumOfSnakes
 If X=1
  Z=1
 Else
  Z=1+((X-1)*LinksPerSnake)
 EndIf
 Snake(Z).OldX#=Snake(Z).X#
 Snake(Z).OldY#=Snake(Z).Y#
 Snake(Z).OldZ#=Snake(Z).Z#
 If Snake(Z).RandomStep=Snake(Z).NextRand
  Snake(Z).RandomStep=0
  Snake(Z).NextRand=250+Rnd(200)
  Snake(Z).SinMul#=0+RndRange(-4,3)
  Snake(Z).CosMul#=0+RndRange(-3,4)
  Snake(Z).SinMul2#=2+RndRange(-8,6)
 EndIf
 Snake(Z).RandomStep=Snake(Z).RandomStep+1
 Snake(Z).X#=Snake(Z).X#+(Sin(Theta#)*Snake(Z).SinMul#)
 Snake(Z).Y#=Snake(Z).Y#-(Cos(Theta#)*Snake(Z).CosMul#)
 Snake(Z).Z#=Snake(Z).Z#+(Sin(Theta#)*Snake(Z).SinMul2#)
 Snake(Z).X#=Snake(Z).X#+Snake(Z).XVelo#
 Snake(Z).Y#=Snake(Z).Y#+Snake(Z).YVelo#
 Snake(Z).Z#=Snake(Z).Z#+Snake(Z).ZVelo#
 If Snake(Z).X#<32
  Snake(Z).XVelo#=Snake(Z).XVelo#*-1
  Snake(Z).X#=32
 EndIf
 If Snake(Z).X#>608
  Snake(Z).XVelo#=Snake(Z).XVelo#*-1
  Snake(Z).X#=608
 EndIf
 If Snake(Z).Y#<32
  Snake(Z).YVelo#=Snake(Z).YVelo#*-1
  Snake(Z).Y#=32
 EndIf
 If Snake(Z).Y#>448
  Snake(Z).YVelo#=Snake(Z).YVelo#*-1
  Snake(Z).Y#=448
 EndIf
 If Snake(Z).Z#<1000
  Snake(Z).ZVelo#=Snake(Z).ZVelo#*-1
  Snake(Z).Z#=1000
 EndIf
 If Snake(Z).Z#>2000
  Snake(Z).ZVelo#=Snake(Z).ZVelo#*-1
  Snake(Z).Z#=2000
 EndIf
Next X

StepCount=StepCount+1
If StepCount=6
 StepCount=0
 For X=1 To NumOfSnakes
  FirstLink=2+((X-1)*LinksPerSnake)
  SecondLink=(X*LinksPerSnake)
  For L=FirstLink To SecondLink
   Snake(L).OldX#=Snake(L).X#
   Snake(L).OldY#=Snake(L).Y#
   Snake(L).OldZ#=Snake(L).Z#
   Snake(L).X#=Snake(L-1).OldX#
   Snake(L).Y#=Snake(L-1).OldY#
   Snake(L).Z#=Snake(L-1).OldZ#
   If Snake(L).Image<>Snake(L-1).Image
    Snake(L).Image=Snake(L-1).Image
    SpriteImage L,SpikeBallImgs(Snake(L).Image)
   EndIf
  Next L
 Next X
EndIf
EndPsub
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

Tracy

Cool. I like it. :)