-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkey
78 lines (64 loc) · 1.13 KB
/
key
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
<!doctype html>
<html>
<head>
<title>Game</title>
</head>
<body style="margin:0px;">
<canvas></canvas>
<script>
var c = document.querySelector('canvas');
c.width = window.innerWidth*0.98;
c.height = window.innerHeight*0.98;
var f = c.getContext('2d');
var keyList = [];
console.log(keyList);
window.onkeydown = (event)=>{
let key = event.which || event.keyCode;
alert(key);
}
window.onkeyup = (event)=>{
let key = event.which || event.keyCode;
alert(key);
}
</script>
<script>
class Player{
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.xv = 0;
this.yv = 0;
}
draw(ctx){
ctx.fillRect(this.x, this.y, this.w, this.h);
}
controls(keyList){
if(keyList[39]){
this.xv = 10;
}
if(keyList[37]){
this.xv = -10;
}
}
doKinematics(){
this.xpos += this.xv;
this.ypos += this.yv;
this.xv *= 0.9;
this.yv *= 0.9;
}
}
</script>
<script>
let bob = new Player(200, 200, 30, 30);
let gl = setInterval(loop, 1000/50);
function loop(){
f.clearRect(0,0,c.width, c.height);
bob.controls(keyList);
bob.doKinematics();
bob.draw();
}
</script>
</body>
</html>