snake.nv
· 2.7 KiB · Text
Eredeti
Playground
| 'INSTRUCTIONS' | ~~
you can move using the WASD keys !
|| '@include' (
https://gist.casuallyblue.dev/autumn/056799f483464c668e0a6a2d9db099df/raw/HEAD/game.nv
https://gist.casuallyblue.dev/autumn/e1c6774f800249be87a94240042135db/raw/HEAD/reduce.nv
)
'snake'
. head 7 11
. body 6 11
. body 5 11
'direction' right
'movement speed' 6
'timer' 0
'cell size' 16
'grid size' 23
'' start game
| '' start game 'cell size' $cell? 'grid size' $grid?
| '' (@reduce @main =
start a { $cell $grid * } by { $cell $grid * } game .)
| '' game loop
| '' check if w is pressed
'' check if a is pressed
'' check if s is pressed
'' check if d is pressed
'' update movement timer
'' draw background
'' draw snake
'' next frame
| 'key pressed' w 'direction' down? |
| 'key pressed' a 'direction' right? |
| 'key pressed' s 'direction' up? |
| 'key pressed' d 'direction' left? |
| 'key pressed' w 'direction' $_ | 'direction' up
| 'key pressed' a 'direction' $_ | 'direction' left
| 'key pressed' s 'direction' $_ | 'direction' down
| 'key pressed' d 'direction' $_ | 'direction' right
| '' move 'direction' up? | '' move snake 'vector' + 0 - 1
| '' move 'direction' left? | '' move snake 'vector' - 1 + 0
| '' move 'direction' down? | '' move snake 'vector' + 0 + 1
| '' move 'direction' right? | '' move snake 'vector' + 1 + 0
| '' move snake? 'vector' $1 $2 $3 $4 'snake' head $x $y 'grid size' $size?
| 'new body position' $x $y
'' (@reduce seen = head { $x $2 $1 0 $size 1 - wrap }
{ $y $4 $3 0 $size 1 - wrap } .)
| '' move snake? 'snake' body $x $y 'new body position' $nx $ny
| 'new body position' $x $y
'seen' body $nx $ny
| '' move snake 'new body position' $x $y | '' reset snake
| '' update movement timer 'movement speed' $speed? 'timer' $speed
| '' move 'timer' 0
| '' update movement timer 'timer' $time
| '' (@reduce timer = { $time 1 + } .)
| '' draw background
| '@graphics' clear #000c0f
| '' draw snake? 'snake' head $x $y 'pixel_position' $px $py 'cell size' $size?
| '@graphics' draw square $px $py $size #5af7ff
'seen' head $x $y
| '' draw snake? 'snake' body $x $y 'pixel_position' $px $py 'cell size' $size?
| '' (@reduce @graphics = draw square
{ $px 1 + } { $py 1 + } { $size 2 - } #10b0b0 .)
'seen' body $x $y
| '' draw snake? 'snake' $segment $x $y?
| '' convert $x $y to pixel position
| '' draw snake | '' reset snake
| '@graphics' draw square $x $y $size $color '@@canvas context' $c?
| '@js' $c.fillStyle = $color;
$c.fillRect($x, $y, $size, $size);
| '' convert $x $y to pixel position 'cell size' $scalar?
| '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .)
| '' reset snake? 'seen' $segment $x $y | 'snake' $segment $x $y
| '' reset snake |
| 1 | | 'INSTRUCTIONS' | ~~ |
| 2 | you can move using the WASD keys ! |
| 3 | |
| 4 | || '@include' ( |
| 5 | https://gist.casuallyblue.dev/autumn/056799f483464c668e0a6a2d9db099df/raw/HEAD/game.nv |
| 6 | https://gist.casuallyblue.dev/autumn/e1c6774f800249be87a94240042135db/raw/HEAD/reduce.nv |
| 7 | ) |
| 8 | |
| 9 | 'snake' |
| 10 | . head 7 11 |
| 11 | . body 6 11 |
| 12 | . body 5 11 |
| 13 | 'direction' right |
| 14 | 'movement speed' 6 |
| 15 | 'timer' 0 |
| 16 | 'cell size' 16 |
| 17 | 'grid size' 23 |
| 18 | |
| 19 | '' start game |
| 20 | |
| 21 | | '' start game 'cell size' $cell? 'grid size' $grid? |
| 22 | | '' (@reduce @main = |
| 23 | start a { $cell $grid * } by { $cell $grid * } game .) |
| 24 | |
| 25 | | '' game loop |
| 26 | | '' check if w is pressed |
| 27 | '' check if a is pressed |
| 28 | '' check if s is pressed |
| 29 | '' check if d is pressed |
| 30 | '' update movement timer |
| 31 | '' draw background |
| 32 | '' draw snake |
| 33 | '' next frame |
| 34 | |
| 35 | | 'key pressed' w 'direction' down? | |
| 36 | | 'key pressed' a 'direction' right? | |
| 37 | | 'key pressed' s 'direction' up? | |
| 38 | | 'key pressed' d 'direction' left? | |
| 39 | | 'key pressed' w 'direction' $_ | 'direction' up |
| 40 | | 'key pressed' a 'direction' $_ | 'direction' left |
| 41 | | 'key pressed' s 'direction' $_ | 'direction' down |
| 42 | | 'key pressed' d 'direction' $_ | 'direction' right |
| 43 | |
| 44 | | '' move 'direction' up? | '' move snake 'vector' + 0 - 1 |
| 45 | | '' move 'direction' left? | '' move snake 'vector' - 1 + 0 |
| 46 | | '' move 'direction' down? | '' move snake 'vector' + 0 + 1 |
| 47 | | '' move 'direction' right? | '' move snake 'vector' + 1 + 0 |
| 48 | |
| 49 | | '' move snake? 'vector' $1 $2 $3 $4 'snake' head $x $y 'grid size' $size? |
| 50 | | 'new body position' $x $y |
| 51 | '' (@reduce seen = head { $x $2 $1 0 $size 1 - wrap } |
| 52 | { $y $4 $3 0 $size 1 - wrap } .) |
| 53 | |
| 54 | | '' move snake? 'snake' body $x $y 'new body position' $nx $ny |
| 55 | | 'new body position' $x $y |
| 56 | 'seen' body $nx $ny |
| 57 | |
| 58 | | '' move snake 'new body position' $x $y | '' reset snake |
| 59 | |
| 60 | | '' update movement timer 'movement speed' $speed? 'timer' $speed |
| 61 | | '' move 'timer' 0 |
| 62 | |
| 63 | | '' update movement timer 'timer' $time |
| 64 | | '' (@reduce timer = { $time 1 + } .) |
| 65 | |
| 66 | | '' draw background |
| 67 | | '@graphics' clear #000c0f |
| 68 | |
| 69 | | '' draw snake? 'snake' head $x $y 'pixel_position' $px $py 'cell size' $size? |
| 70 | | '@graphics' draw square $px $py $size #5af7ff |
| 71 | 'seen' head $x $y |
| 72 | |
| 73 | | '' draw snake? 'snake' body $x $y 'pixel_position' $px $py 'cell size' $size? |
| 74 | | '' (@reduce @graphics = draw square |
| 75 | { $px 1 + } { $py 1 + } { $size 2 - } #10b0b0 .) |
| 76 | 'seen' body $x $y |
| 77 | |
| 78 | | '' draw snake? 'snake' $segment $x $y? |
| 79 | | '' convert $x $y to pixel position |
| 80 | |
| 81 | | '' draw snake | '' reset snake |
| 82 | |
| 83 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? |
| 84 | | '@js' $c.fillStyle = $color; |
| 85 | $c.fillRect($x, $y, $size, $size); |
| 86 | |
| 87 | | '' convert $x $y to pixel position 'cell size' $scalar? |
| 88 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) |
| 89 | |
| 90 | | '' reset snake? 'seen' $segment $x $y | 'snake' $segment $x $y |
| 91 | | '' reset snake | |
| 92 |