Microsoft Small Basic

Program Listing: XKT407
MaxX=40 ' width of the gameboard
MaxY=30 'heigth of the gameboard

InitialSpeed=5 'initial speed. how many steps per second
sppedinc=50 'how often speed increases
AllowManyFood="False" 'do worm needs to eat before new food arrives?
FoodProbability=100 '0=never 1000=every step

MinFood=1 'minimum food value
MaxFood=10 'maximum food value




GraphicsWindow.Show()
GraphicsWindow.Title="Worm by GT"
GraphicsWindow.Height=MaxY*10
GraphicsWindow.Width=MaxX*10
For i=1 To MaxX
For j=1 To MaxY
Arr[i][j]=0
EndFor
EndFor

GraphicsWindow.BrushColor="red"
For i=1 to MaxX
Arr[i][1]=1
Arr[i][MaxY]=1
GraphicsWindow.FillRectangle(i*10-10,0,10,10)
GraphicsWindow.FillRectangle(i*10-10,MaxY*10-10,10,10)
endfor

For i=1 to MaxY
Arr[1][i]=1
Arr[MaxX][i]=1
GraphicsWindow.FillRectangle(0,i*10-10,10,10)
GraphicsWindow.FillRectangle(MaxX*10-10,i*10-10,10,10)
endfor

PosX=Math.Round(MaxX/2)
PosY=Math.Round(MaxY/2)

Interval=1000/InitialSpeed
Points=0

DeltaX=0
DeltaY=0

TailEnd=1
TailMax=TailEnd
TailX[TailEnd]=PosX
TailY[TailEnd]=PosY
TailWait=10

CanDoIt="true" ' do not modify!!!!
FoodDone="True" 'do not modify!!!!


GraphicsWindow.ShowMessage("Press any arrow to start...","Worm")
Timer.Interval=interval
Timer.Tick=doit
GraphicsWindow.KeyDown=key



Sub doit
If (DeltaX<>0) Or (DeltaY<>0) then
If CanDoIt then
CanDoIt="false"
If FoodDone Or AllowManyFood then
If Math.GetRandomNumber(1000) FoodX=Math.GetRandomNumber(MaxX-2)+1
FoodY=Math.GetRandomNumber(MaxY-2)+1
If Arr[FoodX][FoodY]=0 then
Arr[FoodX][FoodY]=-1*(MinFood+Math.GetRandomNumber(MaxFood-MinFood))
GraphicsWindow.BrushColor="green"
GraphicsWindow.FillRectangle(FoodX*10-10,FoodY*10-10,10,10)
FoodDone="False"
EndIf
EndIf
EndIf 'food generation

'lets move!
PosX=PosX+DeltaX
PosY=PosY+DeltaY

If Arr[PosX][PosY]=1 then 'food at position already ocupied by worm or wall
Timer.Pause()
GraphicsWindow.ShowMessage("THE END!",":)")
Program.End()
EndIf


'is there any food at my head position?
If Arr[PosX][PosY]<0 then
TailWait=TailWait-Arr[PosX][PosY] 'lets stop tail for some moves
FoodDone="True"
EndIf

'drawing head
GraphicsWindow.BrushColor="red"
GraphicsWindow.FillRectangle(PosX*10-10,PosY*10-10,10,10)
Arr[PosX][PosY]=1

'drawing tail
TailMax=TailMax+1
TailX[TailMax]=PosX
TailY[TailMax]=PosY
If TailWait<=0 then
TailEnd=TailEnd+1
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(TailX[TailEnd]*10-10,TailY[TailEnd]*10-10,10,10)
Arr[TailX[TailEnd]][TailY[TailEnd]]=0

EndIf

'temporary stoping tail = worm groves bigger
If TailWait>0 then
TailWait=TailWait-1
Points=Points+1

'displaying score
GraphicsWindow.BrushColor="red"
GraphicsWindow.FillRectangle(10,0,100,10)
GraphicsWindow.BrushColor="black"
GraphicsWindow.FontSize=8

GraphicsWindow.DrawText(10,0,Points)


EndIf

'increasing speed
If TailMax/sppedinc=Math.Round(TailMax/sppedinc) then
Interval=Math.round(Interval*0.9)
Timer.Interval=Interval
EndIf

CanDoIt="true"
EndIf
EndIf
EndSub


Sub key
lk=GraphicsWindow.LastKey
If lk="Escape" Then
Program.End()
EndIf
If lk="Left" then
DeltaX=-1
DeltaY=0
EndIf
If lk="Right" then
DeltaX=1
DeltaY=0
EndIf
If lk="Up" then
DeltaX=0
DeltaY=-1
EndIf
If lk="Down" then
DeltaX=0
DeltaY=1
EndIf

EndSub