-
Notifications
You must be signed in to change notification settings - Fork 21
/
TRexBot.js
62 lines (44 loc) · 1.75 KB
/
TRexBot.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
setInterval(function(){
if (Runner.instance_.horizon.obstacles.length > 0){ // if obsticles exist
if (Runner.instance_.horizon.obstacles[0].xPos < Runner.instance_.currentSpeed * 20 - Runner.instance_.horizon.obstacles[0].width/2 && Runner.instance_.horizon.obstacles[0].yPos > 75){
keyUp(40);
keyDown(38);
}
if (Runner.instance_.horizon.obstacles[0].xPos < Runner.instance_.currentSpeed * 20 - Runner.instance_.horizon.obstacles[0].width/2 && Runner.instance_.horizon.obstacles[0].yPos <= 75)
keyDown(40);
}
}, 5);
function keyDown(codeKey){
// Simulate a key press
Podium = {};
var oEvent = document.createEvent('KeyboardEvent');
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, codeKey, codeKey, "", "", false, "");
} else {
oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, codeKey, 0);
}
oEvent.keyCodeVal = codeKey;
document.body.dispatchEvent(oEvent);
}
function keyUp(codeKey) {
// Similate a key up
Podium = {};
var oEvent = document.createEvent('KeyboardEvent');
Object.defineProperty(oEvent, 'keyCode', {
get : function() {
return this.keyCodeVal;
}
});
if (oEvent.initKeyboardEvent) {
oEvent.initKeyboardEvent("keyup", true, true, document.defaultView, codeKey, codeKey, "", "", false, "");
} else {
oEvent.initKeyEvent("keyup", true, true, document.defaultView, false, false, false, false, codeKey, 0);
}
oEvent.keyCodeVal = codeKey;
document.body.dispatchEvent(oEvent);
}