Created
February 26, 2020 23:09
-
-
Save javascripto/691102456892f5ea5a95afeea1d97e77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const readline = require('readline'); | |
| readline.emitKeypressEvents(process.stdin); | |
| process.stdin.setRawMode(true); | |
| process.stdin.on('keypress', (str, key) => { | |
| if (key.ctrl && key.name === 'c') { | |
| process.exit(); | |
| } | |
| handleKey(key); | |
| }); | |
| const player = { x: 0, y: 0}; | |
| const board = ('-'.repeat(100) + '\n') | |
| .repeat(20) | |
| .split('\n') | |
| .map(line => line.split('')); | |
| function handleKey(key) { | |
| const movePlayer = ({ | |
| up: () => player.y--, | |
| down: () => player.y++, | |
| left: () => player.x--, | |
| right: () => player.x++, | |
| })[key.name]; | |
| if (movePlayer) { | |
| movePlayer(); | |
| } | |
| render(); | |
| console.log(key); | |
| } | |
| function render() { | |
| const screen = board | |
| .map((line, lineIndex) => { | |
| return line | |
| .map((dash, dashIndex) => { | |
| return dashIndex === player.x && lineIndex === player.y ? 'O' : dash; | |
| }) | |
| .join(''); | |
| }) | |
| .join('\n'); | |
| console.clear(); | |
| console.log(screen); | |
| } | |
| render(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment