-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
142 lines (122 loc) · 4.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pong Game</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
}
canvas {
display: block;
background-color: black;
margin-bottom: 10px;
}
input[type=range] {
display: block;
width: 100%;
margin-bottom: 10px;
}
button {
margin-bottom: 10px;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<input type="range" id="paddleSlider" min="0" max="100" value="50">
<button id="startButton">Start</button>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const paddleSlider = document.getElementById('paddleSlider');
const startButton = document.getElementById('startButton');
const paddleHeight = 10;
let paddleWidth = 100;
const ballRadius = 8;
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
let bounceSoundBuffer;
fetch('https://raw.githubusercontent.com/ervin-szilagyi/test-sounds/main/bounce.wav')
.then(response => response.arrayBuffer())
.then(data => audioContext.decodeAudioData(data))
.then(buffer => {
bounceSoundBuffer = buffer;
});
function playBounceSound() {
const source = audioContext.createBufferSource();
source.buffer = bounceSoundBuffer;
source.connect(audioContext.destination);
source.start(0);
}
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - paddleSlider.offsetHeight - startButton.offsetHeight - 40;
if (canvas.width < 480) {
paddleWidth = canvas.width * 0.2;
}
let ballX = canvas.width / 2;
let ballY = canvas.height / 2;
let ballSpeedX = 2;
let ballSpeedY = 2;
function drawBall() {
ctx.beginPath();
ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
const paddleX = (canvas.width - paddleWidth) * (paddleSlider.value / 100);
ctx.beginPath();
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
}
function update() {
ballX += ballSpeedX;
ballY += ballSpeedY;
if (ballX + ballRadius > canvas.width || ballX - ballRadius < 0) {
ballSpeedX = -ballSpeedX;
playBounceSound();
}
if (ballY - ballRadius < 0) {
ballSpeedY = -ballSpeedY;
playBounceSound();
} else if (ballY + ballRadius >
canvas.height - paddleHeight) {
const paddleX = (canvas.width - paddleWidth) * (paddleSlider.value / 100);
if (ballX > paddleX && ballX < paddleX + paddleWidth) {
ballSpeedY = -ballSpeedY;
playBounceSound();
} else {
ballX = canvas.width / 2;
ballY = canvas.height / 2;
}
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
}
let intervalId;
startButton.addEventListener('click', () => {
if (!intervalId) {
intervalId = setInterval(update, 10);
startButton.textContent = "Stop";
} else {
clearInterval(intervalId);
intervalId = null;
startButton.textContent = "Start";
}
});
</script>
</body>
</html>