-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaze.html
344 lines (329 loc) · 9.68 KB
/
maze.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<meta charset="utf-8">
<style>
#GameBoardCanvas {
padding: 5px;
margin-left: auto;
margin-right: auto;
}
body {
background: black;
padding: 5px;
}
h2 {
color: white;
}
</style>
<div id="area1">
<h2>AMaze</h2>
<canvas id="Maze" width="400px" height="400px">
</canvas>
</div>
<div id="div1"></div>
<script src="jquery.js"></script>
<script>
var canvas = $('#Maze');
//The game board 1 = walls, 0 = free space, and -1 = the goal
var board = [
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1],
[ 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1],
[ 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1],
[ 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1],
[ 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1],
[ 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
[ 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1],
[ 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1],
[ 1, -1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
var player = {
x: 1,
y: 1
};
var ctx = canvas[0].getContext('2d');
var ctx2 = canvas[0].getContext('2d');
var width = canvas.width();
var blockSize = width/board.length;
var half = blockSize/4;
var dx=5;
var dy=5;
var x, y, initialx, initialy;
var totalAnger = 0;
var moveIndex = 0;
//Moves with no anger but reaches the goal
//var moves = [4,4,4,4,4,4,4,4,4,2,2,2,2,2,2,2,3,3,1,1,1,1,1,3,3,3,3,2,3,3,3,2,2,4,2,2,2,2,3];
//Moves with some angry (color and shake)
//var moves = [4,4,4,4,4,4,5,4,4,4,2,2,2,5,2,2,2,2,3,5,3,1,1,1,1,5,1,3,3,3,3,5,2,3,3,3,2,2,4,2,2,2,2,3];
//Moves with a lot of anger (color, shake and then bounce)
//var moves = [4,4,4,4,4,4,5,4,4,4,2,2,5,2,5,2,2,2,2,3,5,3,1,1,1,1,5,1,3,3,3,3,5,2,3,3,3,5,5,2,2,4,2,5,5,2,2,2,3];
//Random tests
var moves = [4,4,3,5,5,5,5,5,5,4,4,4,4,4,4,4,4,2,2,2,2,2,2,2,3,3,1,1,1,1,5,1,3,3,3,3,5,2,3,3,3,5,5,2,2,4,2,5,5,2,2,2,3];
//Draw the game board
function draw(){
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, width, width);
ctx.fillStyle="white";
//Loop through the board array drawing the walls and the goal
for(var y = 0; y < board.length; y++){
for(var x = 0; x < board[y].length; x++){
//Draw a wall
if(board[y][x] === 1){
ctx.fillRect(x*blockSize, y*blockSize, blockSize, blockSize);
}
//Draw the goal
else if(board[y][x] === -1){
ctx.beginPath();
ctx.lineWidth = 5;
ctx.strokeStyle = "gold";
ctx.moveTo(x*blockSize, y*blockSize);
ctx.lineTo((x+1)*blockSize, (y+1)*blockSize);
ctx.moveTo(x*blockSize, (y+1)*blockSize);
ctx.lineTo((x+1)*blockSize, y*blockSize);
ctx.stroke();
}
}
}
// console.log("totalAnger is :", totalAnger);
if(totalAnger <= 4)
drawNormalPlayer();
else if(totalAnger > 4 && totalAnger <=7)
drawLittleShakyPlayer();
else if(totalAnger > 7){
// console.log("here");
drawMoreShakyPlayer(3);
dx=5;
dy=5
}
}
function getColor(){
var colors = ["#FFCCCC","#FF9999", "#FF6666", "#FF3333", "#FF0000"];
if(totalAnger>4)
return colors[4];
else
return colors[totalAnger];
}
function drawLittleShakyPlayer(){
var width = canvas.width();
var blockSize = width/board.length;
var half = blockSize/4;
requestAnimationFrame(drawLittleShakyPlayer);
ctx2.clearRect(player.x*blockSize+half-4, player.y*blockSize+half-4,half*2+10, half*2+10);
ctx2.save();
var dx = Math.random()*2;
var dy = Math.random()*2;
ctx2.translate(dx, dy);
ctx2.fillStyle = '#F00';
ctx2.beginPath();
ctx2.arc(player.x*blockSize+half+half, player.y*blockSize+half+half, half, 0, 2*Math.PI);
ctx2.fill();
ctx2.restore();
}
function drawMoreShakyPlayer(){
var width = canvas.width();
var blockSize = width/board.length;
var half = blockSize/4;
requestAnimationFrame(drawMoreShakyPlayer);
ctx2.clearRect(player.x*blockSize+half-4, player.y*blockSize+half-4,half*2+12, half*2+12);
ctx2.save();
var dx = Math.random()*7;
var dy = Math.random()*7;
ctx2.translate(dx, dy);
ctx2.fillStyle = '#F00';
ctx2.beginPath();
ctx2.arc(player.x*blockSize+half+half, player.y*blockSize+half+half, half, 0, 2*Math.PI);
ctx2.fill();
ctx2.restore();
}
function drawNormalPlayer(){
//console.log("normal player")
var width = canvas.width();
var blockSize = width/board.length;
//Draw the player
ctx2.beginPath();
ctx2.fillStyle = getColor();
ctx2.arc(player.x*blockSize+half+half, player.y*blockSize+half+half, half, 0, 2*Math.PI);
ctx2.fill();
}
function shakeBall(){
let start = Date.now(); // remember start time
let timer = setInterval(function() {
// how much time passed from the start?
let timePassed = Date.now() - start;
if (timePassed >= 200) {
clearInterval(timer); // finish the animation after 2 seconds
return;
}
// draw the animation at the moment timePassed
shake();
}, 10);
}
function shake()
{
// console.log("shake", x, dx)
var width = canvas.width();
var blockSize = width/board.length;
var half = blockSize/4;
//requestAnimationFrame(drawShakyPlayer);
ctx2.clearRect(player.x*blockSize+half, player.y*blockSize+half,half*2+8, half*2+8);
ctx2.save();
var dx = Math.random()*5;
var dy = Math.random()*5;
ctx2.translate(dx, dy);
ctx2.fillStyle = getColor();
ctx2.beginPath();
ctx2.arc(player.x*blockSize+half+half, player.y*blockSize+half+half, half, 0, 2*Math.PI);
ctx2.fill();
ctx2.restore();
}
function bounceBall(direction){
x = player.x * blockSize+half+half;
y = player.y *blockSize+half+half;
initialx = x;
initialy = y;
let start = Date.now(); // remember start time
// console.log("-----------", x, dx)
let timer = setInterval(function() {
// how much time passed from the start?
let timePassed = Date.now() - start;
if (timePassed >= 200) {
clearInterval(timer); // finish the animation after 2 seconds
return;
}
// draw the animation at the moment timePassed
if(direction == 1)
bounceUp();
else if(direction == 2)
bounceDown();
else if(direction == 3)
bounceLeft();
else if(direction == 4){
bounceRight();
}
}, 35);
}
function bounceLeft()
{
// console.log("bounceLeft", x, dx)
ctx2.clearRect(initialx-half-half, initialy-half-half,half*2+8, half*2+8);
ctx2.beginPath();
ctx2.fillStyle=getColor();
ctx2.arc(x, y, half, 0, 2*Math.PI);
ctx2.closePath();
ctx2.fill();
// Boundary Logic
if(x < initialx-half || x > initialx) dx=-dx;
x-=dx;
}
function bounceRight()
{
// console.log("bounceRight", x, dx)
ctx2.clearRect(initialx-half, initialy-half,half*2+8, half*2+8);
ctx2.beginPath();
ctx2.fillStyle=getColor();
ctx2.arc(x, y, half, 0, 2*Math.PI);
ctx2.closePath();
ctx2.fill();
if(x>initialx+half || x < initialx) dx=-dx;
x = x + dx;
}
function bounceUp()
{
// console.log("bounceUp", x, dx)
ctx2.clearRect(initialx-half-half, initialy-half-half,half*2+10, half*2+10);
ctx2.beginPath();
ctx2.fillStyle=getColor();
ctx2.arc(x, y, half, 0, 2*Math.PI);
ctx2.closePath();
ctx2.fill();
// Boundary Logic
if(y < initialy-half || y > initialy) dy=-dy;
y-=dy;
}
function bounceDown()
{
// console.log("bounceDown", x, dx)
ctx2.clearRect(initialx-half, initialy-half,half*2+8, half*2+8);
ctx2.beginPath();
ctx2.fillStyle=getColor();
ctx2.arc(x, y, half, 0, 2*Math.PI);
ctx2.closePath();
ctx2.fill();
// Boundary Logic
if(y>initialy+half || y < initialy) dy=-dy;
y = y + dy;
}
//Check to see if the new space is inside the board and not a wall
function canMove(x, y){
return (y>=0) && (y<board.length) && (x >= 0) && (x < board[y].length) && (board[y][x] != 1);
}
draw();
var backtrack=0 ;
var temp =0 ;
for(moveIndex=0; moveIndex < moves.length; moveIndex++){
(function(moveIndex){
window.setTimeout(function(){
if (moves[moveIndex]==1 && temp==2){
backtrack++;
console.log("here1");
}
else if (moves[moveIndex]==2 && temp==1){
backtrack++;
console.log("here2");
}
else if (moves[moveIndex]==3 && temp==4){
backtrack++;
console.log("here3");
}
else if (moves[moveIndex]==4 && temp==3){
backtrack++;
console.log("here4");
}
if (moves[moveIndex] != 5){
temp = moves[moveIndex];
}
console.log(backtrack);
if (moves[moveIndex] != 5){
temp = moves[moveIndex];
}
if(moves[moveIndex] == 1){ // up
if(!canMove(player.x, player.y-1))
draw();
else {
player.y--;
draw();
}
}
else if( moves[moveIndex] == 2){ //down
if(!canMove(player.x, player.y+1))
draw();
else {
player.y++;
draw();
}
}
else if( moves[moveIndex] == 3){ //left
if(!canMove(player.x-1, player.y))
draw();
else {
player.x--;
draw();
}
}
else if( moves[moveIndex] == 4){ //right
if(!canMove(player.x+1, player.y))
draw();
else {
player.x++;
draw();
}
}
else if (moves[moveIndex] == 5){
totalAnger+=1;
draw();
}
}, moveIndex * 200);
}(moveIndex));
}
</script>