-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
520 lines (472 loc) · 11.4 KB
/
game.js
File metadata and controls
520 lines (472 loc) · 11.4 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
var currentLevel = new Array();
// secret king's paths
var LEVEL1PATH = [[1, 1],
[2, 2],
[1, 3],
[2, 4]];
var LEVEL2PATH = [[2, 1],
[1, 2],
[2, 3],
[2, 4],
[2, 5],
[2, 6],
[2, 7]];
var LEVEL3PATH = [[1, 1],
[2, 2],
[3, 2],
[4, 2],
[4, 3],
[4, 4]];
var LEVEL4PATH = [[3, 1],
[2, 2],
[3, 3],
[2, 4],
[3, 5],
[3, 6],
[4, 7]];
var messageNumber;
var level = 1;
var levelHeight;
var levelWidth;
// for making the chess board
var tile_colors = [0x000000, 0xFFFFFF];
var COLOR_BeadBorder = PS.COLOR_GRAY;
var COLOR_BoardLight = PS.COLOR_WHITE;
var COLOR_BoardDark = PS.COLOR_BLACK;
var moveKing;
var kingIndex;
var gameOver;
// numbers associated with pieces
var KING = 7;
var PAWN = 3;
var PAWN_MOVE = 4;
var BISHOP = 5;
var BISHOP_MOVE = 6;
var KNIGHT = 1;
var KNIGHT_MOVE = 2;
// clicks for moving pieces
var firstClick;
var piece;
var pieceX;
var pieceY;
function DrawLevel (levelNumber)
{
//level 1
if (levelNumber == 1)
{
currentLevel = [
[0,0,KING,0],
[0,0,PAWN_MOVE,0],
[0,0,PAWN,0],
[0,PAWN_MOVE,0,0],
[0,PAWN,0,0],
];
}
//level 2
if (levelNumber == 2)
{
currentLevel = [
[0,KING,0,0],
[0,PAWN_MOVE,0,0],
[0,PAWN,0,0],
[0,0,0,BISHOP_MOVE],
[0,0,BISHOP_MOVE,0],
[0,BISHOP_MOVE,0,0],
[BISHOP,0,0,0],
[0,0,0,0],
];
}
//level 3
if (levelNumber == 3)
{
currentLevel = [
[0,KING,0,0,0,0],
[0,0,0,PAWN_MOVE,0,0],
[0,0,KNIGHT_MOVE,PAWN,KNIGHT_MOVE,0],
[0,KNIGHT_MOVE,0,0,0,KNIGHT_MOVE],
[0,0,0,KNIGHT,0,0],
];
}
//level 4
if (levelNumber == 4)
{
currentLevel = [
[0,0,0,0,KING,0],
[0,0,PAWN_MOVE,0,0,BISHOP],
[0,0,PAWN,0,BISHOP_MOVE,0],
[0,0,0,BISHOP_MOVE,0,0],
[0,0,BISHOP_MOVE,0,0,0],
[0,BISHOP_MOVE,KNIGHT_MOVE,0,KNIGHT_MOVE,0],
[BISHOP_MOVE,KNIGHT_MOVE,0,0,0,KNIGHT_MOVE],
[0,0,0,KNIGHT,0,0],
];
}
//starting important variables
levelHeight = currentLevel.length;
levelWidth = currentLevel[0].length;
PS.GridSize(levelWidth, levelHeight);
PS.BeadBorderWidth( PS.ALL, PS.ALL, 1);
PS.StatusColor (PS.COLOR_BLACK);
PS.StatusText ("God Save the King\u2654");
PS.BeadFlash(PS.ALL, PS.ALL, false);
PS.BeadBorderWidth(PS.ALL, PS.ALL, 1);
//loop for each row and column to make the board
for (var y = 0; y < levelHeight; y++)
{
for(var x = 0; x < levelWidth; x++)
{
DrawLevelBead(x, y, currentLevel[y][x]);
PS.BeadData(x, y, currentLevel[y][x]);
PS.BeadBorderColor(x,y,COLOR_BeadBorder);
//this will help create the chess board
if( (x + y) % 2 == 0)
{
PS.BeadColor(x,y,COLOR_BoardLight);
}
else
{
PS.BeadColor(x,y,COLOR_BoardDark);
}
}
}
//variables to reset on reload
firstClick = true;
moveKing = false;
gameOver = false;
};
function DrawLevelBead(x, y, data)
{
//Setting the style to Default
PS.BeadBorderWidth(x, y, 0);
PS.BeadGlyph(x, y, " ");
PS.BeadData(x, y, data);
//checking bead and data
if (data == 0) //empty space
{
PS.BeadColor(x , y, 0xEFEFFE);
}
else if (data == KNIGHT) //Red Knight
{
PS.BeadGlyph(x, y, "\u265E");
PS.BeadGlyphColor(x, y, PS.COLOR_RED);
}
else if (data == KNIGHT_MOVE) //Red knight's path - light red
{
PS.BeadGlyph(x, y, "\u2658");
PS.BeadGlyphColor(x, y, 0xFFA4A4);
}
else if (data == PAWN) //Red Pawn
{
PS.BeadGlyph(x, y, "\u265F");
PS.BeadGlyphColor(x, y, PS.COLOR_RED);
}
else if (data == PAWN_MOVE) //light pawn path
{
PS.BeadGlyph(x, y, "\u2659");
PS.BeadGlyphColor(x, y, 0xFFA4A4);
}
else if (data == BISHOP) //bishop
{
PS.BeadGlyph(x, y, "\u265D");
PS.BeadGlyphColor(x, y, PS.COLOR_RED);
}
else if (data == BISHOP_MOVE) // light bishop path
{
PS.BeadGlyph(x, y, "\u2657");
PS.BeadGlyphColor(x, y, 0xFFA4A4);
}
else if (data == KING) // The King
{
PS.BeadGlyph(x, y, "\u2654");
PS.BeadGlyphColor(x, y, PS.COLOR_BLUE);
}
};
PS.Init = function ()
{
"use strict";
messageNumber = 1;
//load audio files
//sound for a mistake
PS.AudioLoad("fx_hoot");
//winning the level
PS.AudioLoad("fx_tada");
PS.Clock(180);
//every 60 ticks = 1 second
//by default starts with level 1
DrawLevel(level);
};
PS.Click = function (x, y, data, options)
{
"use strict";
// if game isn't over and the king isn't in the move phase, make pieces movable
if (gameOver != true && moveKing != true) {
// making the first click
if (firstClick) {
// PS.Debug("First click\n");
// PS.Debug("Piece: " + data + "\n");
piece = data;
pieceX = x;
pieceY = y;
//highlight the clicked space
PS.BeadBorderWidth(x, y, 5);
PS.BeadBorderColor(x, y, PS.COLOR_YELLOW);
firstClick = false;
}
// checking if the second click can move a piece
else {
//for valid moves, move the piece
if (piece == PAWN)
{
// PS.Debug("Pawn\n");
if (data == PAWN_MOVE) {
DrawLevelBead(x, y, PAWN);
DrawLevelBead(pieceX, pieceY, 0);
}
}
else if (piece == BISHOP)
{
// PS.Debug("Bishop\n");
if (currentLevel[y][x] == BISHOP_MOVE) {
DrawLevelBead(x, y, BISHOP);
DrawLevelBead(pieceX, pieceY, 0);
}
}
else if (piece == KNIGHT)
{
// PS.Debug("Knight\n");
if (currentLevel[y][x] == KNIGHT_MOVE) {
DrawLevelBead(x, y, KNIGHT);
DrawLevelBead(pieceX, pieceY, 0);
}
}
// if not a valid move, do nothing
PS.BeadBorderWidth(pieceX, pieceY, 0); //removes the highlight
firstClick = true;
kingIndex = 0;
}
}
};
PS.Release = function (x, y, data, options)
{
"use strict";
// Put code here for when the mouse button is released over a bead
};
PS.Enter = function (x, y, data, options)
{
"use strict";
// Put code here for when the mouse enters a bead
};
PS.Leave = function (x, y, data, options)
{
"use strict";
// Put code here for when the mouse leaves a bead
};
PS.KeyDown = function (key, shift, ctrl, options)
{
"use strict";
//space bar to start king's movement
if(key == 32)
{
if (moveKing == false)
{
moveKing = true;
}
else if (kingIndex == LEVEL1PATH.length)
{
level++;
}
}
//level 1
else if (key == 49)
{
level = 1;
DrawLevel(1);
}
//level 2
else if (key == 50)
{
level = 2;
DrawLevel(2);
}
//level 3
else if (key == 51)
{
level = 3;
DrawLevel(3);
}
//level 4
else if (key == 52)
{
level = 4;
DrawLevel(4);
}
};
PS.KeyUp = function (key, shift, ctrl, options)
{
"use strict";
// Put code here for when a key is released
};
PS.Wheel = function (dir, options)
{
"use strict";
// Put code here for when mouse wheel is moved
};
PS.Tick = function (options)
{
"use strict";
//messages for each tick
if (messageNumber == 1)
{
PS.StatusText ("Help the King on a safe path");
PS.StatusColor (PS.COLOR_BLACK);
}
if (messageNumber == 2)
{
PS.StatusText ("Move the solid pieces by clicking");
}
if (messageNumber == 3)
{
PS.StatusText ("on them and then");
}
if (messageNumber == 4)
{
PS.StatusText ("clicking on an outlined piece.");
}
if (messageNumber == 5)
{
PS.StatusText ("The King \u2654 has a secret");
}
if (messageNumber == 6)
{
PS.StatusText ("predetermined path.");
}
if (messageNumber == 7)
{
PS.StatusText ("Once you have moved the pieces");
}
if (messageNumber == 8)
{
PS.StatusText ("press the space bar.");
}
if (messageNumber == 9)
{
PS.StatusText ("The King's will then move");
}
if (messageNumber == 10)
{
PS.StatusText ("along his path.");
}
if (messageNumber == 11)
{
PS.StatusText ("God Save the King\u2654");
PS.StatusColor (PS.COLOR_BLUE);
}
messageNumber++;
// move king phase
// if king can move (spacebar hit) and game isn't over, animate king's movement
if (moveKing == true && gameOver != true)
{
if (level == 1)
{
if (kingIndex < LEVEL1PATH.length)
{
// PS.Debug("king moved.\n");
// PS.Debug("X: " + LEVEL1PATH[kingIndex][0] + ", " + LEVEL1PATH[kingIndex][1] + "\n");
isOccupied(LEVEL1PATH[kingIndex][0], LEVEL1PATH[kingIndex][1]);
}
kingIndex++;
if (kingIndex == LEVEL1PATH.length) {
PS.AudioPlay( "fx_tada" );
PS.StatusText ("\u2654 KING SAVED! Press " + (level+1) + " for the next level.");
messageNumber = 12;
}
}
if (level == 2)
{
if (kingIndex < LEVEL2PATH.length)
{
// PS.Debug("king moved.\n");
// PS.Debug("X: " + LEVEL2PATH[kingIndex][0] + ", " + LEVEL2PATH[kingIndex][1] + "\n");
isOccupied(LEVEL2PATH[kingIndex][0], LEVEL2PATH[kingIndex][1]);
}
kingIndex++;
if (kingIndex == LEVEL2PATH.length) {
PS.AudioPlay( "fx_tada" );
PS.StatusText ("\u2654 KING SAVED! Press " + (level+1) + " for the next level.");
messageNumber = 12;
}
}
if (level == 3)
{
if (kingIndex < LEVEL3PATH.length)
{
// PS.Debug("king moved.\n");
// PS.Debug("X: " + LEVEL3PATH[kingIndex][0] + ", " + LEVEL3PATH[kingIndex][1] + "\n");
isOccupied(LEVEL3PATH[kingIndex][0], LEVEL3PATH[kingIndex][1]);
}
kingIndex++;
if (kingIndex == LEVEL3PATH.length) {
PS.AudioPlay( "fx_tada" );
PS.StatusText ("\u2654 KING SAVED! Press " + (level+1) + " for the next level.");
messageNumber = 12;
}
}
if (level == 4)
{
if (kingIndex < LEVEL4PATH.length)
{
// PS.Debug("king moved.\n");
// PS.Debug("X: " + LEVEL4PATH[kingIndex][0] + ", " + LEVEL4PATH[kingIndex][1] + "\n");
isOccupied(LEVEL4PATH[kingIndex][0], LEVEL4PATH[kingIndex][1]);
}
kingIndex++;
if (kingIndex == LEVEL4PATH.length) {
PS.AudioPlay( "fx_tada" );
PS.StatusText ("\u2654 KING SAVED! YOU WON!");
messageNumber = 12;
}
}
}
};
// checks if the given space on the board is occupied or not
function isOccupied(x, y)
{
// if the space is not empty or not a piece highlight bead, the king will die
if (PS.BeadData (x, y) != 0 && PS.BeadData (x, y) != PAWN_MOVE &&
PS.BeadData(x, y) != BISHOP_MOVE && PS.BeadData(x, y) != KNIGHT_MOVE)
{
// PS.Debug("Hit!! X: " + x + ", Y: " + y + "\n");
messageNumber = 12;
if (PS.BeadData(x, y) == PAWN) {
PS.StatusText ("\u265F killed the king! Press " + level + " to reset.");
PS.StatusColor (PS.COLOR_RED);
//highlight the clicked space
PS.BeadBorderWidth(x, y, 5);
PS.BeadBorderColor(x, y, PS.COLOR_RED);
}
else if (PS.BeadData(x, y) == KNIGHT) {
PS.StatusText ("\u265E killed the king! Press " + level + " to reset.");
PS.StatusColor (PS.COLOR_RED);
//highlight the clicked space
PS.BeadBorderWidth(x, y, 5);
PS.BeadBorderColor(x, y, PS.COLOR_RED);
}
else if (PS.BeadData(x, y) == BISHOP) {
PS.StatusText ("\u265D killed the king! Press " + level + " to reset.");
PS.StatusColor (PS.COLOR_RED);
//highlight the clicked space
PS.BeadBorderWidth(x, y, 5);
PS.BeadBorderColor(x, y, PS.COLOR_RED);
}
PS.AudioPlay( "fx_hoot" );
gameOver = true;
}
// open space or highlight bead, so king can move
else
{
PS.StatusText("\u2654 King moving...");
PS.BeadGlyph(x, y, "\u2654");
PS.BeadGlyphColor(x, y, PS.COLOR_BLUE);
// DrawLevelBead(x, y, KING);
}
};