November 27, 2020, 05:52:24 AM

Author Topic: Smart Snake  (Read 398 times)

bplus

• Full Member
• Posts: 147
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 arrowsconst B = 25 'Board and array dimension BxB array and boardconst 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) / 2randomize'======== key instructions: definekey 0xFF04, aleft   'press left arrowdefinekey 0xFF05, aright  'press right arrowdefinekey 0xFF09, up      'press up arrowdefinekey 0xFF0A, down    'press down arrowdefineKey 32, startgame   'spacebar restarts gamedefinekey 27, esc         'press esc to quit'definekey subs for arrows, escsub aleft  kd = 3end subsub aright  kd = 4endsub up  kd = 1end sub down  kd = 2endsub esc  quit = 1end 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   showpageendsub 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   endifendsub 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   endifend 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  updateend'=================================================================maincolor rgb(200, 200, 220), 0at (xmax - tw) / 2, 0 : ? titles1 = "Game over: crash into wall or snake, (reverse) direction,  or esc"at (xmax - txtw(s1)) / 2, th : ? s1 startgamerepeat   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 300until quits1 = "Your snake ate " + f + " morsels. Goodbye!"at (xmax - txtw(s1) ) / 2 ,ymax - 2 * th - 5 : ? s1showpagedelay 2000pause 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.