-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
34 lines (30 loc) · 850 Bytes
/
script.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
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var stickPos = 185;
var ballPosx = 197.5;
var ballPosy = 390;
initilizeStick();
initilizeBall();
document.addEventListener("keydown", moveStick);
function initilizeStick(){
ctx.fillRect(stickPos, 395, 30, 5);
}
function initilizeBall(){
ctx.fillRect(ballPosx, ballPosy, 5, 5);
}
function moveStick(e){
if(e.key == "ArrowLeft") {
if(stickPos > 0) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
stickPos = stickPos - 5
ctx.fillRect(stickPos, 395, 30, 5);
}
}
else if (e.key == "ArrowRight") {
if(stickPos < 370) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
stickPos = stickPos + 5
ctx.fillRect(stickPos, 395, 30, 5);
}
}
}