-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-pong.js
204 lines (169 loc) · 4.7 KB
/
ping-pong.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const canvasEl = document.querySelector('canvas'),
canvasCt = canvasEl.getContext('2d')
const teclado = {key: 0}
//objeto campo
const campo = {
altura: window.innerHeight,
largura: window.innerWidth,
desenho: function () {
//campo
canvasCt.fillStyle = 'darkorange'
canvasCt.fillRect(0, 0, this.largura, this.altura)
},
}
//objeto linha
const linha = {
altura: campo.altura,
largura: 10,
desenho: function () {
canvasCt.fillStyle = 'white'
canvasCt.fillRect(campo.largura / 2 - this.largura, 0, this.largura, this.altura)
},
}
//objeto raquetes
const raqueteEsq = {
x: 10,
y: campo.altura/2,
altura: 150,
largura: linha.largura,
_movimentacao: function(){
if (teclado.key === 'w'){
this.y -= 10
}
if (teclado.key === 's'){
this.y += 10
}
},
desenho: function () {
canvasCt.fillStyle = 'white'
canvasCt.fillRect(this.x, this.y, this.largura, this.altura)
this._movimentacao()
}
}
const raqueteDir = {
x: campo.largura - (linha.largura + raqueteEsq.x),
y: campo.altura/2 - raqueteEsq.altura,
altura: raqueteEsq.altura,
largura: linha.largura,
velocidade: 3,
_movimentacao: function (){
console.log(teclado.key)
if (teclado.key === 'ArrowUp'){
this.y -= 10
}
if (teclado.key === 'ArrowDown'){
this.y += 10
}
},
desenho: function () {
canvasCt.fillStyle = 'white'
canvasCt.fillRect(this.x, this.y, this.largura, this.altura)
this._movimentacao()
}
}
//objeto placar
const placar = {
player1: 0,
player2: 0,
desenho: function () {
canvasCt.font = 'bold 50px Arial'
canvasCt.textAlign = 'center'
canvasCt.textBaseline = 'top'
canvasCt.fillStyle = 'white'
canvasCt.fillText(this.player1, campo.largura / 4, 10)
canvasCt.fillText(this.player2, campo.largura / 4 + campo.largura / 2, 10)
}
}
//objeto bola
const bolinha = {
x: campo.largura/2,
y: campo.altura/2,
raio: 15,
velocidade: 10,
direcaoX: 1,
direcaoY: 1,
_calcPosicao: function(){
//impede a bolinha de sair do campo
if ((this.y > campo.altura - this.raio && this.direcaoY > 0) || this.y < 0 + this.raio && this.direcaoY < 0){
this.direcaoY *= -1
}
//verifica se o jogador 1 esta fazendo o ponto
if (this.x > campo.largura - this.raio - raqueteDir.largura - 10){
//verifica se a bola vai bater na raquete direita
if (this.y + this.raio > raqueteDir.y && this.y - this.raio < raqueteDir.y + raqueteDir.altura){
this.direcaoX *= -1
}
//pontua jogador 1
else {
placar.player1++
this._restartGame()
}
}
//verifica se o jogador 2 esta fazendo o ponto
if(this.x < 0 + this.raio + raqueteDir.largura + 10){
if(this.y + this.raio > raqueteEsq.y && this.y - this.raio < raqueteEsq.y + raqueteEsq.altura){
this.direcaoX *= -1
}
else{
placar.player2++
this._restartGame()
}
}
},
_restartGame: function(){
bolinha.x = campo.largura/2
bolinha.y = campo.altura/2
},
_movimentacao: function () {
this.x += this.direcaoX * this.velocidade
this.y += this.direcaoY * this.velocidade
},
desenho: function () {
canvasCt.fillStyle = 'white'
canvasCt.beginPath()
canvasCt.arc(this.x, this.y, this.raio, 0, Math.PI * 2, false)
canvasCt.fill()
this._calcPosicao()
this._movimentacao()
}
}
function setup() {
canvasEl.width = campo.largura
canvasEl.height = campo.altura
canvasCt.width = campo.largura
canvasCt.height = campo.altura
}
function desenho() {
campo.desenho()
linha.desenho()
raqueteEsq.desenho()
raqueteDir.desenho()
placar.desenho()
bolinha.desenho()
}
setup()
desenho()
window.animateFrame = (function () {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
return window.setTimeout(callback, 1000 / 60)
}
)
})()
function main() {
animateFrame(main)
desenho()
}
setup()
main()
document.addEventListener('keydown', function(event){
teclado.key = event.key
})
document.addEventListener('keyup', function(event){
teclado.key = 0
})