-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.js
84 lines (71 loc) · 2.02 KB
/
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
const url = `http://localhost:3000/api/auth/create?username=A${Math.floor(Math.random() * 10000 )}`
fetch (url, {
mode: 'cors'
})
.then((res) => res.json())
.then(data => joinGame(data.access_token))
function joinGame(jwt) {
const socketOptions = {
transportOptions: {
polling: {
extraHeaders: {
Authorization: `Bearer ${jwt}`
}
}
}
};
const socket = io('http://localhost:3000/api/pong/match', socketOptions)
// private ball: Ball;
// private leftPaddle: Paddle;
// private rightPaddle: Paddle;
// private leftScore: number = 0
// private rightScore: number = 0
// private lastUpdate: number = 0
// private difficulty: CONSTANTS.PongMode
// private winner: 'left' | 'right' | null = null
socket.emit('match', {
uid: Math.floor(Math.random() * 10000),
mode: 'easy',
matchType: 'quick'
})
const ctx = document.getElementById('main').getContext('2d')
socket.on('render', (data) => {
ctx.clearRect(0, 0, 600, 600)
ctx.fillRect(data.ball.x, data.ball.y, data.ball.width, data.ball.height)
ctx.fillRect(data.leftPaddle.x, data.leftPaddle.y, data.leftPaddle.width, data.leftPaddle.height)
ctx.fillRect(data.rightPaddle.x, data.rightPaddle.y, data.rightPaddle.width, data.rightPaddle.height)
document.getElementById('score').children[0].innerHTML = data.leftScore
document.getElementById('score').children[1].innerHTML = data.rightScore
})
socket.on('gameEnd', (data) => {
document.getElementById('winner').innerHTML = `Winner is: ${data.winner}`
})
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowUp') {
socket.emit('movePaddle', {
key: 'up',
isDown: true
})
}
if (e.key === 'ArrowDown') {
socket.emit('movePaddle', {
key: 'down',
isDown: true
})
}
})
document.addEventListener('keyup', (e) => {
if (e.key === 'ArrowUp') {
socket.emit('movePaddle', {
key: 'up',
isDown: false
})
}
if (e.key === 'ArrowDown') {
socket.emit('movePaddle', {
key: 'down',
isDown: false
})
}
})
}