-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
214 lines (184 loc) · 5.74 KB
/
index.html
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
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Pong Game</title>
<style>
body {
background-color: black;
}
@font-face {
font-family: "DOTMATRI";
src: url("DOTMATRI.TTF") format("truetype");
}
canvas {
border: 3px solid #FFF;
display: block;
margin: 20px auto;
}
.header{
font-family: DOTMATRI;
display: flex;
justify-content: center;
color: #FFFFFF;
}
.player1 {
font-family: DOTMATRI;
margin-left: 50px;
color: #FFFFFF;
}
.player2 {
font-family: DOTMATRI;
margin-right: 50px;
color: #FFFFFF;
}
.waiting {
font-family: DOTMATRI;
display: inline;
justify-content: center;
color: #FFFFFF;
}
.notifiers {
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
</head>
<body>
<div>
<h1 class="header">Websocket Pong</h1>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div class="notifiers">
<h1 id="player1" class="player1">
Player 1 not connected.
</h1>
<h1 id="waiting" class="waiting">
Waiting for players.
</h1>
<h1 id="player2" class="player2">
Player 2 not connected.
</h1>
</div>
<script>
let playerOne = {
x:20,
y:250,
width:15,
height:100
}
let playerTwo = {
x:770,
y:250,
width:15,
height:100
}
let pong = new Audio("PONG.mp3")
let score = new Audio("SCORE.mp3")
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
ctx.fillRect(playerOne.x, playerOne.y,playerOne.width, playerOne.height);
ctx.fillRect(playerTwo.x, playerTwo.y, playerTwo.width, playerTwo.height);
const socket = new WebSocket('wss://wspongserver.onrender.com/ws');
socket.addEventListener('open', (event) => {
console.log('WebSocket connection opened:', event);
});
socket.addEventListener('message', (event) => {
const data = JSON.parse(event.data);
ctx.fillStyle = 'white';
let playerInfoOne = document.getElementById("player1")
let playerInfoTwo = document.getElementById("player2")
let waiting = document.getElementById("waiting")
if(!data.player1Y || !data.player2Y) {
waiting.textContent = "Waiting for players."
}
else {
waiting.textContent = '';
}
if (data.player1Y) {
playerInfoOne.textContent = "Player 1 connected!";
}
if (data.player2Y) {
playerInfoTwo.textContent = "Player 2 connected!";
}
var scoreOne;
var scoreTwo;
if(data.score1){
scoreOne = data.score1;
}
else {
scoreOne = 0;
}
if(data.score2){
scoreTwo = data.score2;
}
else {
scoreTwo = 0;
}
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.font = "72px DOTMATRI"
ctx.fillText(scoreOne.toString(), canvas.width / 2 - 70, 70)
ctx.fillText(scoreTwo.toString(), canvas.width / 2 + 50, 70)
drawDashedLine(canvas.width / 2, 10, canvas.height, 20, ctx)
if(data.audio == 1) {
pong.play()
}
else if(data.audio == 2) {
score.play()
}
if(data.player1Y > 0 && data.player1Y < canvas.height - 100 ) {
ctx.fillRect(playerOne.x, data.player1Y, playerOne.width, playerOne.height); // Left paddle
}
else if(data.player1Y < 10) {
ctx.fillRect(playerOne.x, 0, playerOne.width, playerOne.height); // Left paddle
}
else {
ctx.fillRect(playerOne.x, 500, playerOne.width, playerOne.height);
}
if(data.player2Y < canvas.width - 50 && data.player2Y > 0) {
ctx.fillRect(playerTwo.x, data.player2Y, playerTwo.width, playerTwo.height); // Left paddle
}
else if(data.player2Y < 10) {
ctx.fillRect(playerTwo.x, 0, playerTwo.width, playerTwo.height); // Left paddle
}
else {
ctx.fillRect(playerTwo.x, 500, playerTwo.width, playerTwo.height);
}
drawBall(data.ballX, data.ballY, ctx)
});
function drawBall(X,Y,ctx) {
ctx.beginPath();
ctx.arc(X, Y, 10, 0, 2 * Math.PI);
ctx.fillStyle = '#FFF';
ctx.fill();
ctx.closePath();
}
function drawDashedLine(x, y, length, dashLength, ctx) {
ctx.beginPath();
ctx.setLineDash([dashLength, dashLength]);
ctx.strokeStyle = "#FFF"
ctx.moveTo(x, y);
ctx.lineTo(x, y + length);
ctx.stroke();
ctx.setLineDash([]); // Reset line dash to default (solid line)
}
socket.addEventListener('close', (event) => {
console.log('WebSocket connection closed:', event);
});
// Handle user input (e.g., moving the paddle)
document.addEventListener('keydown', (event) => {
const keyCode = event.keyCode;
// For simplicity, let's assume 'W' and 'S' keys control the left paddle
if (keyCode === 87) { // W key
// Send a message to the server indicating the desired action
socket.send(JSON.stringify({ action: 'moveUp' }));
} else if (keyCode === 83) { // S key
socket.send(JSON.stringify({ action: 'moveDown' }));
}
});
</script>
</body>
</html>