-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.js
50 lines (45 loc) · 1.99 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
// pos is the PacMan image position variable- it is set to 0 initially
var pos = 0;
//pageWidth is the width of the webpage. This is later used to calculate when Pac-Man needs to turn around.
let pageWidth = window.innerWidth;
//This array contains all the PacMan movement images
const pacArray = [
['PacMan1.png', 'PacMan2.png'],
['PacMan3.png', 'PacMan4.png'],
];
// this variable defines what direction should PacMan go into:
// 0 = left to right
// 1 = right to left (reverse)
var direction = 0;
// This variable helps determine which PacMan image should be displayed. It flips between values 0 and 1
var focus = 0;
// This function is called on mouse click. Every time it is called, it updates the PacMan image, position and direction on the screen.
function Run() {
let img = document.getElementById('PacMan');
let imgWidth = img.width;
focus = (focus + 1) % 2;
direction = checkPageBounds(direction, imgWidth, pos, null);
img.src = pacArray[direction][focus];
if (direction) {
pos -= 20;
img.style.left = pos + 'px';
} else {
pos += 20;
img.style.left = pos + 'px';
}
}
// TODO: Add a Javascript setInterval() method that will call the Run() function above every 300 milliseconds. Note: in the video, Dr. Williams uses the setTimeout() method, but here we are going to use a slightly different
// method called setInterval(), so that you can have practice using this method.
// Inside of the Run() function you will also have to add an extra argument "pageWidth", which is declared on line 4 when you call the checkPageBounds() function below.
setInterval (Run, 300);
// This function determines the direction of PacMan based on screen edge detection.
function checkPageBounds(direction, imgWidth, pos, pageWidth) {
//
// TODO: Complete this to reverse direction upon hitting screen edge
//
if (direction == 0 && pos + imgWidth > pageWidth) direction = 1;
if (direction == 1 && pos <0) direction = 0;
return direction;
}
//Please do not change
module.exports = checkPageBounds;