snake.nv
· 5.0 KiB · Text
Raw
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 > >)
'snake' . head 5 11 . body 4 11 . body 3 11
'direction' right
'speed' 1.7
'timer' 0
'food' 19 11
'score' 0
'cell size' 16
'grid size' 23
'' start game
'' set score text
| '' start game 'cell size' $cell? 'grid size' $grid?
| '' (@reduce @main =
start a { $cell $grid * } by { $cell $grid * } game .)
| '' game loop $dt
| '' check if w is pressed
'' check if a is pressed
'' check if s is pressed
'' check if d is pressed
'' check if r is pressed
'' update movement timer $dt
'' check if snake can eat food
'' draw background
'' draw snake
'' draw food
'' check if snake died
'' 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 | 'new direction' up
| 'key pressed' a | 'new direction' left
| 'key pressed' s | 'new direction' down
| 'key pressed' d | 'new direction' right
| '' move? 'direction' $_ 'new direction' $direction |
'direction' $direction
| '' 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 $dt 'paused'? |
| '' update movement timer $dt 'timer' $time
| '' (@reduce timer = { $dt 60 / $time + } .)
'' move if timer is done
| '' move if timer is done 'timer' $time 'speed' $speed?
| '@js' if ($time > $speed) {
f("", "move");
f("timer", 0);
} else {
f("timer", $time);
}
| '' 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
| '' check if snake died? 'snake' head $x $y | 'head' $x $y
| '' check if snake died 'snake' body $x $y 'head' $x $y? |
'' . reset snake . replace head . snake died
| '' check if snake died? 'snake' body $x $y | 'seen' body $x $y
| '' check if snake died | '' reset snake '' replace head
| '' replace head 'head' $x $y | 'snake' head $x $y
| '' snake died 'score' $score?
| 'paused'
'DOM' (<query h2 @clear @append
[Game over! Press R to restart. Score: ]
@append $score
>)
| 'key pressed' r 'paused' | '' restart game
| 'key pressed' r |
| '' restart game? 'seen' $segment $x $y |
| '' restart game? 'snake' $segment $x $y |
| '' restart game 'direction' $direction 'food' $x $y 'score' $score
| 'snake' . head 5 11 . body 4 11 . body 3 11
'direction' right
'food' 19 11
'score' 0 '' set score text
| '' 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 <style text-align = center > >) |
10 | |
11 | 'snake' . head 5 11 . body 4 11 . body 3 11 |
12 | 'direction' right |
13 | 'speed' 1.7 |
14 | 'timer' 0 |
15 | 'food' 19 11 |
16 | 'score' 0 |
17 | 'cell size' 16 |
18 | 'grid size' 23 |
19 | |
20 | '' start game |
21 | '' set score text |
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 $dt |
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 | '' check if r is pressed |
33 | '' update movement timer $dt |
34 | '' check if snake can eat food |
35 | '' draw background |
36 | '' draw snake |
37 | '' draw food |
38 | '' check if snake died |
39 | '' next frame |
40 | |
41 | | 'key pressed' w 'direction' down? | |
42 | | 'key pressed' a 'direction' right? | |
43 | | 'key pressed' s 'direction' up? | |
44 | | 'key pressed' d 'direction' left? | |
45 | | 'key pressed' w | 'new direction' up |
46 | | 'key pressed' a | 'new direction' left |
47 | | 'key pressed' s | 'new direction' down |
48 | | 'key pressed' d | 'new direction' right |
49 | |
50 | | '' move? 'direction' $_ 'new direction' $direction | |
51 | 'direction' $direction |
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 $dt 'paused'? | |
70 | | '' update movement timer $dt 'timer' $time |
71 | | '' (@reduce timer = { $dt 60 / $time + } .) |
72 | '' move if timer is done |
73 | |
74 | | '' move if timer is done 'timer' $time 'speed' $speed? |
75 | | '@js' if ($time > $speed) { |
76 | f("", "move"); |
77 | f("timer", 0); |
78 | } else { |
79 | f("timer", $time); |
80 | } |
81 | |
82 | | '' check if snake can eat food 'snake' head $x $y? 'food' $x $y? |
83 | | '' move food to random location |
84 | '' extend snake |
85 | '' increase score |
86 | '' set score text |
87 | | '' check if snake can eat food | |
88 | |
89 | | '' move food to random location 'food' $x $y 'grid size' $max? |
90 | | '' (@reduce food = { 0 $max random } { 0 $max random } .) |
91 | |
92 | | '' increase score 'score' $score |
93 | | '' (@reduce score = { $score 1 + } .) |
94 | |
95 | | '' set score text 'score' $score? |
96 | | 'DOM' (<query h2 @clear @append [Score: ] @append $score >) |
97 | |
98 | | '' extend snake? 'snake' $segment $x $y | 'seen' $segment $x $y |
99 | | '' extend snake | '' duplicate tail segment |
100 | | '' duplicate tail segment 'seen' $segment $x $y? |
101 | | 'snake' $segment $x $y '' reset snake |
102 | |
103 | | '' check if snake died? 'snake' head $x $y | 'head' $x $y |
104 | | '' check if snake died 'snake' body $x $y 'head' $x $y? | |
105 | '' . reset snake . replace head . snake died |
106 | | '' check if snake died? 'snake' body $x $y | 'seen' body $x $y |
107 | | '' check if snake died | '' reset snake '' replace head |
108 | | '' replace head 'head' $x $y | 'snake' head $x $y |
109 | |
110 | | '' snake died 'score' $score? |
111 | | 'paused' |
112 | 'DOM' (<query h2 @clear @append |
113 | [Game over! Press R to restart. Score: ] |
114 | @append $score |
115 | >) |
116 | |
117 | | 'key pressed' r 'paused' | '' restart game |
118 | | 'key pressed' r | |
119 | |
120 | | '' restart game? 'seen' $segment $x $y | |
121 | | '' restart game? 'snake' $segment $x $y | |
122 | | '' restart game 'direction' $direction 'food' $x $y 'score' $score |
123 | | 'snake' . head 5 11 . body 4 11 . body 3 11 |
124 | 'direction' right |
125 | 'food' 19 11 |
126 | 'score' 0 '' set score text |
127 | |
128 | | '' draw background |
129 | | '@graphics' clear #000c0f |
130 | |
131 | | '' draw snake? 'snake' head $x $y 'pixel_position' $px $py 'cell size' $size? |
132 | | '@graphics' draw square $px $py $size #5af7ff |
133 | 'seen' head $x $y |
134 | |
135 | | '' draw snake? 'snake' body $x $y 'pixel_position' $px $py 'cell size' $size? |
136 | | '' (@reduce @graphics = draw square |
137 | { $px 1 + } { $py 1 + } { $size 2 - } #10b0b0 .) |
138 | 'seen' body $x $y |
139 | |
140 | | '' draw snake? 'snake' $segment $x $y? |
141 | | '' convert $x $y to pixel position |
142 | |
143 | | '' draw snake | '' reset snake |
144 | |
145 | | '' draw food 'pixel_position' $x $y 'cell size' $size? |
146 | | '@graphics' draw circle $x $y $size #f0b050 |
147 | | '' draw food? 'food' $x $y? |
148 | | '' convert $x $y to pixel position |
149 | |
150 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? |
151 | | '@js' $c.fillStyle = $color; |
152 | $c.fillRect($x, $y, $size, $size); |
153 | |
154 | | '@graphics' draw circle $x $y $width $color '@@canvas context' $c? |
155 | | '@js' $c.beginPath(); |
156 | const r = $width / 2; |
157 | $c.arc(Number($x) + r, Number($y) + r, r, 0, 2 * Math.PI); |
158 | $c.fillStyle = $color; |
159 | $c.fill(); |
160 | |
161 | | '' convert $x $y to pixel position 'cell size' $scalar? |
162 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) |
163 | |
164 | | '' reset snake? 'seen' $segment $x $y | 'snake' $segment $x $y |
165 | | '' reset snake | |
166 |