Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Smarter than a fb Worm
#1
This snake never goes hungry:
Code: (Select All)
_Title "Snake AI-1.1" 'b+ 2020-03-16
'2020-03-14 Snake AI-1 first post
'2020-03-16  Snake AI-1.1 there must be overlap of the snake somewhere!

Const sq = 20, sqs = 20, xmax = 400, ymax = 400
Screen _NewImage(xmax, ymax, 32)
_Delay .25
_ScreenMove _Middle
Randomize Timer
Dim X(xmax + 100), Y(ymax + 100), overlap(19, 19) As Integer
hx = 10: hy = 10: ax = 15: ay = 15: top = 0: X(top) = hx: Y(top) = hy 'initialize
Do
    _Title Str$(top + 1)
    Line (0, 0)-(xmax, ymax), &HFF006600, BF 'clear garden

    '>>>>>>>>>>>       SNAKE BRAIN    <<<<<<<<<<<<<<<
    If hx = 0 And hy = 19 Then
        hy = hy - 1
    ElseIf hx Mod 2 = 0 And hy <> 0 And hy <> 19 Then
        hy = hy - 1
    ElseIf hx Mod 2 = 0 And hy = 0 And hy <> 19 Then
        hx = hx + 1
    ElseIf hx Mod 2 = 1 And hx <> 19 And hy < 18 Then
        hy = hy + 1
    ElseIf hx Mod 2 = 1 And hx <> 19 And hy = 18 Then
        hx = hx + 1
    ElseIf hx = 19 And hy = 19 Then
        hx = hx - 1
    ElseIf hy = 19 And hx <> 0 Then
        hx = hx - 1
    ElseIf hx Mod 2 = 1 And hy = 0 And hy <> 19 Then
        hy = hy + 1
    ElseIf hx = 19 And hy < 19 Then
        hy = hy + 1
    End If
    For i = 0 To top - 1
        X(i) = X(i + 1): Y(i) = Y(i + 1)
    Next
    X(top) = hx: Y(top) = hy

    'apple
    If (ax = hx And ay = hy) Then 'snake eats apple, get new apple watch it's not where snake is
        top = top + 1
        X(top) = hx: Y(top) = hy
        Do 'check new apple
            ax = Int(Rnd * sqs): ay = Int(Rnd * sqs): good = -1
            For i = 0 To top - 1
                If ax = X(i) And ay = Y(i) Then good = 0: Exit For
            Next
        Loop Until good
    End If
    Line (ax * sq, ay * sq)-Step(sq - 2, sq - 2), _RGB32(255, 100, 255), BF

    'snake
    Erase overlap
    For i = 0 To top
        If i = top Then
            c~& = &HFF000000
        Else
            Select Case (top - i) Mod 4
                Case 0: c~& = &HFF000088
                Case 1: c~& = &HFF880000
                Case 2: c~& = &HFFBB8800
                Case 3: c~& = &HFF008888
            End Select
        End If
        overlap(X(i), Y(i)) = overlap(X(i), Y(i)) + 1
        Line (X(i) * sq, Y(i) * sq)-Step(sq - 2, sq - 2), c~&, BF
        If overlap(X(i), Y(i)) > 1 Then Line (X(i) * sq + .25 * sq, Y(i) * sq + .25 * sq)-Step(.5 * sq - 2, .5 * sq - 2), &HFFFFFFFF, BF
    Next
    _Display
    If top < 10 Then
        _Limit 10 + top
    ElseIf top < 300 Then
        _Limit 100
    Else
        _Limit 10
    End If
Loop

And it's the dumbest snake I have!
b = b + ...
Reply


Messages In This Thread
Smarter than a fb Worm - by bplus - 02-13-2023, 05:44 PM
RE: Smarter than a fb Worm - by bplus - 02-14-2023, 07:53 PM
RE: Smarter than a fb Worm - by TempodiBasic - 02-15-2023, 12:30 AM
RE: Smarter than a fb Worm - by bplus - 02-15-2023, 01:07 AM
RE: Smarter than a fb Worm - by bplus - 02-15-2023, 08:24 PM



Users browsing this thread: 2 Guest(s)