Random way of making a console snake by a random 👍️😃
jdic
Posted on April 27, 2024
One day I was bored, with nothing better to do, so, I decided to make a snake in typescript, this snake is something strange, because the first time I did it was my first day of typescript, so I decided to do it again, here is TypeSnake.
First version: Snake
New version: TypeSnake
Features
- Difficulty Control: You can choose from different difficulty levels, from easy to custom, which allow you to adjust the speed and other game parameters.
- Power-Ups: The game includes power-ups such as magnets, slow motion and bonuses that appear randomly on the board and affect the game in various ways.
- Interactive Console Interface: Uses the readline library to provide an interactive console interface that allows the player to control the snake with the arrow keys.
Challenges Faced
Flickering Drawing on Console
One of the main challenges was the flickering drawing on the console when updating the board every game cycle. This was due to the way the entire board was being cleared and redrawn on each update. To address this issue, the following fixes were implemented:
-
Minimizing Console Cleanup: Instead of clearing and reprinting the entire board on each update, the
process.stdout.write
function was used to update only the parts that have changed. - Use of ANSI Escape Characters: ANSI escape characters were used to move the console cursor and update only the necessary parts of the dashboard.
- Drawing Logic Optimization: Drawing logic was optimized to update only the parts of the board that have changed, rather than redrawing the entire board every cycle.
These solutions helped to improve the smoothness of the game and reduce console flickering, providing a more enjoyable gameplay experience for the user.
process.stdout.write('\x1b[H')
process.stdout.write(board + '\n')
About
The game uses the Bun runtime, so it may not work correctly on windows, as Bun was initially made on macOS and Linux.
Posted on April 27, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.