-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
141 lines (130 loc) · 3.85 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const game = document.getElementById("game");
const playerScore = document.getElementById("playerScore");
const computerScore = document.getElementById("computerScore");
const ball = {
elem: document.getElementById("ball"),
w: 30,
h: 30,
color: 'red',
x: 500,
y: 500,
dx: -8,
dy: 8,
show() {
this.elem.style.width = this.w + 'px'
this.elem.style.height = this.h + 'px'
this.elem.style.backgroundColor = this.color
this.elem.style.borderRadius = '50%'
this.elem.style.left = this.x + 'px'
this.elem.style.top = this.y + 'px'
},
move() {
if (this.y < 0 || this.y >= 570) {
this.dy *= -1;
}
if ((this.x < 10 && this.y >= playerBar.y - 15 && this.y <= playerBar.y + 135) ||
(this.x > 760 && this.y >= computerBar.y - 15 && this.y <= computerBar.y + 135)) {
this.dx *= -1;
} else if (this.x < 0) {
computerScore.innerHTML = `Computer: ${computerBar.score+=10}`;
this.dx *= -1;
} else if (this.x > 760) {
playerScore.innerHTML = `Player: ${playerBar.score+=10}`;
this.dx *= -1;
}
if (Math.random() < 0.01) {
this.dx = Math.random() < 0.5 ? this.dx+=0.05 : this.dx;
this.dy = Math.random() < 0.5 ? this.dy+=0.05 : this.dy;
}
this.x += this.dx;
this.y += this.dy;
this.show();
}
};
const playerBar = {
elem: document.getElementById("playerBar"),
w: 10,
h: 150,
color: 'cyan',
x: 0,
y: 200,
dx: 0,
dy: 10,
score: 0,
show() {
this.elem.style.width = this.w + 'px'
this.elem.style.height = this.h + 'px'
this.elem.style.backgroundColor = this.color
this.elem.style.left = this.x + 'px'
this.elem.style.top = this.y + 'px'
},
move(e) {
if (e.keyCode == 38 && this.y > 0) this.y -= 10;
if (e.keyCode == 40 && this.y < 450) this.y += 10;
this.show();
}
};
const computerBar = {
elem: document.getElementById("computerBar"),
w: 10,
h: 150,
color: '#F6AF65',
x: 790,
y: 0,
dx: 0,
dy: 10,
score: 0,
show() {
this.elem.style.width = this.w + 'px'
this.elem.style.height = this.h + 'px'
this.elem.style.backgroundColor = this.color
this.elem.style.left = this.x + 'px'
this.elem.style.top = this.y + 'px'
},
move() {
if (this.y < 0 || this.y > 450) this.dy *= -1;
this.y += this.dy;
this.show();
}
};
function checkScore() {
if (playerBar.score === 30) {
gameOver("You Win:)","green","black");
}
else if (computerBar.score === 30) {
gameOver("Computer Win:))","red","white");
}
}
function updateScores() {
playerScore.innerHTML = `Player: ${playerBar.score}`;
computerScore.innerHTML = `Computer: ${computerBar.score}`;
}
let timer = setInterval(() => {
ball.move();
computerBar.move();
checkScore();
updateScores();
}, 40);
document.onkeydown = (e) => playerBar.move(e);
playerBar.show();
computerBar.show();
function gameOver(message,backgroundColor,messagecolor) {
clearInterval(timer);
const winMessage = document.createElement("div");
winMessage.textContent = message;
game.style.backgroundColor = backgroundColor;
winMessage.style.color = messagecolor;
winMessage.style.fontSize = "24px";
winMessage.style.position = "absolute";
winMessage.style.left = "50%";
winMessage.style.top = "50%";
winMessage.style.transform = "translate(-50%, -50%)";
game.appendChild(winMessage);
const line = document.getElementById("line");
const circle = document.getElementById("circle");
game.removeChild(line);
game.removeChild(circle);
playerBar.elem.remove();
computerBar.elem.remove();
ball.elem.remove();
}