Snake Game Starter Code
I recently updated my Snake game code to allow practice with really long snakes. This is allowing the snake to take only one step in direction of arrow (unless you hold button down) thus hard to accidently cash into wall or it's own body.
A 4x4 cell screen is set up to demo a quick fill the screen game. The game will freeze when the sanke's body takes all but the last square with apple because there is no place to put the next apple. Black circle is snake head, red circle is the apple/food that makes the snake grow.
Code: (Select All)
_Title "Walk the Snake Yourself, b+ mod 2024-06-30"
' mod 2024-06-30 I don't like the snake moving without being told
' this will fix that and now if you crash into wall you die you dummy! LOL
' oh also fix the apple so it's never on snake body
Option _Explicit
Const sps = 4 ' cells per row or column !!!! < try 4 cells for quick garden fill!!!
Const sq = 20 ' pixels per cell
Const xmax = sps * sq, ymax = xmax
Screen _NewImage(400, 400, 12)
Dim px, py, ax, ay, tail, i, keypress$, crash, moved
Dim trailX(1000), trailY(1000)
px = 0 ' snake head
py = 0
ax = 2 ' apple food
ay = 2
tail = 1 ' how long snake is
trailX(0) = px
trailY(0) = py
Do ' update display first, get key and then update game
Line (0, 0)-(xmax, ymax), 6, BF ' clear garden 6
For i = 0 To tail - 1 'show snake
If i = 0 Then
Circle (trailX(i) * sq + .5 * sq, trailY(i) * sq + .5 * sq), .5 * sq, 0
Paint (trailX(i) * sq + .5 * sq, trailY(i) * sq + .5 * sq), 0, 0
Else
Line (trailX(i) * sq + 2, trailY(i) * sq + 2)-Step(sq - 4, sq - 4), (i Mod 4) + 7, BF
End If
Next
Circle (ax * sq + .5 * sq, ay * sq + .5 * sq), .5 * sq, 4 ' show apple
Paint (ax * sq + .5 * sq, ay * sq + .5 * sq), 4, 4
_Display
_Limit 30
moved = 0 ' this walks snake step by step with arrow keys no stupid timing required
keypress$ = InKey$
If keypress$ = Chr$(0) + Chr$(72) Then 'up
py = py - 1: moved = 1
ElseIf keypress$ = Chr$(0) + Chr$(80) Then 'down
py = py + 1: moved = 1
ElseIf keypress$ = Chr$(0) + Chr$(77) Then 'right
px = px + 1: moved = 1
ElseIf keypress$ = Chr$(0) + Chr$(75) Then 'left
px = px - 1: moved = 1
ElseIf keypress$ = "q" Or keypress$ = Chr$(27) Then
End '>>>>>>>> here is quit or escape clause!!!!
End If
_KeyClear
If moved Then
' check for wall crashes
If px < 0 Then crash = 1
If px > sps - 1 Then crash = 1
If py < 0 Then crash = 1
If py > sps - 1 Then crash = 1
For i = 0 To tail - 1 ' check for snake crashes into self
If px = trailX(i) And py = trailY(i) Then crash = 2: Exit For
Next
If crash Then
If crash = 1 Then
_PrintString (84, 192), "Crashed into wall - GAME OVER!"
Else
_PrintString (54, 192), "Snake crashed into self - GAME OVER!"
End If
_Display
End
End If
If ax = px And ay = py Then ' snake eats apple and grows
tail = tail + 1
GoSub update_snake
newapple:
ax = Int(Rnd * sps) ' set new place for apple
ay = Int(Rnd * sps)
For i = 0 To tail - 1 ' check update apple
If ax = trailX(i) And ay = trailY(i) Then GoTo newapple
Next
Else
GoSub update_snake
End If
End If
Loop
update_snake:
For i = tail - 1 To 1 Step -1
trailX(i) = trailX(i - 1): trailY(i) = trailY(i - 1)
Next
trailX(0) = px
trailY(0) = py
Return
b = b + ...