-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (90 loc) · 2.87 KB
/
script.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//Get the button
var mybutton = document.getElementById("myBtn");
// When the user scrolls down 200px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 200 || document.documentElement.scrollTop > 200) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
// use for projct post start
function postFunction() {
document.body.scrollTop = 1570;
document.documentElement.scrollTop = 1570;
}
// end
// eyes script start
var balls = document.getElementsByClassName("ball");
document.onmousemove = (event) => {
var x = (event.clientX * 100) / window.innerWidth + "%";
var y = (event.clientY * 100) / window.innerHeight + "%";
for (let i = 0; i < 2; i++) {
balls[i].style.left = x;
balls[i].style.top = y;
balls[i].transform = "translate(-" + x + ",-" + y + ")";
}
};
// eyes script end
// packmen start
var pos = 0;
const pacArray = [
['img/PacMan1.png', 'img/PacMan2.png'],
['img/PacMan3.png', 'img/PacMan4.png']
];
var direction = 0;
const pacMen = [];
function setToRandom(scale) {
return {
x: Math.random() * scale,
y: Math.random() * scale
}
}
// Factory to make a PacMan
function makePac() {
// returns an object with values scaled {x: 33, y: 21}
let velocity = setToRandom(10);
let position = setToRandom(200);
// Add image to div id = game
let game = document.getElementById('game');
let newimg = document.createElement('img');
newimg.style.position = 'absolute';
newimg.src = 'img/PacMan1.png';
newimg.width = 100;
newimg.style.left = position.x;
newimg.style.top = position.y;
game.appendChild(newimg);
// new style of creating an object
return {
position,
velocity,
newimg
}
}
function update() {
//loop over pacmen array and move each one and move image in DOM
pacMen.forEach((item) => {
checkCollisions(item)
item.position.x += item.velocity.x;
item.position.y += item.velocity.y;
item.newimg.style.left = item.position.x;
item.newimg.style.top = item.position.y;
})
setTimeout(update, 20);
}
function checkCollisions(item) {
if (item.position.x + item.velocity.x + item.newimg.width > window.innerWidth ||
item.position.x + item.velocity.x < 0) item.velocity.x = -item.velocity.x;
if (item.position.y + item.velocity.y + item.newimg.height > window.innerHeight ||
item.position.y + item.velocity.y < 0) item.velocity.y = -item.velocity.y;
}
function makeOne() {
pacMen.push(makePac()); // add a new PacMan
}
// packmen end--------------------------------------------------------------