Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- snake_reset = [
- .sp = 0 // points
- .sf = 600 // speed (ms)
- .sw = 30 // board width
- .sh = 20 // board height
- .st = (* $.sw $.sh) // total board tiles
- .sdirs = (format "%1 0 UP -1 90 LEFT %2 180 DOWN 1 270 RIGHT" (- 0 $.sw) $.sw) // direction offset, rotation, key
- .scurd = (at $.sdirs (* (rnd 4) 3)) // direction
- .snewd = $.scurd // new input direction
- .sinpt = "" // input list
- .state = 0 // game state, 0 = waiting, 1 = running, 2 = ended
- .time1 = 0 // game start (ms)
- .time2 = 0 // game end (ms)
- // locate spawn point within a centered board area of X-6 and Y-6, then generate the extra
- // two segments of the snake in the opposite direction from where it is facing at the start
- .snake = (+ (* (rnd (- $.sh 3) 3) $.sw) (rnd (- $.sw 3) 3))
- .snake = (concat $.snake (- $.snake $.scurd) (- $.snake (* $.scurd 2)))
- // pick new random location for the apple to avoid overlapping the snake segments, but it
- // will need fixing later to avoid never-ending random search for unoccupied numbers (XXX)
- //.apple = (at $.snake 0)
- //while [>= (listfind= $.snake $.apple) 0] [ .apple = (rnd (* $.sw $.sh)) ]
- snake_apple
- ]
- snake_apple = [
- if $arg1 [] [ .apple = (at $.snake 0) ]
- while [>= (listfind= (concat $arg1 $.snake) $.apple) 0] [ .apple = (rnd $.st) ]
- ]
- snake_loop = [
- if $arg1 [
- snake_reset
- .state = 1
- .time1 = $getmillis
- // EXTRA STUFF
- sleep $.sf [ snake_loop ]
- ] [
- arg2 = (? $.snewd $.snewd $.scurd) // direction offset, new if key was pressed, otherwise go with current dir
- arg3 = (at $.snake 0) // current position of snake's head
- arg4 = (+ $arg3 $arg2) // new position of head
- if (&& [< 0 $arg4 $.st ] [ // ensure new head position is within board limits
- if (= (abs $arg2) 1) [ // check if sideways movement
- = (div $arg3 $.sw) (div $arg4 $.sw) // check if still on the same row
- ] [ result 1 ] // default to 1 otherwise
- // CURRENTLY DOES NOT CHECK FOR SELF COLLISION, TO-DO LATER
- ]) [
- arg5 = (= $arg4 $.apple) // is the new head position on the apple?
- if $arg5 [ snake_apple $arg4 ]
- .snake = (concat $arg4 (listsplice $.snake "" (+ (listlen $.snake) -1 $arg5) 1))
- .scurd = $arg2
- sleep $.sf [ snake_loop ]
- ] [
- .state = 2
- .time2 = $getmillis
- ]
- ]
- ]
- UImenu "snake" [
- local time
- uivlist $.UI_pad_L [
- uihlist 0 [
- uiclamp.x
- uitext (concat "Points:" $.sp) 0.7
- UIbutton "hold2" [ uitext "Start Game" 0.6 ] 0 0 [ snake_loop 1 ]
- time = (case $.state 0 0 1 (- $getmillis $.time1) () (- $.time2 $.time1))
- uitext (concatword (div $time 1000) ":" (pad0 (mod $time 1000) 3)) 0.7
- ]
- UIbox "box d d d n n" 0 0 [
- uioffset (*f $.UI_pad_2XL (mod $.apple $.sw)) (*f $.UI_pad_2XL (div $.apple $.sw)) [
- UIfastimg "ui/" "ui_radio" "1" $.UI_pad_2XL
- ]
- loop i (listlen $.snake) [
- uioffset (*f $.UI_pad_2XL (mod (at $.snake $i) $.sw)) (*f $.UI_pad_2XL (div (at $.snake $i) $.sw)) [
- uifill $.UI_pad_2XL $.UI_pad_2XL [
- if $i [
- UIfastimg "ui/" "ui_bar" "1" (-f $.UI_pad_2XL (*f (divf 0.50 (- (listlen $.snake) 1)) $i $.UI_pad_2XL))
- ] [
- UIfastimg "ui/" "ui_bar" "1" $.UI_pad_2XL 0 [
- uispace $.UI_pad_MS $.UI_pad_MS [
- case (? $.snewd $.snewd $.scurd) (- 0 $.sw) [
- uialign 0 -1
- UItriangle (? (iskeyheld "UP") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 0
- ] -1 [
- uialign -1 0
- UItriangle (? (iskeyheld "LEFT") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 90
- ] $.sw [
- uialign 0 1
- UItriangle (? (iskeyheld "DOWN") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 180
- ] 1 [
- uialign 1 0
- UItriangle (? (iskeyheld "RIGHT") $c_fill $c_menu) $.UI_pad_M $.UI_pad_S 270
- ]
- ]
- ]
- ]
- ]
- ]
- ]
- uialign* -1 -1
- uivlist 0 [
- uiclamp.x
- loop x $.sh [ uioutline $c_line 0 $.UI_pad_2XL ]
- uiclamp*x
- ]
- uihlist 0 [
- uiclamp.y
- loop x $.sw [ uioutline $c_line $.UI_pad_2XL 0 ]
- uiclamp*y
- ]
- ]
- ]
- if (= $.state 1) [
- looplist3 offset dir key $.sdirs [
- if (iskeyheld $key) [
- if (&& [< (listfind= $.sinpt $offset) 0] [!= $offset (* $.scurd -1)]) [
- .sinpt = (listsplice $.sinpt $offset 0 0)
- .snewd = $offset
- ]
- ] [
- if (>= (listfind= $.sinpt $offset) 0) [
- .sinpt = (listdel $.sinpt $offset)
- .snewd = (at $.sinpt 0)
- ]
- ]
- ]
- ]
- ] [ snake_reset ]
Add Comment
Please, Sign In to add comment