-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (74 loc) · 1.78 KB
/
index.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
let canvas = document.getElementById("main");
let ctx = canvas.getContext("2d");
let x = canvas.width / 2;
let y = canvas.height - 30;
let ballRadius = 10;
let dx = 2;
let dy = -2;
let paddleHeight = 10;
let paddleWidth = 75;
let paddleX = (canvas.width - paddleWidth) / 2;
let paddleDx = 7;
let rightPressed;
let leftPressed;
function keyDownHandler(event) {
if (event.keyCode === 39) {
rightPressed = true;
} else if (event.keyCode === 37) {
leftPressed = true;
}
}
function keyUpHandler(event) {
if (event.keyCode === 39) {
rightPressed = false;
} else if (event.keyCode === 37) {
leftPressed = false;
}
}
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "blue";
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = "red";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if (y + dy < ballRadius) {
dy = -dy;
}
if (
y + dy < ballRadius ||
(y + dy > canvas.height - paddleHeight - ballRadius &&
x + dx > paddleX &&
x + dx < paddleX + paddleWidth)
) {
dy = -dy;
} else if (y + dy > canvas.height) {
alert("haha");
location.reload();
}
if (rightPressed && paddleX + paddleWidth < canvas.width) {
paddleX += paddleDx;
} else if (leftPressed && paddleX > 0) {
paddleX -= paddleDx;
}
x += dx;
y += dy;
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);