Skip to content

Commit 09071c9

Browse files
authored
Add files via upload
1 parent c47cd92 commit 09071c9

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// pos is the PacMan image position variable- it is set to 0 initially
2+
var pos = 0;
3+
//pageWidth is the width of the webpage. This is later used to calculate when Pac-Man needs to turn around.
4+
let pageWidth = window.innerWidth;
5+
//This array contains all the PacMan movement images
6+
const pacArray = [
7+
['./images/PacMan1.png', './images/PacMan2.png'],
8+
['./images/PacMan3.png', './images/PacMan4.png'],
9+
];
10+
11+
// this variable defines what direction should PacMan go into:
12+
// 0 = left to right
13+
// 1 = right to left (reverse)
14+
var direction = 0;
15+
16+
// This variable helps determine which PacMan image should be displayed. It flips between values 0 and 1
17+
var focus = 0;
18+
19+
// This function is called on mouse click. Every time it is called, it updates the PacMan image, position and direction on the screen.
20+
function Run() {
21+
let img = document.getElementById('PacMan');
22+
let imgWidth = img.width;
23+
focus = (focus + 1) % 2;
24+
direction = checkPageBounds(direction, imgWidth, pos, null);
25+
img.src = pacArray[direction][focus];
26+
if (direction) {
27+
pos -= 20;
28+
img.style.left = pos + 'px';
29+
} else {
30+
pos += 20;
31+
img.style.left = pos + 'px';
32+
}
33+
}
34+
// TODO: Add a Javascript setInterval() method that will call the Run() function above every 200 milliseconds. Note: in the video, Dr. Williams uses the setTimeout() method, but here we are going to use a slightly different
35+
// method called setInterval(), so that you can have practice using this method.
36+
// 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.
37+
38+
setInterval(Run,200);
39+
// This function determines the direction of PacMan based on screen edge detection.
40+
function checkPageBounds(direction, imgWidth, pos, pageWidth) {
41+
//
42+
// TODO: Complete this to reverse direction upon hitting screen edge
43+
//
44+
if (direction == 0 && pos + imgWidth > pageWidth) direction = 1;
45+
if (direction == 1 && pos < 0) direction = 0;
46+
return direction;
47+
}
48+
49+
//Please do not change
50+
module.exports = checkPageBounds;

0 commit comments

Comments
 (0)