-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sketch.js
161 lines (142 loc) · 4.19 KB
/
Sketch.js
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
/** 2D map of the field which has following entities;
* 0 = BARRIER
* 1 = BISCUIT / play with map to increase or decrease the difficulty level
* 3 = CHERRY
* 4 = GHOST
* 5 = PAC-MAN /find 5 and change location as per from where you want to start your game.
*/
const FIELD = [
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
"0,1,1,1,1,1,1,3,5,1,1,1,1,1,1,1,1,1,1,1",
"0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,3,0,0,1",
"0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1",
"0,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1",
"0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0",
"0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0",
"0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0",
"0,0,0,0,0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0",
"0,1,1,1,1,1,1,1,0,4,1,4,0,1,1,1,1,3,1,0",
"0,1,1,1,1,3,1,1,0,4,1,4,0,1,1,1,1,1,1,0",
"0,0,0,0,0,0,1,1,0,1,0,0,0,1,1,0,0,0,0,0",
"0,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0",
"0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0",
"0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0",
"0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0",
"0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0",
"0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0",
"0,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,0",
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
];
var field = [];
var ghosts = [];
var pacman;
var score;
var endScore;
/**
* function to initialize game
*/
function setup() {
createCanvas(500, 535);
score = 0;
field = generateField();
}
function draw() {
background(51);
drawHUD();
/* update and draw ghosts */
for (var j = 0; j < ghosts.length; j++) {
ghosts[j].update();
ghosts[j].draw();
}
/* update and draw Pac-man */
pacman.update();
pacman.draw();
handleInput(); // keyboard (arrow keys for movement) input
}
/**
* handles user input via keyboard for movements
*/
function handleInput() {
if (keyIsDown(UP_ARROW)) {
pacman.move(0, -1, true);
} else if (keyIsDown(DOWN_ARROW)) {
pacman.move(0, 1, true);
} else if (keyIsDown(LEFT_ARROW)) {
pacman.move(-1, 0, true);
} else if (keyIsDown(RIGHT_ARROW)) {
pacman.move(1, 0, true);
}
}
/**
* draws all tiles except types GHOST and PACMAN
*/
function drawHUD() {
/* field */
for (var i = 0; i < field.length; i++) {
if (field[i].intact) {
if (field[i].type != "GHOST" && field[i].type != "PACMAN")
field[i].draw();
}
}
/* score */
noStroke();
fill(255);
textSize(30);
textAlign(LEFT);
text(score, 5, height - 5);
}
function endGame(won) {
textSize(60);
textAlign(CENTER);
fill(255);
stroke(0);
strokeWeight(5);
if (won) {
text("You win!", width / 2, height / 2);
} else {
text("You lose!", width / 2, height / 2);
}
textSize(50);
text("Press f5 to restart", width / 2, height / 2 + 50);
noLoop();
}
/**
* populates field and ghost arrays
* initializes Pac-man
* based upon FIELD constant
*/
function generateField() {
var f = []; // returning array
var ghostId = 0; // handling behavior of ghost
for (var i = 0; i < FIELD.length; i++) { // loop through each string
var row = FIELD[i].split(",");
for (var j = 0; j < row.length; j++) { // loop through numbers in string
var type = TYPES[row[j]];
var tile = new Tile(j, i, type, -1);
switch (type) {
case "PACMAN":
pacman = tile;
f.push(new Tile(j, i, "OPEN"));
break;
case "GHOST":
var behavior = (ghostId % 2); // every other ghost will be agressive
ghosts.push(new Tile(j, i, type, behavior));
f.push(new Tile(j, i, "OPEN"));
ghostId++;
break;
case "BARRIER":
f.push(tile);
break;
case "CHERRY":
endScore += 10; // worth 10 points
f.push(tile);
break;
case "BISCUIT":
endScore++; // worth 1 point
f.push(tile);
break;
}
}
}
return f;
}