-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
47 lines (39 loc) · 932 Bytes
/
player.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
let player1,player2;
// Player1 movement
let up = false;
let down = false;
function move(){
if(up) {
if (player1.y > 10){
player1.y -= player1.speed;
} else {
return;
}
}
if(down) {
if ((player1.y + player1.height) < canvas.height - 10){
player1.y += player1.speed;
} else {
return;
}
}
//limit move
document.onkeydown = function(e) {
if(e.key == 'ArrowUp') up = true;
if(e.key == 'ArrowDown') down = true;
}
document.onkeyup = function(e) {
if(e.key == 'ArrowUp') up = false;
if(e.key == 'ArrowDown') down = false;
}
}
// player Move - computer
function movePlayer(player){
let centerY = player.y + player.height / 2;
if (centerY < ball.y - player.offset){
player.y += 10;
}
else if (centerY > ball.y + player.offset){
player.y -= 10;
}
}