-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (112 loc) · 4.27 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
let barOne = {
x: 400,
y: 0,
elem: document.getElementById('bar-1')
}
let barTwo = {
x: 400,
y: 485,
elem: document.getElementById('bar-2')
}
let ball = {
x: 440,
y: 465,
radius: 10,
elem: document.getElementById('ball')
};
let velocityX = 10;
let velocityY = -5;
let barOneScore = 0;
let barTwoScore = 0;
if (localStorage.getItem('PingPongMaxScore') === null) {
localStorage.setItem('PingPongMaxScore', 0);
}
let maxScore = localStorage.getItem('PingPongMaxScore');
document.getElementById('max-score').innerText = `Highest Score: ${maxScore}`;
window.onload = function () {
window.alert("Welcome to the Ping Pong Game! Press SPACE to Play/Pause!");
};
let moveBall = function () {
ball.x += velocityX;
ball.y += velocityY;
if (ball.x + ball.radius >= 900) {
velocityX = -velocityX;
} else if (ball.y <= 15) {
velocityY = -velocityY;
if (ball.x < barTwo.x - ball.radius || ball.x + ball.radius > barOne.x + 100) {
if (barOneScore > barTwoScore) {
window.alert('Game ended! Player 1 won!');
if(barOneScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
} else if(barOneScore === barTwoScore) {
window.alert('Game ended! It\'s a tie!');
if(barOneScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
}else {
window.alert('Game ended! Player 2 won!');
if(barTwoScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
}
localStorage.setItem('PingPongMaxScore', Math.max(localStorage.getItem('PingPongMaxScore'), Math.max(barOneScore, barTwoScore)));
location.reload();
}
barOneScore += 100;
document.getElementById('player-one-score').innerText = `Player One: ${barOneScore}`;
} else if (ball.x <= 0) {
velocityX = -velocityX;
} else if (ball.y + ball.radius >= 475) {
velocityY = -velocityY;
if (ball.x < barTwo.x - ball.radius || ball.x + ball.radius > barTwo.x + 100) {
if (barOneScore > barTwoScore) {
window.alert('Game ended! Player 1 won!');
if(barOneScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
} else if(barOneScore === barTwoScore) {
window.alert('Game ended! It\'s a tie!');
if(barOneScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
} else {
window.alert('Game ended! Player 2 won!');
if(barTwoScore > maxScore) {
window.alert('Congrats! New Highest Score Achieved!');
}
}
localStorage.setItem('PingPongMaxScore', Math.max(localStorage.getItem('PingPongMaxScore'), Math.max(barOneScore, barTwoScore)));
location.reload();
}
barTwoScore += 100;
document.getElementById('player-two-score').innerText = `Player Two: ${barTwoScore}`;
}
ball.elem.style.marginLeft = ball.x + 'px';
ball.elem.style.marginTop = ball.y + 'px';
}
let spaceBarPressed = 0, interval;
window.addEventListener('keydown', function (event) {
if (event.key == ' ') {
if (spaceBarPressed % 2 === 0) {
interval = setInterval(moveBall, 30);
} else {
window.alert("Game Paused!");
clearInterval(interval);
}
spaceBarPressed++;
}
if (event.key == "ArrowLeft") {
barOne.x = (barOne.x - 15 < 0) ? 0 : barOne.x - 15;
barTwo.x = (barTwo.x - 15 < 0) ? 0 : barTwo.x - 15;
barOne.elem.style.marginLeft = barOne.x + 'px';
barTwo.elem.style.marginLeft = barTwo.x + 'px';
console.log(barOne.elem.style.marginLeft);
}
if (event.key == "ArrowRight") {
barOne.x = (barOne.x + 15 > 750) ? 750 : barOne.x + 15;
barTwo.x = (barTwo.x + 15 > 750) ? 750 : barTwo.x + 15;
barOne.elem.style.marginLeft = barOne.x + 'px';
barTwo.elem.style.marginLeft = barTwo.x + 'px';
}
});