最終更新 1751739830

a snake game that runs on the Myte playground !

修正履歴 5b3a846588f4413d74ff6392c9d9d729eb41aae6

snake.nv Raw Playground
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