snake.nv
· 3.7 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'
. head 5 11
. body 4 11
. body 3 11
'direction' right
'movement speed' 2
'timer' 0
'food' 19 11
'score' 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
'' check if snake can eat food
'' draw background
'' draw food
'' 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 + } .)
| '' check if snake can eat food 'snake' head $x $y? 'food' $x $y?
| '' move food to random location
'' extend snake
| '' check if snake can eat food |
| '' move food to random location 'food' $x $y 'grid size' $max?
| '' (@reduce food = { 0 $max random } { 0 $max random } .)
| '' extend snake? 'snake' $segment $x $y | 'seen' $segment $x $y
| '' extend snake | '' duplicate tail segment
| '' duplicate tail segment 'seen' $segment $x $y?
| 'snake' $segment $x $y '' reset snake
| '' 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
| '' draw food 'pixel_position' $x $y 'cell size' $size?
| '@graphics' draw circle $x $y $size #f0b050
| '' draw food? 'food' $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);
| '@graphics' draw circle $x $y $width $color '@@canvas context' $c?
| '@js' $c.beginPath();
const r = $width / 2;
$c.arc(Number($x) + r, Number($y) + r, r, 0, 2 * Math.PI);
$c.fillStyle = $color;
$c.fill();
| '' 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 5 11 |
11 | . body 4 11 |
12 | . body 3 11 |
13 | 'direction' right |
14 | 'movement speed' 2 |
15 | 'timer' 0 |
16 | 'food' 19 11 |
17 | 'score' 0 |
18 | 'cell size' 16 |
19 | 'grid size' 23 |
20 | |
21 | '' start game |
22 | |
23 | | '' start game 'cell size' $cell? 'grid size' $grid? |
24 | | '' (@reduce @main = |
25 | start a { $cell $grid * } by { $cell $grid * } game .) |
26 | |
27 | | '' game loop |
28 | | '' check if w is pressed |
29 | '' check if a is pressed |
30 | '' check if s is pressed |
31 | '' check if d is pressed |
32 | '' update movement timer |
33 | '' check if snake can eat food |
34 | '' draw background |
35 | '' draw food |
36 | '' draw snake |
37 | '' next frame |
38 | |
39 | | 'key pressed' w 'direction' down? | |
40 | | 'key pressed' a 'direction' right? | |
41 | | 'key pressed' s 'direction' up? | |
42 | | 'key pressed' d 'direction' left? | |
43 | | 'key pressed' w 'direction' $_ | 'direction' up |
44 | | 'key pressed' a 'direction' $_ | 'direction' left |
45 | | 'key pressed' s 'direction' $_ | 'direction' down |
46 | | 'key pressed' d 'direction' $_ | 'direction' right |
47 | |
48 | | '' move 'direction' up? | '' move snake 'vector' + 0 - 1 |
49 | | '' move 'direction' left? | '' move snake 'vector' - 1 + 0 |
50 | | '' move 'direction' down? | '' move snake 'vector' + 0 + 1 |
51 | | '' move 'direction' right? | '' move snake 'vector' + 1 + 0 |
52 | |
53 | | '' move snake? 'vector' $1 $2 $3 $4 'snake' head $x $y 'grid size' $size? |
54 | | 'new body position' $x $y |
55 | '' (@reduce seen = head { $x $2 $1 0 $size 1 - wrap } |
56 | { $y $4 $3 0 $size 1 - wrap } .) |
57 | |
58 | | '' move snake? 'snake' body $x $y 'new body position' $nx $ny |
59 | | 'new body position' $x $y |
60 | 'seen' body $nx $ny |
61 | |
62 | | '' move snake 'new body position' $x $y | '' reset snake |
63 | |
64 | | '' update movement timer 'movement speed' $speed? 'timer' $speed |
65 | | '' move 'timer' 0 |
66 | |
67 | | '' update movement timer 'timer' $time |
68 | | '' (@reduce timer = { $time 1 + } .) |
69 | |
70 | | '' check if snake can eat food 'snake' head $x $y? 'food' $x $y? |
71 | | '' move food to random location |
72 | '' extend snake |
73 | |
74 | | '' check if snake can eat food | |
75 | |
76 | | '' move food to random location 'food' $x $y 'grid size' $max? |
77 | | '' (@reduce food = { 0 $max random } { 0 $max random } .) |
78 | |
79 | | '' extend snake? 'snake' $segment $x $y | 'seen' $segment $x $y |
80 | | '' extend snake | '' duplicate tail segment |
81 | | '' duplicate tail segment 'seen' $segment $x $y? |
82 | | 'snake' $segment $x $y '' reset snake |
83 | |
84 | | '' draw background |
85 | | '@graphics' clear #000c0f |
86 | |
87 | | '' draw snake? 'snake' head $x $y 'pixel_position' $px $py 'cell size' $size? |
88 | | '@graphics' draw square $px $py $size #5af7ff |
89 | 'seen' head $x $y |
90 | |
91 | | '' draw snake? 'snake' body $x $y 'pixel_position' $px $py 'cell size' $size? |
92 | | '' (@reduce @graphics = draw square |
93 | { $px 1 + } { $py 1 + } { $size 2 - } #10b0b0 .) |
94 | 'seen' body $x $y |
95 | |
96 | | '' draw snake? 'snake' $segment $x $y? |
97 | | '' convert $x $y to pixel position |
98 | |
99 | | '' draw snake | '' reset snake |
100 | |
101 | | '' draw food 'pixel_position' $x $y 'cell size' $size? |
102 | | '@graphics' draw circle $x $y $size #f0b050 |
103 | | '' draw food? 'food' $x $y? |
104 | | '' convert $x $y to pixel position |
105 | |
106 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? |
107 | | '@js' $c.fillStyle = $color; |
108 | $c.fillRect($x, $y, $size, $size); |
109 | |
110 | | '@graphics' draw circle $x $y $width $color '@@canvas context' $c? |
111 | | '@js' $c.beginPath(); |
112 | const r = $width / 2; |
113 | $c.arc(Number($x) + r, Number($y) + r, r, 0, 2 * Math.PI); |
114 | $c.fillStyle = $color; |
115 | $c.fill(); |
116 | |
117 | | '' convert $x $y to pixel position 'cell size' $scalar? |
118 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) |
119 | |
120 | | '' reset snake? 'seen' $segment $x $y | 'snake' $segment $x $y |
121 | | '' reset snake | |
122 |