Step 4¶
Moving the fruit¶
When the snake eats a fruit we will need another unoccupied location for the new fruit.
Let's first compute the list of locations that are unoccupied.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
[]
is an empty sequence.(ForRange)
iterates a range of values (including both:From
and:To
ends).(- a b)
is a built-in function that can act on constant values (as opposed to(Math.Subtract)
that can act on constants and variables).(When)
is similar to(If)
, except it is "passthrough" by default (don't worry about that concept for now).(Is)
and(IsNot)
compare two values.(And)
is a logic operator.(Push)
adds a value to a sequence.
The function takes the positions of the snake body (incl. head and tail) and the current fruit position. It then iterates through all possible coordinates skipping the ones that are either occupied by the snake or the current fruit. The sequence of potential locations is then returned as the function's output.
Now all we need to do is select a random position from this sequence and that will be our next fruit position.
1 2 3 4 5 |
|
(Count)
returns the number of elements in a sequence.(RandomInt)
returns a random value between 0 and the given maximum (exclusive).- We have already seen
(Take)
in step 2. (ToInt2)
ensures that we return anint2
value.
Moving the snake¶
In the snake game, the snake moves by one cell either horizontally or vertically. We could "move" every cell of its body one by one, but there is a clever trick we can use: we can just remove the first element (tail) from the snake's sequence and add one for the new head position.
When the snake is growing (after eating a fruit) we only add the new head (in place of the fruit element) but keep the tail such that snake seems to grow in the direction of the fruit it just ate.
1 2 3 |
|
- We have already seen
(RTake)
in step 3. (WhenNot)
is similar to(When)
but with the opposite logic.(DropFront)
removes the first element of a sequence.
Now we need the snake to move at regular intervals in the direction chosen by the player.
Let's first listen to the player's input. We will use the keyboard's arrow keys (up, down, left, right) for this.
1 2 3 4 |
|
(Inputs.KeyDown)
executes an action when a key is down. It has a sibling event:(Inputs.KeyUp)
.(>)
is an alias forPush
.
We also want to prevent the player from accidentally selecting the opposite direction (e.g. down while the snake is going up) since that would immediately end the game (as the snake would immediately turn backward to eat its own body starting with its neck). One easy way to do this is to compare the newly chosen direction with the previous one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Finally, the snake will move at regular intervals. We can do that by comparing the current time and the time since the last update.
1 2 3 4 5 6 7 |
|
(Time.Now)
returns the time elapsed since the start of the game.
Now the snake will move every half a second. We could change this value to vary the difficulty of the game.
Let's try it out!¶
Putting together all that we have seen so far, and adding a bit of initialization (that we conveniently put in its function to allow us to reinitialize the game if the player hits the space bar), we have the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
Note
(Once)
is executed only once and thus is not repeated every frame.