snake.nv
| @@ -25,11 +25,7 @@ | |||
| 25 | 25 | start a { $cell $grid * } by { $cell $grid * } game .) | |
| 26 | 26 | ||
| 27 | 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 | |
| 28 | + | | '' check keys 'controls' ( w a s d r ) | |
| 33 | 29 | '' update movement timer $dt | |
| 34 | 30 | '' check if snake can eat food | |
| 35 | 31 | '' draw background | |
| @@ -38,6 +34,9 @@ | |||
| 38 | 34 | '' check if snake died | |
| 39 | 35 | '' next frame | |
| 40 | 36 | ||
| 37 | + | | '' check keys? 'controls' $key | '' check if $key is pressed | |
| 38 | + | | '' check keys | | |
| 39 | + | ||
| 41 | 40 | | 'key pressed' w 'direction' down? | | |
| 42 | 41 | | 'key pressed' a 'direction' right? | | |
| 43 | 42 | | 'key pressed' s 'direction' up? | | |
snake.nv
| @@ -10,7 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | 11 | 'snake' . head 5 11 . body 4 11 . body 3 11 | |
| 12 | 12 | 'direction' right | |
| 13 | - | 'movement speed' 5 | |
| 13 | + | 'speed' 1.7 | |
| 14 | 14 | 'timer' 0 | |
| 15 | 15 | 'food' 19 11 | |
| 16 | 16 | 'score' 0 | |
| @@ -24,13 +24,13 @@ | |||
| 24 | 24 | | '' (@reduce @main = | |
| 25 | 25 | start a { $cell $grid * } by { $cell $grid * } game .) | |
| 26 | 26 | ||
| 27 | - | | '' game loop | |
| 27 | + | | '' game loop $dt | |
| 28 | 28 | | '' check if w is pressed | |
| 29 | 29 | '' check if a is pressed | |
| 30 | 30 | '' check if s is pressed | |
| 31 | 31 | '' check if d is pressed | |
| 32 | 32 | '' check if r is pressed | |
| 33 | - | '' update movement timer | |
| 33 | + | '' update movement timer $dt | |
| 34 | 34 | '' check if snake can eat food | |
| 35 | 35 | '' draw background | |
| 36 | 36 | '' draw snake | |
| @@ -66,12 +66,18 @@ | |||
| 66 | 66 | ||
| 67 | 67 | | '' move snake 'new body position' $x $y | '' reset snake | |
| 68 | 68 | ||
| 69 | - | | '' update movement timer 'paused'? | | |
| 70 | - | | '' update movement timer 'movement speed' $speed? 'timer' $speed | |
| 71 | - | | '' move 'timer' 0 | |
| 72 | - | ||
| 73 | - | | '' update movement timer 'timer' $time | |
| 74 | - | | '' (@reduce timer = { $time 1 + } .) | |
| 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 | + | } | |
| 75 | 81 | ||
| 76 | 82 | | '' check if snake can eat food 'snake' head $x $y? 'food' $x $y? | |
| 77 | 83 | | '' move food to random location | |
snake.nv
| @@ -42,10 +42,13 @@ | |||
| 42 | 42 | | 'key pressed' a 'direction' right? | | |
| 43 | 43 | | 'key pressed' s 'direction' up? | | |
| 44 | 44 | | 'key pressed' d 'direction' left? | | |
| 45 | - | | 'key pressed' w 'direction' $_ | 'direction' up | |
| 46 | - | | 'key pressed' a 'direction' $_ | 'direction' left | |
| 47 | - | | 'key pressed' s 'direction' $_ | 'direction' down | |
| 48 | - | | 'key pressed' d 'direction' $_ | 'direction' right | |
| 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 | |
| 49 | 52 | ||
| 50 | 53 | | '' move 'direction' up? | '' move snake 'vector' + 0 - 1 | |
| 51 | 54 | | '' move 'direction' left? | '' move snake 'vector' - 1 + 0 | |
| @@ -105,12 +108,12 @@ | |||
| 105 | 108 | @append $score | |
| 106 | 109 | >) | |
| 107 | 110 | ||
| 108 | - | | 'key pressed' r 'paused' | '' reset game | |
| 111 | + | | 'key pressed' r 'paused' | '' restart game | |
| 109 | 112 | | 'key pressed' r | | |
| 110 | 113 | ||
| 111 | - | | '' reset game? 'seen' $segment $x $y | | |
| 112 | - | | '' reset game? 'snake' $segment $x $y | | |
| 113 | - | | '' reset game 'direction' $direction 'food' $x $y 'score' $score | |
| 114 | + | | '' restart game? 'seen' $segment $x $y | | |
| 115 | + | | '' restart game? 'snake' $segment $x $y | | |
| 116 | + | | '' restart game 'direction' $direction 'food' $x $y 'score' $score | |
| 114 | 117 | | 'snake' . head 5 11 . body 4 11 . body 3 11 | |
| 115 | 118 | 'direction' right | |
| 116 | 119 | 'food' 19 11 | |
snake.nv
| @@ -6,17 +6,11 @@ | |||
| 6 | 6 | https://gist.casuallyblue.dev/autumn/e1c6774f800249be87a94240042135db/raw/HEAD/reduce.nv | |
| 7 | 7 | ) | |
| 8 | 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 | |
| 9 | + | 'DOM' (<query h2 <style text-align = center > >) | |
| 10 | + | ||
| 11 | + | 'snake' . head 5 11 . body 4 11 . body 3 11 | |
| 18 | 12 | 'direction' right | |
| 19 | - | 'movement speed' 4 | |
| 13 | + | 'movement speed' 5 | |
| 20 | 14 | 'timer' 0 | |
| 21 | 15 | 'food' 19 11 | |
| 22 | 16 | 'score' 0 | |
| @@ -24,6 +18,7 @@ | |||
| 24 | 18 | 'grid size' 23 | |
| 25 | 19 | ||
| 26 | 20 | '' start game | |
| 21 | + | '' set score text | |
| 27 | 22 | ||
| 28 | 23 | | '' start game 'cell size' $cell? 'grid size' $grid? | |
| 29 | 24 | | '' (@reduce @main = | |
| @@ -34,11 +29,13 @@ | |||
| 34 | 29 | '' check if a is pressed | |
| 35 | 30 | '' check if s is pressed | |
| 36 | 31 | '' check if d is pressed | |
| 32 | + | '' check if r is pressed | |
| 37 | 33 | '' update movement timer | |
| 38 | 34 | '' check if snake can eat food | |
| 39 | 35 | '' draw background | |
| 40 | - | '' draw food | |
| 41 | 36 | '' draw snake | |
| 37 | + | '' draw food | |
| 38 | + | '' check if snake died | |
| 42 | 39 | '' next frame | |
| 43 | 40 | ||
| 44 | 41 | | 'key pressed' w 'direction' down? | | |
| @@ -66,6 +63,7 @@ | |||
| 66 | 63 | ||
| 67 | 64 | | '' move snake 'new body position' $x $y | '' reset snake | |
| 68 | 65 | ||
| 66 | + | | '' update movement timer 'paused'? | | |
| 69 | 67 | | '' update movement timer 'movement speed' $speed? 'timer' $speed | |
| 70 | 68 | | '' move 'timer' 0 | |
| 71 | 69 | ||
| @@ -86,13 +84,38 @@ | |||
| 86 | 84 | | '' (@reduce score = { $score 1 + } .) | |
| 87 | 85 | ||
| 88 | 86 | | '' set score text 'score' $score? | |
| 89 | - | | 'DOM' (<query h2 @clear @append [score : ] @append $score >) | |
| 87 | + | | 'DOM' (<query h2 @clear @append [Score: ] @append $score >) | |
| 90 | 88 | ||
| 91 | 89 | | '' extend snake? 'snake' $segment $x $y | 'seen' $segment $x $y | |
| 92 | 90 | | '' extend snake | '' duplicate tail segment | |
| 93 | 91 | | '' duplicate tail segment 'seen' $segment $x $y? | |
| 94 | 92 | | 'snake' $segment $x $y '' reset snake | |
| 95 | 93 | ||
| 94 | + | | '' check if snake died? 'snake' head $x $y | 'head' $x $y | |
| 95 | + | | '' check if snake died 'snake' body $x $y 'head' $x $y? | | |
| 96 | + | '' . reset snake . replace head . snake died | |
| 97 | + | | '' check if snake died? 'snake' body $x $y | 'seen' body $x $y | |
| 98 | + | | '' check if snake died | '' reset snake '' replace head | |
| 99 | + | | '' replace head 'head' $x $y | 'snake' head $x $y | |
| 100 | + | ||
| 101 | + | | '' snake died 'score' $score? | |
| 102 | + | | 'paused' | |
| 103 | + | 'DOM' (<query h2 @clear @append | |
| 104 | + | [Game over! Press R to restart. Score: ] | |
| 105 | + | @append $score | |
| 106 | + | >) | |
| 107 | + | ||
| 108 | + | | 'key pressed' r 'paused' | '' reset game | |
| 109 | + | | 'key pressed' r | | |
| 110 | + | ||
| 111 | + | | '' reset game? 'seen' $segment $x $y | | |
| 112 | + | | '' reset game? 'snake' $segment $x $y | | |
| 113 | + | | '' reset game 'direction' $direction 'food' $x $y 'score' $score | |
| 114 | + | | 'snake' . head 5 11 . body 4 11 . body 3 11 | |
| 115 | + | 'direction' right | |
| 116 | + | 'food' 19 11 | |
| 117 | + | 'score' 0 '' set score text | |
| 118 | + | ||
| 96 | 119 | | '' draw background | |
| 97 | 120 | | '@graphics' clear #000c0f | |
| 98 | 121 | ||
snake.nv
| @@ -6,12 +6,17 @@ | |||
| 6 | 6 | https://gist.casuallyblue.dev/autumn/e1c6774f800249be87a94240042135db/raw/HEAD/reduce.nv | |
| 7 | 7 | ) | |
| 8 | 8 | ||
| 9 | + | 'DOM' ( <query h2 | |
| 10 | + | <style text-align = center > | |
| 11 | + | @clear @append [score : 0] | |
| 12 | + | > ) | |
| 13 | + | ||
| 9 | 14 | 'snake' | |
| 10 | 15 | . head 5 11 | |
| 11 | 16 | . body 4 11 | |
| 12 | 17 | . body 3 11 | |
| 13 | 18 | 'direction' right | |
| 14 | - | 'movement speed' 2 | |
| 19 | + | 'movement speed' 4 | |
| 15 | 20 | 'timer' 0 | |
| 16 | 21 | 'food' 19 11 | |
| 17 | 22 | 'score' 0 | |
| @@ -70,12 +75,19 @@ | |||
| 70 | 75 | | '' check if snake can eat food 'snake' head $x $y? 'food' $x $y? | |
| 71 | 76 | | '' move food to random location | |
| 72 | 77 | '' extend snake | |
| 73 | - | ||
| 78 | + | '' increase score | |
| 79 | + | '' set score text | |
| 74 | 80 | | '' check if snake can eat food | | |
| 75 | 81 | ||
| 76 | 82 | | '' move food to random location 'food' $x $y 'grid size' $max? | |
| 77 | 83 | | '' (@reduce food = { 0 $max random } { 0 $max random } .) | |
| 78 | 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 | + | ||
| 79 | 91 | | '' extend snake? 'snake' $segment $x $y | 'seen' $segment $x $y | |
| 80 | 92 | | '' extend snake | '' duplicate tail segment | |
| 81 | 93 | | '' duplicate tail segment 'seen' $segment $x $y? | |
snake.nv
| @@ -7,12 +7,14 @@ | |||
| 7 | 7 | ) | |
| 8 | 8 | ||
| 9 | 9 | 'snake' | |
| 10 | - | . head 7 11 | |
| 11 | - | . body 6 11 | |
| 12 | - | . body 5 11 | |
| 10 | + | . head 5 11 | |
| 11 | + | . body 4 11 | |
| 12 | + | . body 3 11 | |
| 13 | 13 | 'direction' right | |
| 14 | - | 'movement speed' 6 | |
| 14 | + | 'movement speed' 2 | |
| 15 | 15 | 'timer' 0 | |
| 16 | + | 'food' 19 11 | |
| 17 | + | 'score' 0 | |
| 16 | 18 | 'cell size' 16 | |
| 17 | 19 | 'grid size' 23 | |
| 18 | 20 | ||
| @@ -28,7 +30,9 @@ | |||
| 28 | 30 | '' check if s is pressed | |
| 29 | 31 | '' check if d is pressed | |
| 30 | 32 | '' update movement timer | |
| 33 | + | '' check if snake can eat food | |
| 31 | 34 | '' draw background | |
| 35 | + | '' draw food | |
| 32 | 36 | '' draw snake | |
| 33 | 37 | '' next frame | |
| 34 | 38 | ||
| @@ -63,6 +67,20 @@ | |||
| 63 | 67 | | '' update movement timer 'timer' $time | |
| 64 | 68 | | '' (@reduce timer = { $time 1 + } .) | |
| 65 | 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 | + | ||
| 66 | 84 | | '' draw background | |
| 67 | 85 | | '@graphics' clear #000c0f | |
| 68 | 86 | ||
| @@ -80,10 +98,22 @@ | |||
| 80 | 98 | ||
| 81 | 99 | | '' draw snake | '' reset snake | |
| 82 | 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 | + | ||
| 83 | 106 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? | |
| 84 | 107 | | '@js' $c.fillStyle = $color; | |
| 85 | 108 | $c.fillRect($x, $y, $size, $size); | |
| 86 | 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 | + | ||
| 87 | 117 | | '' convert $x $y to pixel position 'cell size' $scalar? | |
| 88 | 118 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) | |
| 89 | 119 | ||
snake.nv
| @@ -6,8 +6,11 @@ | |||
| 6 | 6 | https://gist.casuallyblue.dev/autumn/e1c6774f800249be87a94240042135db/raw/HEAD/reduce.nv | |
| 7 | 7 | ) | |
| 8 | 8 | ||
| 9 | - | 'snake' 11 4 | |
| 10 | - | 'direction' down | |
| 9 | + | 'snake' | |
| 10 | + | . head 7 11 | |
| 11 | + | . body 6 11 | |
| 12 | + | . body 5 11 | |
| 13 | + | 'direction' right | |
| 11 | 14 | 'movement speed' 6 | |
| 12 | 15 | 'timer' 0 | |
| 13 | 16 | 'cell size' 16 | |
| @@ -29,18 +32,30 @@ | |||
| 29 | 32 | '' draw snake | |
| 30 | 33 | '' next frame | |
| 31 | 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? | | |
| 32 | 39 | | 'key pressed' w 'direction' $_ | 'direction' up | |
| 33 | 40 | | 'key pressed' a 'direction' $_ | 'direction' left | |
| 34 | 41 | | 'key pressed' s 'direction' $_ | 'direction' down | |
| 35 | 42 | | 'key pressed' d 'direction' $_ | 'direction' right | |
| 36 | 43 | ||
| 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 | + | | '' 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 | |
| 44 | 59 | ||
| 45 | 60 | | '' update movement timer 'movement speed' $speed? 'timer' $speed | |
| 46 | 61 | | '' move 'timer' 0 | |
| @@ -49,17 +64,28 @@ | |||
| 49 | 64 | | '' (@reduce timer = { $time 1 + } .) | |
| 50 | 65 | ||
| 51 | 66 | | '' draw background | |
| 52 | - | | '@graphics' clear black | |
| 67 | + | | '@graphics' clear #000c0f | |
| 53 | 68 | ||
| 54 | - | | '' draw snake 'pixel_position' $x $y 'cell size' $size? | |
| 55 | - | | '@graphics' draw square $x $y $size #F75AFF | |
| 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 | |
| 56 | 72 | ||
| 57 | - | | '' draw snake? 'snake' $x $y? | |
| 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? | |
| 58 | 79 | | '' convert $x $y to pixel position | |
| 59 | 80 | ||
| 81 | + | | '' draw snake | '' reset snake | |
| 82 | + | ||
| 60 | 83 | | '@graphics' draw square $x $y $size $color '@@canvas context' $c? | |
| 61 | 84 | | '@js' $c.fillStyle = $color; | |
| 62 | 85 | $c.fillRect($x, $y, $size, $size); | |
| 63 | 86 | ||
| 64 | 87 | | '' convert $x $y to pixel position 'cell size' $scalar? | |
| 65 | 88 | | '' (@reduce pixel_position = { $x $scalar * } { $y $scalar * } .) | |
| 89 | + | ||
| 90 | + | | '' reset snake? 'seen' $segment $x $y | 'snake' $segment $x $y | |
| 91 | + | | '' reset snake | | |
snake.nv
| @@ -1,53 +1,65 @@ | |||
| 1 | - | || 'INSTRUCTIONS' you can move using the WASD keys! | |
| 1 | + | | 'INSTRUCTIONS' | ~~ | |
| 2 | + | you can move using the WASD keys ! | |
| 2 | 3 | ||
| 3 | - | '@include' ( | |
| 4 | - | lib/rpn.nv | |
| 5 | - | lib/platforms/browser_dom.nv | |
| 6 | - | lib/platforms/browser_game.nv | |
| 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 | 7 | ) | |
| 8 | 8 | ||
| 9 | - | 'snake' 200 200 | |
| 9 | + | 'snake' 11 4 | |
| 10 | + | 'direction' down | |
| 11 | + | 'movement speed' 6 | |
| 12 | + | 'timer' 0 | |
| 13 | + | 'cell size' 16 | |
| 14 | + | 'grid size' 23 | |
| 10 | 15 | ||
| 11 | - | '' start a 400 by 400 game | |
| 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 .) | |
| 12 | 21 | ||
| 13 | 22 | | '' game loop | |
| 14 | - | | '' | |
| 15 | - | . check if w is pressed | |
| 16 | - | . check if a is pressed | |
| 17 | - | . check if s is pressed | |
| 18 | - | . check if d is pressed | |
| 19 | - | . draw background | |
| 20 | - | . draw snake | |
| 21 | - | . next frame | |
| 22 | - | ||
| 23 | - | | 'key pressed' w | 'vector' + 0 - 1 | |
| 24 | - | | 'key pressed' a | 'vector' - 1 + 0 | |
| 25 | - | | 'key pressed' s | 'vector' + 0 + 1 | |
| 26 | - | | 'key pressed' d | 'vector' + 1 + 0 | |
| 27 | - | | 'snake' $x $y 'vector' $1 $2 $3 $4 | |
| 28 | - | | '' move snake '@rpn' ($y $4 $3 $x $2 $1) | |
| 29 | - | | '' move snake '@rpn data' ($x $y) | |
| 30 | - | | 'snake' $x $y | |
| 31 | - | ||
| 32 | - | | '' draw background | '@graphics' clear black | |
| 33 | - | ||
| 34 | - | | '' draw snake 'snake' $x $y? | |
| 35 | - | | '@graphics' draw square $x $y 10 green | |
| 36 | - | ||
| 37 | - | | '@graphics' draw square $x $y $size $color | |
| 38 | - | '@@canvas context' $context? | |
| 39 | - | | '@js' | |
| 40 | - | $context.fillStyle = $color; | |
| 41 | - | $context.fillRect($x, $y, $size, $size); | |
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | | '' start a $x by $y game | |
| 46 | - | | '@graphics' . canvas init canvas . set resolution $x $y | |
| 47 | - | 'DOM' ( <query canvas <style display = block margin = [1rem auto] > > ) | |
| 48 | - | '' game loop | |
| 49 | - | ||
| 50 | - | | '' next frame | |
| 51 | - | | '@js' window.requestAnimationFrame(nextFrame); | |
| 52 | - | ||
| 53 | - | || '@js' const nextFrame = () => { f("", "game", "loop"); me.run(); } | |
| 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 * } .) | |
snake.nv
| @@ -43,7 +43,7 @@ | |||
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | 45 | | '' start a $x by $y game | |
| 46 | - | | '@graphics' . canvas init canvas . set-resolution _ $x $y | |
| 46 | + | | '@graphics' . canvas init canvas . set resolution $x $y | |
| 47 | 47 | 'DOM' ( <query canvas <style display = block margin = [1rem auto] > > ) | |
| 48 | 48 | '' game loop | |
| 49 | 49 | ||