Ok. There is no ‘sophisticated’ code. It’s just my code, and remember there are more and different ways to reach your goals. This is just a small part of the code. When the snake dies I take a picture of the current canvas and store that with the user’s score in a SQLite db, so you can look back on previous games.
Of course I use a Canvas for the snake’s grid. I initialize it with:
[code]g.ForeColor = Prefs.GridColor
Dim StepSize As Integer = Prefs.BlockSize + Prefs.DividerSize
'precalculated width of the playing field
For I As Integer = 1 To MainWindow.Width Step StepSize
For J As Integer = 1 To MainWindow.Height Step StepSize
g.FillRect(I,J,Prefs.BlockSize,Prefs.BlockSize)
Next
Next [/code]
Prefs is a class in which I store all the user’s presets.
Next I calculate if the grid nicely fits into the Window
[code]'Adjust Window size (and Canvas)
If MainWindow.Width <> Prefs.BlockCount * StepSize Then
MainWindow.Width = (Prefs.BlockCount * Stepsize) + 1
MainWindow.Height = (Prefs.BlockCount * StepSize) + 1
End If
'define upper boundaries for trapping bouncing the walls
XMax = (MainWindow.Width / StepSize)+1
'MsgBox(Str(XMax))
'same for Ymax (square)
YMax = XMax[/code]
Next I initialise a Timer. The interval is set with the users speed setting
Then I draw the snake. It is a class with several methods and properties. The snake’s elements are drawn with a color blend:
for K as Integer = 0 to Ubound(TheSnake.Elements,1)-1
g.ForeColor= CalcGradient(0,k,Ubound(TheSnake.Elements),TheSnake.Headcolor,TheSnake.Tailcolor)
g.FillRect((TheSnake.Elements(k,0)*StepSize)+1,(TheSnake.Elements(k,1)*StepSize)+1,Prefs.BlockSize,Prefs.BlockSize)
next
The ‘CalcGradient’ method is a mod of the Colorblend function you can find (google Xojo colorblend).
In the timer event I trap the cursor keys (I could have done this with Select Case). As the snake can’t reverse you have to force the ‘wrong’ key into the right direction:
[code]If Keyboard.AsyncKeyDown(123) Then
If TheSnake.OldDirection <> “R” Then
TheSnake.Direction = “L”
End If
End If
If Keyboard.AsyncKeyDown(124) Then
If TheSnake.OldDirection <> “L” Then
TheSnake.Direction = “R”
End If
End If
If Keyboard.AsyncKeyDown(125) Then
If TheSnake.OldDirection <> “D” Then
TheSnake.Direction = “U”
End If
End If
If Keyboard.AsyncKeyDown(126) Then
If TheSnake.OldDirection <> “U” Then
TheSnake.Direction = “D”
End If
End If
TheSnake.OldDirection = TheSnake.Direction
[/code]
To place the cookie you have to make sure that it isn’t placed on a current snake (element):
[code]Do
onSnake = False
'randomly generate new food position
foodXPos = App.Randomizer.InRange(0, BlockCount - 1)
foodYPos = App.Randomizer.InRange(0, BlockCount - 1)
'make sure the position of the food does not lie on the snake,
'we loop through every segment of the snake and compare it’s position with the position of the food
For i = 0 To Ubound(theSnake.Elements)
If (foodXPos = TheSnake.Elements(i,0)) And (foodYPos = TheSnake.Elements(i,1)) Then
'ok, so we hit the snake! this food position is not suitable
onSnake = True
Exit For
End If
Next
Loop Until (onSnake = False)[/code]
Good luck and happy coding!