-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacmansolution.js
More file actions
33 lines (29 loc) · 862 Bytes
/
pacmansolution.js
File metadata and controls
33 lines (29 loc) · 862 Bytes
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
var pos = 0;
let pageWidth = window.innerWidth;
const pacArray = [
["./images/PacMan1.png", "./images/PacMan2.png"],
["./images/PacMan3.png", "./images/PacMan4.png"],
];
var direction = 0;
var focus = 0;
function Run() {
let img = document.getElementById("PacMan");
let imgWidth = img.width;
focus = (focus + 1) % 2;
direction = checkPageBounds(direction, imgWidth, pos, pageWidth);
img.src = pacArray[direction][focus];
if (direction) {
pos -= 20;
img.style.left = pos + "px";
} else {
pos += 20;
img.style.left = pos + "px";
}
}
setInterval(Run, 200);
function checkPageBounds(direction, imgWidth, pos, pageWidth) {
if (direction == 0 && pos + imgWidth > pageWidth) direction = 1;
if (direction == 1 && pos < 0) direction = 0;
return direction;
}
module.exports = checkPageBounds;