-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
102 lines (91 loc) · 2.84 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<title>Maze</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/maze.css" />
<script type="text/javascript" src="bower_components/jquery/dist/jquery.slim.min.js"></script>
<script type="text/javascript" src="js/maze.js"></script>
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
<script>
let timer, mazeGame;
$(document).ready(function () {
let canvasElement = document.getElementById('maze');
mazeGame = new MazeGame(canvasElement, {});
startTick();
$("form").on('submit', function () {
let dimensions = [$('#w').val(), $('#h').val()];
mazeGame = new MazeGame(canvasElement, { dimensions });
startTick();
$('#options, #end-game').hide();
return false;
});
$('body').on('keypress', '#h, #options', function (e) {
return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) ? false : true;
})
$(window).on('click', function(e){
if (!$(e.target).is("#options") && $(e.target).parents('#options').length == 0)
$('#options').hide();
});
$('a[href="#options"]').on('click', function(){
center($('#options').show());
return false;
});
});
$(window).on('keydown', function (e) {
let keyCode = e.keyCode || e.which;
let keyCodes = {
37: "left",
38: "up",
39: "right",
40: "down",
65: "left",
87: "up",
68: "right",
83: "down"
};
if (keyCodes[keyCode] !== null && keyCodes[keyCode] !== undefined) {
// send arrow keys and wsad to game
mazeGame.move(keyCodes[keyCode]);
return false;
} else if (keyCode === 27) {
// close options on escape
$('#options').hide();
return false;
}
});
$(window).on('resize', function(){
mazeGame.ui.center();
});
function center(e) {
e.css('top', $("#maze").offset().top + $("#maze").height() / 2 - e.outerHeight() / 2)
}
function startTick() {
if (timer) {
clearInterval(timer);
}
timer = setInterval(tick, 100);
}
function tick() {
mazeGame.ui.drawTimer();
}
</script>
</head>
<body>
<canvas id="maze">Sorry your browser doesn't support the canvas element. Try upgrading your browser.</canvas>
<div id="a">
<h1>Maze<span> <a href="#options">options</a></span></h1>
<div id="time"></div><div id="steps"></div>
</div>
<div id="options">
<form>
<label for="w">Width</label>
<label for="h">Height</label>
<input id="w" type="number" min="2" step="1" value="16" />
by
<input id="h" type="number" min="2" step="1" value="10" />
<button type="submit">Generate Maze</button>
</form>
</div>
</body>
</html>