Skip to content

Commit b715fdb

Browse files
committed
pacman demo
1 parent e0645f7 commit b715fdb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pacman.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2018 Google Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* @author ebidel@ (Eric Bidelman)
17+
*/
18+
19+
/**
20+
* Play Google Pac-man Doodle from Node! Uses Puppeteer's keyboard API to
21+
* forward key presses to the browser.
22+
*/
23+
24+
const readline = require('readline');
25+
const puppeteer = require('puppeteer');
26+
27+
(async() => {
28+
29+
const browser = await puppeteer.launch({
30+
headless: false,
31+
args: ['--window-size=800,500']
32+
});
33+
34+
const page = await browser.newPage();
35+
await page.setViewport({width: 800, height: 500, deviceScaleFactor: 2});
36+
await page.goto('https://www.google.com/logos/2010/pacman10-i.html');
37+
38+
process.stdin.on('keypress', async (str, key) => {
39+
// In "raw" mode, so create own kill switch.
40+
if (key.sequence === '\u0003') {
41+
await browser.close();
42+
process.exit();
43+
}
44+
45+
// See https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#keyboarddownkey-options
46+
if (['up', 'down', 'left', 'right'].includes(key.name)) {
47+
const capitalized = key.name[0].toUpperCase() + key.name.slice(1);
48+
const keyName = `Arrow${capitalized}`;
49+
console.log(`page.keyboard.down('${keyName}')`);
50+
await page.keyboard.down(keyName);
51+
}
52+
});
53+
54+
readline.emitKeypressEvents(process.stdin);
55+
process.stdin.setRawMode(true);
56+
57+
})();

0 commit comments

Comments
 (0)