For s = 1 To numSegments
If s = 1 Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "Green"
EndIf
tempSegment = Shapes.AddEllipse(segRadius * 2, segRadius * 2)
Shapes.HideShape(tempSegment)
snake[s][segment] = tempSegment
EndFor
EndSub
Sub ResetSnake
For s = 1 To numSegments
snake[s][x] = startX
snake[s][y] = startY + s
If (s <= visibleSegments) Then
Shapes.ShowShape(snake[s][segment])
Else
Shapes.HideShape(snake[s][segment])
EndIf
EndFor
EndSub
Sub DrawSnake
For iD = 1 To visibleSegments
tempShape = snake[iD][segment]
tempX = snake[iD][x] * gridSize
tempY = (snake[iD][y] * gridSize) + titleRegion
Shapes.Move(tempShape, tempX, tempY)
EndFor
Shapes.ShowShape(snake[visibleSegments][segment])
EndSub
Sub MoveSnake
For iM = numSegments To 2 Step -1
snake[iM][x] = snake[im-1][x]
snake[im][y] = snake[im-1][y]
EndFor
If (snakeDirection = north) Then
snakeVX = 0
snakeVY = -1
ElseIf (snakeDirection = east) Then
snakeVX = 1
snakeVY = 0
ElseIf (snakeDirection = south) Then
snakeVX = 0
snakeVY = 1
ElseIf (snakeDirection = west) Then
snakeVX = -1
snakeVY = 0
EndIf
snake[1][x] = snake[1][x] + snakeVX
snake[1][y] = snake[1][y] + snakeVY
CheckCollisions()
EndSub
Sub CheckCollisions
' Store head attributes
head = snake[1][segment]
headX = snake[1][x]
headY = snake[1][y]
' Check collision against snakes own body
For iC = 2 To visibleSegments
If (headX = snake[iC][x]) And (headY = snake[iC][y]) Then
Shapes.HideShape(snake[iC][segment])
badCollsion = "True"
EndIf
EndFor
' Check that Snake head has not gone over the boundary
If (headX < 0) Or (headX > gridWidth) Then
badCollsion = "True"
ElseIf (headY < 0) Or (headY > gridHeight) Then
badCollsion = "True"
EndIf
' Check Snake head collision with Food
If (headX = foodX) And (headY = foodY) Then
goodCollision = "True"
EndIf
If (goodCollision) Then
goodCollision = "False"
EatFood()
EndIf
If (badCollsion = "True") Then
GameOver()
EndIf
EndSub
Sub GameOver
run = false
badCollsion = "False"
For i = 1 To visibleSegments
Shapes.SetOpacity(snake[i][segment], 40)
EndFor
Shapes.ShowShape(gameOverMessage)
EndSub
' Show Instructions
Shapes.ShowShape(StartMessage)
Shapes.ShowShape(Instructions)
GraphicsWindow.FontSize = 12
EndSub
Sub UpdateGui
Shapes.SetText(scoreShape, score)
Shapes.SetText(lengthShape, visibleSegments)
EndSub
Sub AdjustTimer
' Allow player to eat the first 5 apples then gradually reduce interval time to increase speed
' based on IntervalTime, minIntervalTime, adjustmentTime, totalGameTime
If (score > 25) Then
GameTime = GameTime + intervalTime
If (GameTime > adjustmentTime) And (intervalTime > minIntervalTime) Then
GameTime = 0
intervalTime = intervalTime - 20
If (intervalTime < minIntervalTime) Then
intervalTime = minIntervalTime
EndIf
Timer.Interval = intervalTime
EndIf
EndIf
EndSub
Sub OnTimerTick
GameLoop()
EndSub
Sub OnKeyDown
'TODO: Make the game only recognise one key input per frame
key = GraphicsWindow.LastKey
If (run) Then
If (key = "Up") And (snakeDirection <> south) Then
snakeDirection = north
ElseIf (key = "Right") And (snakeDirection <> west) Then
snakeDirection = east
ElseIf (key = "Down") And (snakeDirection <> north) Then
snakeDirection = south
ElseIf (key = "Left") And (snakeDirection <> east) Then
snakeDirection = west
EndIf
Else
If (key = "Space") Then
run = true
Shapes.HideShape(Instructions)
Shapes.HideShape(StartMessage)
EndIf
EndIf
If (key = "Escape") Then
Program.End()
EndIf
EndSub