RaZgRiZ

Snake game (WIP 2)

Jan 28th, 2021 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. snake_reset = [
  2.     .sp =   0 // points
  3.     .sf = 600 // speed (ms)
  4.  
  5.     .sw =  30 // board width
  6.     .sh =  20 // board height
  7.     .st = (* $.sw $.sh) // total board tiles
  8.  
  9.     .sdirs = (format "%1 0 UP -1 90 LEFT %2 180 DOWN 1 270 RIGHT" (- 0 $.sw) $.sw) // direction offset, rotation, key
  10.     .scurd = (at $.sdirs (* (rnd 4) 3)) // direction
  11.     .snewd = $.scurd // new input direction
  12.     .sinpt = "" // input list
  13.  
  14.     .state = 0 // game state, 0 = waiting, 1 = running, 2 = ended
  15.     .time1 = 0 // game start (ms)
  16.     .time2 = 0 // game end (ms)
  17.  
  18.     // locate spawn point within a centered board area of X-6 and Y-6, then generate the extra
  19.     // two segments of the snake in the opposite direction from where it is facing at the start
  20.     .snake = (+ (* (rnd (- $.sh 3) 3) $.sw) (rnd (- $.sw 3) 3))
  21.     .snake = (concat $.snake (- $.snake $.scurd) (- $.snake (* $.scurd 2)))
  22.  
  23.     // pick new random location for the apple to avoid overlapping the snake segments, but it
  24.     // will need fixing later to avoid never-ending random search for unoccupied numbers (XXX)
  25.     //.apple = (at $.snake 0)
  26.     //while [>= (listfind= $.snake $.apple) 0] [ .apple = (rnd (* $.sw $.sh)) ]
  27.     snake_apple
  28. ]
  29.  
  30. snake_apple = [
  31.     if $arg1 [] [ .apple = (at $.snake 0) ]
  32.     while [>= (listfind= (concat $arg1 $.snake) $.apple) 0] [ .apple = (rnd $.st) ]
  33. ]
  34.  
  35. snake_loop = [
  36.     if $arg1 [
  37.         snake_reset
  38.         .state = 1
  39.         .time1 = $getmillis
  40.  
  41.         // EXTRA STUFF
  42.  
  43.         sleep $.sf [ snake_loop ]
  44.     ] [
  45.         arg2 = (? $.snewd $.snewd $.scurd) // direction offset, new if key was pressed, otherwise go with current dir
  46.         arg3 = (at $.snake 0)    // current position of snake's head
  47.         arg4 = (+ $arg3 $arg2)   // new position of head
  48.  
  49.         if (&& [< 0 $arg4 $.st ] [ // ensure new head position is within board limits
  50.             if (= (abs $arg2) 1) [ // check if sideways movement
  51.                 = (div $arg3 $.sw) (div $arg4 $.sw) // check if still on the same row
  52.             ] [ result 1 ] // default to 1 otherwise
  53.             // CURRENTLY DOES NOT CHECK FOR SELF COLLISION, TO-DO LATER
  54.         ]) [
  55.             arg5 = (= $arg4 $.apple) // is the new head position on the apple?
  56.  
  57.             if $arg5 [ snake_apple $arg4 ]
  58.             .snake = (concat $arg4 (listsplice $.snake "" (+ (listlen $.snake) -1 $arg5) 1))
  59.             .scurd = $arg2
  60.             sleep $.sf [ snake_loop ]
  61.         ] [
  62.             .state = 2
  63.             .time2 = $getmillis
  64.         ]
  65.     ]
  66. ]
  67.  
  68.  
  69. UImenu "snake" [
  70.     local time
  71.     uivlist $.UI_pad_L [
  72.         uihlist 0 [
  73.             uiclamp.x
  74.             uitext (concat "Points:" $.sp) 0.7
  75.             UIbutton "hold2" [ uitext "Start Game" 0.6 ] 0 0 [ snake_loop 1 ]
  76.             time = (case $.state 0 0 1 (- $getmillis $.time1) () (- $.time2 $.time1))
  77.             uitext (concatword (div $time 1000) ":" (pad0 (mod $time 1000) 3)) 0.7
  78.         ]
  79.         UIbox "box d d d n n" 0 0 [
  80.             uioffset (*f $.UI_pad_2XL (mod $.apple $.sw)) (*f $.UI_pad_2XL (div $.apple $.sw)) [
  81.                 UIfastimg "ui/" "ui_radio" "1" $.UI_pad_2XL
  82.             ]
  83.             loop i (listlen $.snake) [
  84.                 uioffset (*f $.UI_pad_2XL (mod (at $.snake $i) $.sw)) (*f $.UI_pad_2XL (div (at $.snake $i) $.sw)) [
  85.                     uifill $.UI_pad_2XL $.UI_pad_2XL [
  86.                         if $i [
  87.                             UIfastimg "ui/" "ui_bar" "1" (-f $.UI_pad_2XL (*f (divf 0.50 (- (listlen $.snake) 1)) $i $.UI_pad_2XL))
  88.                         ] [
  89.                             UIfastimg "ui/" "ui_bar" "1" $.UI_pad_2XL 0 [
  90.                                 uispace $.UI_pad_MS $.UI_pad_MS [
  91.                                     case (? $.snewd $.snewd $.scurd) (- 0 $.sw) [
  92.                                         uialign  0 -1
  93.                                         UItriangle (? (iskeyheld "UP") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 0
  94.                                     ] -1 [
  95.                                         uialign -1  0
  96.                                         UItriangle (? (iskeyheld "LEFT") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 90
  97.                                     ] $.sw [
  98.                                         uialign  0  1
  99.                                         UItriangle (? (iskeyheld "DOWN") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 180
  100.                                     ] 1 [
  101.                                         uialign  1  0
  102.                                         UItriangle (? (iskeyheld "RIGHT") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 270
  103.                                     ]
  104.                                 ]
  105.                             ]
  106.                         ]
  107.                     ]
  108.                 ]
  109.             ]
  110.             uialign* -1 -1
  111.             uivlist 0 [
  112.                 uiclamp.x
  113.                 loop x $.sh [ uioutline $c_line 0 $.UI_pad_2XL ]
  114.                 uiclamp*x
  115.             ]
  116.             uihlist 0 [
  117.                 uiclamp.y
  118.                 loop x $.sw [ uioutline $c_line $.UI_pad_2XL 0 ]
  119.                 uiclamp*y
  120.             ]
  121.         ]
  122.     ]
  123.     if (= $.state 1) [
  124.         looplist3 offset dir key $.sdirs [
  125.             if (iskeyheld $key) [
  126.                 if (&& [< (listfind= $.sinpt $offset) 0] [!= $offset (* $.scurd -1)]) [
  127.                     .sinpt = (listsplice $.sinpt $offset 0 0)
  128.                     .snewd = $offset
  129.                 ]
  130.             ] [
  131.                 if (>= (listfind= $.sinpt $offset) 0) [
  132.                     .sinpt = (listdel $.sinpt $offset)
  133.                     .snewd = (at $.sinpt 0)
  134.                 ]
  135.             ]
  136.         ]
  137.     ]
  138. ] [ snake_reset ]
Add Comment
Please, Sign In to add comment