October 28, 2020, 05:19:08 AM

Author Topic: Smart Snake  (Read 363 times)

Offline bplus

  • Full Member
  • ***
  • Posts: 146
Smart Snake
« on: April 08, 2020, 12:07:18 AM »
Here is Classic Snake Game (1 of 2177 files in SB folder with 237 sub folders (and 1 is Snake Games)):

Code: [Select]
'snake game v5.bas SmallBASIC 0.12.6 [B+=MGA] update
''2015-06-08 started from BASIC256 game
'2016-06-27 update with definekey use arrows

const B = 25 'Board and array dimension BxB array and board
const title = "SNAKE GAME move: up, down, left, right  (re)start: spacebar"
const th = txth(title)
const tw = txtw(title)
const tly = 2 * th
const sq = int( (ymax - 5 * th) / B ) 'decide square size according ymax
const tlx = (xmax - B * sq) / 2

randomize
'======== key instructions:
definekey 0xFF04, aleft   'press left arrow
definekey 0xFF05, aright  'press right arrow
definekey 0xFF09, up      'press up arrow
definekey 0xFF0A, down    'press down arrow
defineKey 32, startgame   'spacebar restarts game
definekey 27, esc         'press esc to quit

'definekey subs for arrows, esc
sub aleft
  kd = 3
end sub
sub aright
  kd = 4
end
sub up
  kd = 1
end
sub down
  kd = 2
end
sub esc
  quit = 1
end

sub Update()
   local x,y,c
   rect tlx-1, tly-1, tlx + B * sq, tly + B * sq, 0 filled
   for x=1 TO B
      for y=1 TO B
         if board(x, y) > 0 and board(x, y) < 9 then
            c = 10
         elif board(x, y) = 10
            c = 13
         else
            c = board(x, y)
         endif
         rect tlx + sq * (x - 1), tly + sq * (y - 1) step sq, sq, c filled
      next
   next
   stripesnake
   showpage
end

sub uv(direction)
   if direction = 1 then
      u = 0 : v = -1
   elseif direction = 2
      u = 0 : v = 1
   elseif direction = 3
      u = -1 : v = 0
   elseif direction = 4
      u = 1 : v = 0
   endif
end

sub stripesnake()
   local rr, ss, cc, ccc, j, dir
   if f then
      rr = r : ss = s
      for j = 1 to f
         cc = j mod 4
         select case cc
         case 0 : ccc = 9
         case 1 : ccc = 10
         case 2 : ccc = 12
         case 3 : ccc = 0
         end select
         rect tlx + sq * (rr - 1), tly + sq * (ss - 1) step sq, sq, ccc filled
         dir = board(rr, ss)
         uv dir
         rr =rr + u : ss = ss + v
      next
   endif
end
 
sub startgame()
  local i, j
  dim board(1 to B,1 to B)
  for i = 1 to B
    for j = 1 to B
      board(i, j) = rgb(0, rnd * 30 + 60, 0)
    next
  next
  p = 12 : q = 6 : board(p, q) = 2
  r = 12 : s = 5 : board(r, s) = 2
  board(20, 20) = 10
  u = 0 : v = 1 : quit = 0 : f = 0 : kd = 0 : d = 2
  update
end

'=================================================================main
color rgb(200, 200, 220), 0
at (xmax - tw) / 2, 0 : ? title
s1 = "Game over: crash into wall or snake, (reverse) direction,  or esc"
at (xmax - txtw(s1)) / 2, th : ? s1
startgame
repeat
   d = board(p, q)
   if kd then
      uv kd
      d = kd
   else
      uv d
   endif
   board(p, q) = d 'do i need this line? YES!!!
   p = p + u : q = q + v
   'are we still in play?
   if p > B or p < 1 or q > B or q < 1 then exit 'game over we hit a wall
   if board(p, q) < 9 and board(p, q) > 0 then exit 'we hit ourself
   'yes still good
   if board(p, q) > 9 'we hit our morsel of food and grow one sgment, set up another morsel
      f += 1
      repeat
         a = int(rnd * B + 1) : rb = int(rnd * B + 1)
      until board(a, rb) > 10 or board(a, rb) < 0
      board(a, rb) = 10 : board(p, q) = d
   else 'board = 0 loose the last segment of tail and find next r,s segment
      board(p, q) = d
      y = board(r, s)
      uv y
      board(r, s) = rgb(0, rnd * 30 + 60, 0)
      r = r + u : s = s + v
   end if
   update
   delay 300
until quit
s1 = "Your snake ate " + f + " morsels. Goodbye!"
at (xmax - txtw(s1) ) / 2 ,ymax - 2 * th - 5 : ? s1
showpage
delay 2000
pause 2


Challenge, create Smart Snake AI st snake fills whole garden.

Actually I have better code in QB64 for plugging in Snake Brains that I will translate if anyone up for challenge. But you have to know the Snake Game first and what happens when snake gets long.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal