Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
b+ Beginners Corner
#60
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 + ...
Reply


Messages In This Thread
b+ Beginners Corner - by bplus - 05-20-2023, 06:34 PM
RE: b+ Beginners Corner - by vince - 05-20-2023, 06:47 PM
RE: b+ Beginners Corner - by bplus - 05-20-2023, 07:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 12:12 AM
RE: b+ Beginners Corner - by bplus - 05-26-2023, 04:11 PM
RE: b+ Beginners Corner - by PhilOfPerth - 05-26-2023, 11:18 PM
RE: b+ Beginners Corner - by mnrvovrfc - 05-27-2023, 12:15 AM
RE: b+ Beginners Corner - by PhilOfPerth - 05-27-2023, 02:27 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 12:07 AM
RE: b+ Beginners Corner - by bplus - 05-29-2023, 01:37 AM
RE: b+ Beginners Corner - by mnrvovrfc - 05-29-2023, 02:29 AM
RE: b+ Beginners Corner - by bplus - 05-30-2023, 04:17 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 03:06 PM
RE: b+ Beginners Corner - by GareBear - 06-15-2023, 07:50 PM
RE: b+ Beginners Corner - by bplus - 06-15-2023, 10:42 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 02:46 PM
RE: b+ Beginners Corner - by CharlieJV - 06-23-2023, 03:26 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 08:28 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-23-2023, 09:45 PM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 09:56 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 02:47 AM
RE: b+ Beginners Corner - by bplus - 06-23-2023, 10:02 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 02:35 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 02:52 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 07:48 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 08:02 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 08:40 PM
RE: b+ Beginners Corner - by TerryRitchie - 06-24-2023, 10:07 PM
RE: b+ Beginners Corner - by bplus - 06-24-2023, 09:08 PM
RE: b+ Beginners Corner - by Dimster - 06-24-2023, 09:12 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-24-2023, 11:44 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 02:27 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 05:49 PM
RE: b+ Beginners Corner - by bplus - 06-25-2023, 06:40 PM
RE: b+ Beginners Corner - by OldMoses - 06-25-2023, 08:03 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 01:14 AM
RE: b+ Beginners Corner - by mnrvovrfc - 06-26-2023, 02:26 AM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 11:29 AM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 12:17 PM
RE: b+ Beginners Corner - by Ultraman - 06-26-2023, 12:21 PM
RE: b+ Beginners Corner - by Dimster - 06-26-2023, 02:38 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 03:32 PM
RE: b+ Beginners Corner - by bplus - 06-26-2023, 04:48 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 01:29 AM
RE: b+ Beginners Corner - by OldMoses - 06-27-2023, 11:49 AM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 12:40 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 02:12 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 03:22 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-27-2023, 05:21 PM
RE: b+ Beginners Corner - by bplus - 06-27-2023, 05:48 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 03:20 AM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 02:54 PM
RE: b+ Beginners Corner - by mnrvovrfc - 06-28-2023, 07:07 PM
RE: b+ Beginners Corner - by Dimster - 06-28-2023, 09:50 PM
RE: b+ Beginners Corner - by bplus - 06-28-2023, 10:27 PM
RE: b+ Beginners Corner - by bplus - 06-04-2024, 01:17 AM
RE: b+ Beginners Corner - by PhilOfPerth - 06-04-2024, 11:37 PM
RE: b+ Beginners Corner - by bplus - 06-05-2024, 12:42 AM
RE: b+ Beginners Corner - by gaslouk - 06-05-2024, 02:37 PM
RE: b+ Beginners Corner - by bplus - 06-30-2024, 07:38 PM
RE: b+ Beginners Corner - by bplus - 07-01-2024, 03:42 PM
RE: b+ Beginners Corner - by aurel - 07-01-2024, 06:16 PM
RE: b+ Beginners Corner - by bplus - 07-01-2024, 07:39 PM
RE: b+ Beginners Corner - by bplus - 07-07-2024, 06:42 PM



Users browsing this thread: 4 Guest(s)