-
Notifications
You must be signed in to change notification settings - Fork 177
/
pacman.js
87 lines (68 loc) · 1.82 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
// Setup initial game stats
let score = 0;
let lives = 2;
// Define your ghosts here
// replace this comment with your four ghosts setup as objects
// Draw the screen functionality
function drawScreen() {
clearScreen();
setTimeout(() => {
displayStats();
displayMenu();
displayPrompt();
}, 10);
}
function clearScreen() {
console.log('\x1Bc');
}
function displayStats() {
console.log(`Score: ${score} Lives: ${lives}`);
}
function displayMenu() {
console.log('\n\nSelect Option:\n'); // each \n creates a new line
console.log('(d) Eat Dot');
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;
}
// 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;
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
const 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', (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', () => {
console.log('\n\nGame Over!\n');
});