In this exercise you have to implement a simple Snake game in a Node.js command line program.
- All output should be written to
stdout
- Use the NPM packages
ansi
andkeypress
for I/O
- The snake must be represented by a filled, green cursor
- The apple must be represented by a filled, red cursor
- A Game Over message must appear once the game is over
- Initially, the snake is positioned in the middle of the game board and automatically moves to the right
- The snake's direction can be controlled using the cursor keys
- If the snake reaches the apple, the number of points are increased and the moving speed increases. Additionally, a new apple appears on a random position inside the game board
- If the player hits the wall of the game board, the game is over
Note that in contrast to the original game, the snake does not grow in length over time. Once you have solved this exercise, you can add this logic if you like an additional challenge.
- ANSI Escape Codes
- You can clear the screen with
process.stdout.write('\x1Bc');
- You can hide the cursor with
process.stdout.write('\x1B[?25l');
- You can show the cursor with
process.stdout.write('\x1B[?25h');
- You can clear the screen with
- Make sure your source code is Lint warning free by checking it online. Make sure to set the following rule configuration:
- ECMA Version 6
- Environment node
- Create an enhanced version of the game in which the snake's length grows whenever it eats an apple
You can find a sample solution (deliberately without comments) in snake.js. However, before you take a look at it, try to find your own solution!