-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
149 lines (133 loc) · 4.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Block Breaker</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #222;
color: #fff;
font-family: Arial, sans-serif;
}
canvas {
display: block;
margin: 0 auto;
}
#counter {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0, 0, 0, 0.5);
padding: 5px 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="counter">Access: 0</div>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
const paddle = { x: 350, y: 550, width: 100, height: 20, dx: 5 };
const ball = { x: 400, y: 500, radius: 10, dx: 3, dy: -3 };
const bricks = [];
const brickRowCount = 5;
const brickColumnCount = 8;
const brickWidth = 80;
const brickHeight = 20;
const brickPadding = 10;
const brickOffsetTop = 50;
const brickOffsetLeft = 35;
for (let c = 0; c < brickColumnCount; c++) {
bricks[c] = [];
for (let r = 0; r < brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
document.addEventListener('keydown', movePaddle);
function movePaddle(e) {
if (e.key === 'ArrowRight') paddle.x += paddle.dx;
if (e.key === 'ArrowLeft') paddle.x -= paddle.dx;
if (paddle.x < 0) paddle.x = 0;
if (paddle.x + paddle.width > canvas.width) paddle.x = canvas.width - paddle.width;
}
function drawPaddle() {
ctx.fillStyle = '#0095dd';
ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = '#ff5733';
ctx.fill();
ctx.closePath();
}
function drawBricks() {
bricks.forEach((column, c) => {
column.forEach((brick, r) => {
if (brick.status === 1) {
const brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
const brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
brick.x = brickX;
brick.y = brickY;
ctx.fillStyle = '#28a745';
ctx.fillRect(brickX, brickY, brickWidth, brickHeight);
}
});
});
}
function collisionDetection() {
bricks.forEach((column) => {
column.forEach((brick) => {
if (brick.status === 1) {
if (
ball.x > brick.x &&
ball.x < brick.x + brickWidth &&
ball.y > brick.y &&
ball.y < brick.y + brickHeight
) {
ball.dy = -ball.dy;
brick.status = 0;
}
}
});
});
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBricks();
drawPaddle();
drawBall();
collisionDetection();
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) ball.dx = -ball.dx;
if (ball.y - ball.radius < 0) ball.dy = -ball.dy;
if (ball.y + ball.radius > canvas.height) {
document.location.reload();
}
if (
ball.x > paddle.x &&
ball.x < paddle.x + paddle.width &&
ball.y + ball.radius > paddle.y
) {
ball.dy = -ball.dy;
}
requestAnimationFrame(draw);
}
async function fetchAccessCount() {
const response = await fetch('/access');
const data = await response.json();
document.getElementById('counter').innerText = `Access: ${data.count}`;
}
fetchAccessCount();
draw();
</script>
</body>
</html>