Just felt like writing a quick snake game to see if I could store the positions of the snake in an array without shifting the array every turn. Turns out it was quite easy.
It works in both DBC1.13 and DBProDemo5.5, I recommend DBPro though
The box command seems to work different in both DB versions, so the appearance changes, and DBPro seems to be more responsive in the keyboard.
20 Line Version:
Set Window On : Set Display Mode 512,512,32 : Cls 0 : Sync On
Sync Rate 10 : Sync : Hide Mouse : Set Text Opaque : Ink 16777215,0
Text 192,232,"Simple Snake Game" : Text 209,248,"By BigDan256"
Text 190,264,"Press A Key To Start" : Sync : Wait Key : Cls 0 : Dim M(64,64)
Dim I(4096) : Dim J(4096) : D=32 : E=32 : C=1 : H=10 : Do : If A=0
A=1 : K(16711680,Rnd(62)+1,Rnd(62)+1) : Endif : F=Scancode() : If F=200
B=0 : C=-1 : Endif : If F=208 : B=0 : C=1 : Endif : If F=203 : B=-1
C=0 : Endif : If F=205 : B=1 : C=0 : Endif : Inc D,B : Inc E,C
If D<0 Or D>63 Or E<0 Or E>63 : Exit : Endif : If M(D,E)=65280 Then Exit
If M(D,E)=16711680 : A=0 : Inc S : Inc H,5 : Endif : K(65280,D,E)
I(Q)=D : J(Q)=E : Inc Q : If Q>4095 : Q=0 : Endif : Inc G : If G>H : Dec G
X=I(P) : Y=J(P) : If M(X,Y)=65280 : K(0,X,Y) : Endif : Inc P : If P>4095 : P=0
Endif : Endif : Sync : Loop : Ink 16777215,0 : Text 218,240,"Your Score:"
Center Text 256,256,Str$(S) : Sync : Wait Key : End
Function K(i,x,y)
M(X,Y)=i : Ink i,0 : Box x*8,y*8,x*8+7,y*8+7
Endfunction
Longer Version (With more descriptive variables):
Set Window On
Set Display Mode 512,512,32
Cls 0 : Sync On : Sync Rate 10 : Sync
Autocam Off : Hide Mouse
Set Text Opaque
Ink Rgb(255,255,255),0
Center Text 256,232,"Simple Snake Game"
Center Text 256,248,"By BigDan256"
Center Text 256,264,"Press A Key To Start"
Sync
Wait Key
Cls 0
ItmBlank=0 : ItmWall=1 : ItmSnake=2 : ItmApple=3
MapWidth=64 : MapHeight=64
Dim Grid(MapWidth,MapHeight)
SnakePos1=0 : SnakePos2=0
SnakeLen=0 : SnakeLenMax=10
SnakePosMax=MapWidth*MapHeight
Dim SnakeX(SnakePosMax)
Dim SnakeY(SnakePosMax)
CurrentX=MapWidth/2 : CurrentY=MapHeight/2
CurrentDirX=0 : CurrentDirY=1
Apples=0 : GameOver=0 : Score=0
While GameOver=0
If Apples=0
X=Rnd(MapWidth-2)+1
Y=Rnd(MapHeight-2)+1
Apples=1
Grid(X,Y)=ItmApple
DrawItem(ItmApple,X,Y)
Endif
If Upkey() Then CurrentDirX=0 : CurrentDirY=-1
If Downkey() Then CurrentDirX=0 : CurrentDirY=1
If Leftkey() Then CurrentDirX=-1 : CurrentDirY=0
If Rightkey() Then CurrentDirX=1 : CurrentDirY=0
If (CurrentX<0) Or (CurrentX>=MapWidth) Then GameOver=1
If (CurrentY<0) Or (CurrentY>=MapHeight) Then GameOver=1
If Grid(CurrentX,CurrentY)=ItmSnake Then GameOver=1
If Grid(CurrentX,CurrentY)=ItmApple : Apples=0 : Inc Score : Inc SnakeLenMax,5 : Endif
Grid(CurrentX,CurrentY)=ItmSnake
DrawItem(ItmSnake,CurrentX,CurrentY)
SnakeX(SnakePos2)=CurrentX
SnakeY(SnakePos2)=CurrentY
Inc SnakeLen
Inc SnakePos2
If SnakePos2>=SnakePosMax Then SnakePos2=0
If SnakeLen>SnakeLenMax
Dec SnakeLen
X=SnakeX(SnakePos1)
Y=SnakeY(SnakePos1)
If Grid(X,Y)=ItmSnake
Grid(X,Y)=ItmBlank
DrawItem(ItmBlank,X,Y)
Endif
Inc SnakePos1
If SnakePos1>=SnakePosMax Then SnakePos1=0
Endif
Inc CurrentX,CurrentDirX
Inc CurrentY,CurrentDirY
Sync
Endwhile
Ink Rgb(255,255,255),0
Center Text 256,240,"Your Score:"
Center Text 256,256,Str$(Score)
Sync
Wait Key
End
Function DrawItem(itm,x,y)
If itm=0 Then Ink Rgb(0,127,0),0
If itm=1 Then Ink Rgb(0,0,255),0
If itm=2 Then Ink Rgb(0,255,0),0
If itm=3 Then Ink Rgb(255,0,0),0
Box x*8,y*8,x*8+7,y*8+7
Endfunction