-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (69 loc) · 1.62 KB
/
index.js
File metadata and controls
84 lines (69 loc) · 1.62 KB
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 animate = window.requestAnimationFrame;
const canvas = document.createElement('canvas');
const width = 1300;
const height = 500;
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
const startSpeed = 3;
let paused = false;
let playerPaused = false;
const ballState = {
y_speed: startSpeed,
x_speed: startSpeed
}
const player = new Player();
const computer = new Computer();
const ball = new Ball(600, 300);
window.onload = function() {
document.body.appendChild(canvas);
animate(step);
};
const step = function() {
update();
render();
animate(step);
};
const update = function() {
checkPaused();
ball.update(player.paddle, computer.paddle, player, computer);
computer.update(ball);
player.update();
};
const render = function() {
context.fillStyle = "#000";
context.fillRect(0, 0, width, height);
player.render();
computer.render();
ball.render();
if (paused) {
context.font = '50px ZCOOL QingKe HuangYou';
context.fillText(`PAUSED`, 550, 200)
context.fillStyle = '#FFF';
}
};
const keysDown = {};
window.addEventListener("keydown", function(event) {
if (event.keyCode !== 32) {
keysDown[event.keyCode] = true;
}
});
window.addEventListener("keyup", function(event) {
if (event.keyCode === 32) {
paused = !paused;
playerPaused = paused;
} else if (event.keyCode === 27) {
playerPaused = false;
} else {
delete keysDown[event.keyCode];
}
});
const checkPaused = function() {
if (paused) {
ball.x_speed = 0;
ball.y_speed = 0;
} else {
ball.y_speed = ballState.y_speed
ball.x_speed = ballState.x_speed
}
}