Skip to content

Instantly share code, notes, and snippets.

@Raagh
Created April 14, 2020 18:30
Show Gist options
  • Save Raagh/17d2a21a9a840942889974974edeeb38 to your computer and use it in GitHub Desktop.
Save Raagh/17d2a21a9a840942889974974edeeb38 to your computer and use it in GitHub Desktop.
Functional Snake Game - Part 2 - final loop
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