snake.nv
· 1.9 KiB · Text
Неформатований
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' 11 4
'direction' down
'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' $_ | 'direction' up
| 'key pressed' a 'direction' $_ | 'direction' left
| 'key pressed' s 'direction' $_ | 'direction' down
| 'key pressed' d 'direction' $_ | 'direction' right
| '' move 'direction' up? | 'vector' + 0 - 1
| '' move 'direction' left? | 'vector' - 1 + 0
| '' move 'direction' down? | 'vector' + 0 + 1
| '' move 'direction' right? | 'vector' + 1 + 0
| 'snake' $x $y 'vector' $1 $2 $3 $4 'grid size' $size?
| '' (@reduce snake = { $x $2 $1 0 $size 1 - wrap }
{ $y $4 $3 0 $size 1 - wrap } .)
| '' update movement timer 'movement speed' $speed? 'timer' $speed
| '' move 'timer' 0
| '' update movement timer 'timer' $time
| '' (@reduce timer = { $time 1 + } .)
| '' draw background
| '@graphics' clear black
| '' draw snake 'pixel_position' $x $y 'cell size' $size?
| '@graphics' draw square $x $y $size #F75AFF
| '' draw snake? 'snake' $x $y?
| '' convert $x $y to pixel position
| '@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 * } .)
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' 11 4 |
10 | 'direction' down |
11 | 'movement speed' 6 |
12 | 'timer' 0 |
13 | 'cell size' 16 |
14 | 'grid size' 23 |
15 | |
16 | '' start game |
17 | |
18 | | '' start game 'cell size' $cell? 'grid size' $grid? |
19 | | '' (@reduce @main = |
20 | start a { $cell $grid * } by { $cell $grid * } game .) |
21 | |
22 | | '' game loop |
23 | | '' check if w is pressed |
24 | '' check if a is pressed |
25 | '' check if s is pressed |
26 | '' check if d is pressed |
27 | '' update movement timer |
28 | '' draw background |
29 | '' draw snake |
30 | '' next frame |
31 | |
32 | | 'key pressed' w 'direction' $_ | 'direction' up |
33 | | 'key pressed' a 'direction' $_ | 'direction' left |
34 | | 'key pressed' s 'direction' $_ | 'direction' down |
35 | | 'key pressed' d 'direction' $_ | 'direction' right |
36 | |
37 | | '' move 'direction' up? | 'vector' + 0 - 1 |
38 | | '' move 'direction' left? | 'vector' - 1 + 0 |
39 | | '' move 'direction' down? | 'vector' + 0 + 1 |
40 | | '' move 'direction' right? | 'vector' + 1 + 0 |
41 | | 'snake' $x $y 'vector' $1 $2 $3 $4 'grid size' $size? |
42 | | '' (@reduce snake = { $x $2 $1 0 $size 1 - wrap } |
43 | { $y $4 $3 0 $size 1 - wrap } .) |
44 | |
45 | | '' update movement timer 'movement speed' $speed? 'timer' $speed |
46 | | '' move 'timer' 0 |
47 | |
48 | | '' update movement timer 'timer' $time |
49 | | '' (@reduce timer = { $time 1 + } .) |
50 | |
51 | | '' draw background |
52 | | '@graphics' clear black |
53 | |
54 | | '' draw snake 'pixel_position' $x $y 'cell size' $size? |
55 | | '@graphics' draw square $x $y $size #F75AFF |
56 | |
57 | | '' draw snake? 'snake' $x $y? |
58 | | '' convert $x $y to pixel position |
59 | |
60 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? |
61 | | '@js' $c.fillStyle = $color; |
62 | $c.fillRect($x, $y, $size, $size); |
63 | |
64 | | '' convert $x $y to pixel position 'cell size' $scalar? |
65 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) |
66 |