-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (112 loc) · 3.28 KB
/
script.js
File metadata and controls
151 lines (112 loc) · 3.28 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var ball = document.getElementById("ball");
ball.style.top = ball.offsetTop + "px";
ball.style.left = ball.offsetLeft + "px";
// If you want dynamic change in values of ball height and width uncomment the below 'onresize' code.
var ballHeight = ball.offsetHeight;
var ballWidth = ball.offsetWidth;
/*
window.onresize = function (e) {
ballHeight = ball.offsetHeight;
ballWidth = ball.offsetWidth;
};
*/
function setValue(value) {
return value + "px";
}
// When using event.keyCode;
function keyCode(keyPress) {
var top = parseInt(ball.style.top);
var left = parseInt(ball.style.left);
// W is pressed
if (keyPress === 119 || keyPress === 87) {
if (top > 5) {
ball.style.top = setValue(top - 5);
}
}
// A is pressed
else if (keyPress === 97 || keyPress === 65) {
if (left > 5) {
ball.style.left = setValue(left - 5);
}
}
// S is pressed
else if (keyPress === 115 || keyPress === 83) {
if (top < (window.innerHeight - ballHeight) - 5) {
ball.style.top = setValue(top + 5);
}
}
// D is pressed
else if (keyPress === 100 || keyPress === 68) {
if (left < (window.innerWidth - ballWidth) - 5) {
ball.style.left = setValue(left + 5);
}
}
};
/*
//When using event.key;
function key(keyPress) {
var top = parseInt(ball.style.top);
var left = parseInt(ball.style.left);
// W is pressed
if (keyPress === "w" || keyPress === "W") {
if (top > 5) {
ball.style.top = setValue(top - 5);
}
}
// A is pressed
else if (keyPress === "a" || keyPress === "A") {
if (left > 5) {
ball.style.left = setValue(left - 5);
}
}
// S is pressed
else if (keyPress === "s" || keyPress === "S") {
if (top < (window.innerHeight - ballHeight) - 5) {
ball.style.top = setValue(top + 5);
}
}
// D is pressed
else if (keyPress === "d" || keyPress === "D") {
if (left < (window.innerWidth - ballWidth) - 5) {
ball.style.left = setValue(left + 5);
}
}
};
*/
/*
//When using event.code;
function code(keyPress) {
console.log(keyPress);
var top = parseInt(ball.style.top);
var left = parseInt(ball.style.left);
// W is pressed
if (keyPress === "KeyW") {
if (top > 5) {
ball.style.top = setValue(top - 5);
}
}
// A is pressed
else if (keyPress === "KeyA") {
if (left > 5) {
ball.style.left = setValue(left - 5);
}
}
// S is pressed
else if (keyPress === "KeyS") {
if (top < (window.innerHeight - ballHeight) - 5) {
ball.style.top = setValue(top + 5);
}
}
// D is pressed
else if (keyPress === "KeyD") {
if (left < (window.innerWidth - ballWidth) - 5) {
ball.style.left = setValue(left + 5);
}
}
};
*/
window.addEventListener("keypress", function (event) {
// code(event.code); //- You can use this
// key(event.key); //- You can also use this
keyCode(event.keyCode);
});