forked from bitmakerlabs/wdi-pacman-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.js
178 lines (154 loc) · 3.52 KB
/
pacman.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Setup initial game stats
var score = 0;
var lives = 2;
var powerPellets = 4;
// Define your ghosts here
var inky = {
menu_option: '1',
name: 'Inky',
colour: 'Red',
character: 'Shadow',
edible: false
};
var blinky = {
menu_option: '2',
name: 'Blinky',
colour: 'Cyan',
character: 'Speedy',
edible: false
};
var pinky = {
menu_option: '3',
name: 'Pinky',
colour: 'Pink',
character: 'Bashful',
edible: false
};
var clyde = {
menu_option: '4',
name: 'Clyde',
colour: 'Orange',
character: 'Pokey',
edible: false
};
// replace this comment with your four ghosts setup as objects
var ghosts = [inky, blinky, pinky, clyde];
// Draw the screen functionality
function drawScreen() {
clearScreen();
setTimeout(function() {
displayStats();
displayMenu();
displayPrompt();
}, 10);
}
function clearScreen() {
console.log('\x1Bc');
}
function displayStats() {
console.log('Score: ' + score + ' Lives: ' + lives + '\n\n\nPower-Pellets: ' + powerPellets);
}
function displayMenu() {
console.log('\n\nSelect Option:\n'); // each \n creates a new line
console.log('(d) Eat Dot');
if (powerPellets > 0) {
console.log('(p) Eat Power-Pellet');
}
console.log('(1) Eat Inky' + edible(ghosts[0]));
console.log('(2) Eat Blinky' + edible(ghosts[1]));
console.log('(3) Eat Pinky' + edible(ghostss[2]));
console.log('(4) Eat Cyde' + edible(ghosts[3]));
console.log('(q) Quit');
}
function displayPrompt() {
// process.stdout.write is similar to console.log except it doesn't add a new line after the text
process.stdout.write('\nWaka Waka :v '); // :v is the Pac-Man emoji.
}
// Menu Options
function eatDot() {
console.log('\nChomp!');
score += 10;
}
function edible(ghost) {
if (ghost.edible === false) {
return '(inedible)';
} else {
return '(edible)';
}
}
function eatGhost(ghost) {
if (ghost.edible === false) {
console.log('\nChomp! ' + ghost.name + ' the ' + ghost.colour + ' ate Pac-Man!');
lives -= 1;
gameOver();
} else {
console.log('\nChomp! Pac-Man just ate' + ghost.name + ' the ' + ghost.colour);
score += 200;
ghost.edible = false;
}
}
function gameOver() {
if (lives === 0) {
process.exit()
}
}
function eatPowerPellet() {
if (powerPellets === 0) {
return console.log('\nNo Power-Pellets left');
}
console.log('\nPower Pellets Acvitivated')
score += 50;
powerPellets--;
for (var i = 0; i < ghosts.length; i++) {
ghosts[i].edible = true
}
}
// Process Player's Input
function processInput(key) {
switch(key) {
case '\u0003': // This makes it so CTRL-C will quit the program
case 'q':
process.exit();
break;
case 'd':
eatDot();
break;
case '1':
eatGhost(ghosts[0]);
break;
case '2':
eatGhost(ghosts[1]);
break;
case '3':
eatGhost(ghosts[2]);
break;
case '4':
eatGhost(ghosts[3]);
break;
case 'p':
eatPowerPellet();
break;
default:
console.log('\nInvalid Command!');
}
}
//
// YOU PROBABLY DON'T WANT TO CHANGE CODE BELOW THIS LINE
//
// Setup Input and Output to work nicely in our Terminal
var stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
// Draw screen when game first starts
drawScreen();
// Process input and draw screen each time player enters a key
stdin.on('data', function(key) {
process.stdout.write(key);
processInput(key);
setTimeout(drawScreen, 300); // The command prompt will flash a message for 300 milliseoncds before it re-draws the screen. You can adjust the 300 number to increase this.
});
// Player Quits
process.on('exit', function() {
console.log('\n\nGame Over!\n');
});