Skip to content

Commit 267e07f

Browse files
committed
Added Assignment
1 parent c445f75 commit 267e07f

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
setInterval (Run, 200);
38+
39+
// This function determines the direction of PacMan based on screen edge detection.
40+
function checkPageBounds(direction, imgWidth, pos, pageWidth) {
41+
// TODO: Complete this to reverse direction upon hitting screen edge
42+
if (direction==0 && pos + imgWidth>=pageWidth ){
43+
return direction;
44+
45+
}
46+
else if (direction==1 && pos<=0)
47+
{
48+
return reverse();
49+
}
50+
direction = 1
51+
}
52+
function reverse()
53+
{
54+
55+
}
56+
//Please do not change
57+
module.exports = checkPageBounds;

0 commit comments

Comments
 (0)