-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
60 lines (48 loc) · 1.12 KB
/
main.js
File metadata and controls
60 lines (48 loc) · 1.12 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
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
var xpos = 10;
var ypos = 10
function update() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "lime"
ctx.fillRect(xpos, ypos, 8, 8)
requestAnimationFrame(update);
}
//key detection
window.addEventListener('keydown', function(event) {
console.log('Key pressed:', event.key);
console.log('Key code:', event.code);
});
var keyDown = function(e){
if (e.key == "s") {
ypos += 1;
update();
}
if (e.key == "w") {
ypos -= 1;
update();
}
if (e.key == "a") {
xpos -= 1;
update();
}
if (e.key == "d") {
xpos += 1;
update();
}
};
window.addEventListener('keydown', keyDown);
// window.addEventListener("keypressed", function onEvent(event) {
// if (event.key == "W") {
// ypos += 1
// update();
// }
// if (event.key == "S") {
// ypos -= 1
// update();
// }
// });
update();
console.log(document.getElementById("gameCanvas"));