Created
July 27, 2016 23:08
-
-
Save anotheredward/33729829958af9a490c4d428ea6156ec to your computer and use it in GitHub Desktop.
11 Minute Roguelike
This file contains 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
<pre id="maze"></pre> | |
<script> | |
'use strict' | |
const maze = [ | |
'#####', | |
'#@..#', | |
'###.#', | |
'#...#', | |
'#.#####', | |
'#.#...###', | |
'#......T#', | |
'#########' | |
] | |
let map = maze.map(line => line.split('')) | |
function render (map) { | |
document.getElementById('maze').innerHTML = | |
map.map(characters => characters.join('')).join('<br>') | |
} | |
render(map) | |
const keyToDirection = { | |
37: {x: -1, y: 0}, | |
38: {x: 0, y: -1}, | |
39: {x: 1, y: 0}, | |
40: {x: 0, y: 1} | |
} | |
document.onkeydown = function (e) { | |
const direction = keyToDirection[e.keyCode] | |
if (direction) { | |
e.preventDefault() | |
move(direction) | |
} | |
} | |
let player = {x: 1, y: 1} | |
const addPoints = (point1, point2) => | |
({x: point1.x + point2.x, y: point1.y + point2.y}) | |
function move(direction) { | |
const newPosition = addPoints(player, direction) | |
const newCharacter = map[point.y][point.x] | |
if (newCharacter === '.') | |
teleportPlayer(newPosition) | |
else if (newCharacter === 'T') | |
alert("Congrats! You are a winner!") | |
} | |
function teleportPlayer ({x,y}) { | |
map[player.y][player.x] = '.' | |
map[y][x] = '@' | |
player = {x, y} | |
render(map) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment