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