-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (134 loc) · 4.35 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!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="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="js/maze.js"></script>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, user-scalable=0, initial-scale=1, maximum-scale=1, minimal-ui" />
<script>
var mazeGame;
var time, timer, startTime;
var mazeOptions = {
onGameEnd: function(didWin){
clearInterval(timer);
gameInProgress = false;
center($("#options").show());
},
onStart: function(){
startTime = new Date();
timer = setInterval(timeTick, 33);
showTime();
}
}
function loadImages(sources, callback) {
var images = {};
var loadedImages = 0;
var numImages = 0;
for(var src in sources) {
numImages++;
}
for(var src in sources) {
images[src] = new Image();
images[src].onload = function() {
if(++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
}
var sources = {
'background': 'maze.jpg',
'character': 'chelsea.png'
}
$(document).ready(function () {
loadImages(sources, function(images) {
console.log('callback');
mazeGame = new MazeGame(document.getElementById('maze'), document.getElementById('background'), images.character, images.background, mazeOptions);
$("form").on('submit', function () {
mazeGame = new MazeGame(document.getElementById('maze'), mazeOptions);
$('#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;
});
$('#key_up').on('click', function(ev) { mazeGame.move('up'); return false; });
$('#key_down').on('click', function(ev) { mazeGame.move('down'); return false; });
$('#key_right').on('click', function(ev) { mazeGame.move('right'); return false; });
$('#key_left').on('click', function(ev) { mazeGame.move('left'); return false; });
});
});
$(window).on('keydown', function (e) {
var keyCode = e.keyCode || e.which,
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;
}
});
function center(e) {
e.css('top', $("#maze").offset().top + $("#maze").height() / 2 - e.outerHeight() / 2)
}
function showTime() {
deltaTime = ((new Date).getTime() - startTime.getTime()) / 1000.0;
deltaTime = deltaTime.toFixed(2);
$("#time").html(deltaTime + " sec");
}
function timeTick() {
showTime();
}
</script>
</head>
<body>
<div style="position: relative;">
<canvas id="background"></canvas>
<canvas id="maze">Sorry your browser doesn't support the canvas element. Try upgrading your browser.</canvas>
</div>
<div id="a">
<div id="controller" style="position: relative;">
<div id="key_up" style="background-color: black; top: -20px; left: 50px; width:45px; height: 45px; position: absolute;">
</div>
<div id="key_down" style="background-color: black; top: 40px; left: 50px; width:45px; height: 45px; position: absolute;">
</div>
<div id="key_left" style="background-color: black; top: 10px; left: 0px; width:45px; height: 45px; position: absolute;">
</div>
<div id="key_right" style="background-color: black; top: 10px; left: 100px; width:45px; height: 45px; position: absolute;">
</div>
</div>
<div id="time"></div>
</div>
<div id="options">
<h2>Cleared!</h2>
<form>
<button type="submit">Try Again</button>
</form>
</div>
</body>
</html>