-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
198 lines (176 loc) · 5.35 KB
/
index.html
File metadata and controls
198 lines (176 loc) · 5.35 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!doctype html>
<meta charset="utf-8">
<title>Ye olde labyrinthe game</title>
<style>
*
{
-webkit-user-select: none;
-moz-user-select: none;
text-align: center;
}
#timer{white-space: pre; font-family: "Lucida Console",monospace; left: 40px; text-align: normal;}
</style>
<body>
<p id="timer">Step off the plate and you've got 90 seconds...</p>
<canvas width="1000" height="800" style="border: 2px solid black" oncontextmenu="return false;"></canvas>
<p id="gameMessages">Just a sec, the minions are hard at work constructing your doom.</p>
<script src="labyrinthgenerator.js"></script>
<script>
// Generate labyrinth
var nRows = 25;
var nCols = 25;
var labyrinth = labyrinthgenerator(nRows, nCols);
// Initialize array to count number of times the player stepped in each location
var floorTiles = [];
for(var i = 0; i < nRows*nCols; i++)
floorTiles[i] = 0;
var playerCell = 0;
floorTiles[playerCell] = 1;
// How big to draw the cells
var CELL_SIZE = 21;
// Set up kayboard event listener and keyCodes
window.addEventListener("keydown", keyboardHandler, false);
var UP = 38;
var DOWN = 40;
var RIGHT = 39;
var LEFT = 37;
// Set up HTML5 canvas
var canvas = document.querySelector("canvas");
var drawingSurface = canvas.getContext("2d");
canvas.width = nRows*CELL_SIZE;
canvas.height = nCols*CELL_SIZE;
// Set up paragraph to show game messages
var gameMessages = document.querySelector("#gameMessages");
gameMessages.innerHTML = "Use the arrow keys to escape the <strong>MAZE OF DEATH</strong> <em>Mwahahahaahaaa...</em><br><br>You are green, get to blue.<br>Backtracking costs you points so watch out.<br>Also, when the time runs out the doors will shut for ever. Toodeloo!";
// Set up timer
var timer = document.querySelector("#timer");
var timeLeft = 90;
var gameRunning = false;
// Draw starting situation
render();
// Handles keyboard input
function keyboardHandler(event)
{
// Start the game
if(!gameRunning && event.keyCode >= 37 && event.keyCode <= 40)
{
timerTick();
gameRunning = true;
}
// Get player (x, y) position from index
var playerRow = Math.floor(playerCell/nRows);
var playerCol = playerCell % nCols;
// Find out where the player wants to go
var nextCell = -1;
switch(event.keyCode)
{
case UP:
if(playerRow > 0)
nextCell = playerCell - nCols;
break;
case DOWN:
if(playerRow < nRows - 1)
nextCell = playerCell + nCols;
break;
case LEFT:
if(playerCol > 0)
nextCell = playerCell - 1;
break;
case RIGHT:
if(playerCol < nCols - 1)
nextCell = playerCell + 1;
break
}
// If arrow key was pressed and cells playerCell and nextCell are adjacent, move player and update game state
if(nextCell >= 0 && labyrinth[playerCell][nextCell])
{
playerCell = nextCell;
floorTiles[playerCell] = floorTiles[playerCell] + 1;
render();
}
// If the player is at the bottom right corner he has won
if(playerCell == nRows*nCols-1)
{
window.removeEventListener("keydown", keyboardHandler, false);
var nSteps = 0;
var nBacktrack = 0;
for(var i = 0; i < nRows*nCols; i++)
{
nSteps += floorTiles[i] > 0;
nBacktrack += floorTiles[i];
}
nBacktrack = nBacktrack - nSteps;
gameMessages.innerHTML = "Conga ratz, you made it out this time...<br>It took you " + nSteps + " steps of which " + Math.ceil(1000*nBacktrack/nSteps)/10 + "% were feeble and confused backtracking.";
}
}
// Handles updating the timer
function timerTick()
{
// If the player is not at the goal
if(playerCell != nRows*nCols-1)
{
// ... and there is time left
if(timeLeft)
{
// Set up next call of timerTick
window.setTimeout(function(){timerTick();}, 1000);
timeLeft--;
// Update timer display
var msg = timeLeft + " Seconds left";
if(timeLeft % 2)
msg = "<b>TICK!</b> " + msg + " ";
else
msg = " " + msg + " <b>TOCK!</b>";
timer.innerHTML = msg;
}
// Time has run out, end the game
else
{
window.removeEventListener("keydown", keyboardHandler, false);
gameMessages.innerHTML = "<b><em>WHAM!</em></b>";
}
}
}
// Draws the labyrinth walls and heatmap of player steps
function render()
{
// Draw player position, exit and heatmap
drawingSurface.clearRect(0, 0, canvas.width, canvas.height);
for(var cell = 0; cell < nRows*nCols; cell++)
{
var cellRow = Math.floor(cell/nRows);
var cellCol = cell % nCols;
if(cell == playerCell)
drawingSurface.fillStyle = "rgba(0, 255, 0, 0.4)";
else if(cell == nRows*nCols-1)
drawingSurface.fillStyle = "rgba(0, 0, 255, 0.6)";
else
drawingSurface.fillStyle = "rgba(255, 0, 0, " + floorTiles[cell]/10 + ")";
drawingSurface.fillRect(cellCol*CELL_SIZE, cellRow*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
// Draw walls over heatmap
drawingSurface.lineWidth = 2;
for(var cell = 0; cell < nRows*nCols; cell++)
{
var cellRow = Math.floor(cell/nRows);
var cellCol = cell % nCols;
if(cellRow < nRows - 1)
if(!labyrinth[cell][cell+nCols])
{
drawingSurface.beginPath();
drawingSurface.moveTo(cellCol*CELL_SIZE, (cellRow+1)*CELL_SIZE);
drawingSurface.lineTo((cellCol+1)*CELL_SIZE, (cellRow+1)*CELL_SIZE);
drawingSurface.stroke();
}
if(cellCol < nCols - 1)
if(!labyrinth[cell][cell+1])
{
drawingSurface.beginPath();
drawingSurface.moveTo((cellCol+1)*CELL_SIZE, cellRow*CELL_SIZE);
drawingSurface.lineTo((cellCol+1)*CELL_SIZE, (cellRow+1)*CELL_SIZE);
drawingSurface.stroke();
}
}
}
</script>
</body>