Created
April 14, 2020 18:30
-
-
Save Raagh/17d2a21a9a840942889974974edeeb38 to your computer and use it in GitHub Desktop.
Functional Snake Game - Part 2 - final loop
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 COLUMNS = 15 | |
const ROWS = 15 | |
const SPEED = 125 | |
let uglyMutableState = initialState | |
const setupInput = () => { | |
readline.emitKeypressEvents(process.stdin) | |
process.stdin.setRawMode(true) | |
process.stdin.on("keypress", (str, key) => { | |
if (key.ctrl && key.name === "c") process.exit() | |
const options = { | |
UP: addMove(direction.NORTH), | |
LEFT: addMove(direction.WEST), | |
DOWN: addMove(direction.SOUTH), | |
RIGHT: addMove(direction.EAST), | |
} | |
const move = options[key.name.toUpperCase()] | |
uglyMutableState = move(uglyMutableState) | |
}) | |
} | |
const displayState = display(COLUMNS, ROWS) | |
const nextState = step(COLUMNS, ROWS) | |
const runGameLoop = () => { | |
setInterval(() => { | |
displayState(uglyMutableState) | |
uglyMutableState = nextState(uglyMutableState) | |
}, SPEED) | |
} | |
setupInput() | |
runGameLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment