├── LICENSE ├── README.md ├── TRexBot.js └── TRexBot.min.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Daniel Apushkinsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # T-Rex Game Bot 2 | A bot that plays the Google Chrome T-Rex game for you. It can consistantly hit around 50,000 points. Highest I've gotten is 100,000, but I'm sure it can go much higher. 3 | 4 | When I had started this project I thought this would be at least 200 lines of code, yet here I am hitting 6 figure scores where the bot itself is just 10 lines of code. Obviously I have to simulate the key inputs, and that's what takes up the other 50 lines. 5 | 6 | I've been able to make it so short by finding a formula to calculate how far away I need to jump before an obstacle. I found this by just playing the game and logging the exact speed of the T-rex and various other variables that the game uses. Putting this into any graphing software you could see a clear correlation between the speed of the T-Rex and the distance from the next obsticle (at the time of jumping). Distance would always be around 20 * speed. From there all that was left to do was type it up! 7 | 8 | 9 | ![HighScore](https://i.imgur.com/uAlZzuq.png) 10 | 11 | 12 | 13 | # How To Use 14 | 15 | 1. Open chrome and go to: chrome://dino/ (or disconnect your internet) 16 | 2. Press "F12" and go to "Console" 17 | 3. Copy and Pase the following text, then press enter. 18 | ```js 19 | function keyDown(e){Podium={};var n=document.createEvent("KeyboardEvent");Object.defineProperty(n,"keyCode",{get:function(){return this.keyCodeVal}}),n.initKeyboardEvent?n.initKeyboardEvent("keydown",!0,!0,document.defaultView,e,e,"","",!1,""):n.initKeyEvent("keydown",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),n.keyCodeVal=e,document.body.dispatchEvent(n)}function keyUp(e){Podium={};var n=document.createEvent("KeyboardEvent");Object.defineProperty(n,"keyCode",{get:function(){return this.keyCodeVal}}),n.initKeyboardEvent?n.initKeyboardEvent("keyup",!0,!0,document.defaultView,e,e,"","",!1,""):n.initKeyEvent("keyup",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),n.keyCodeVal=e,document.body.dispatchEvent(n)}setInterval(function(){Runner.instance_.horizon.obstacles.length>0&&(Runner.instance_.horizon.obstacles[0].xPos<20*Runner.instance_.currentSpeed-Runner.instance_.horizon.obstacles[0].width/2&&Runner.instance_.horizon.obstacles[0].yPos>75&&(keyUp(40),keyDown(38)),Runner.instance_.horizon.obstacles[0].xPos<20*Runner.instance_.currentSpeed-Runner.instance_.horizon.obstacles[0].width/2&&Runner.instance_.horizon.obstacles[0].yPos<=75&&keyDown(40))},5); 20 | ``` 21 | 4. Start the game! 22 | 23 | 24 | # License 25 | 26 | This project is licensed under the MIT License. See the [LICENSE.md](LICENSE.md) file for details 27 | 28 | Created August 2017 by Daniel Apushkinsky (Last Updated October 2020) 29 | -------------------------------------------------------------------------------- /TRexBot.js: -------------------------------------------------------------------------------- 1 | setInterval(function(){ 2 | if (Runner.instance_.horizon.obstacles.length > 0){ // if obsticles exist 3 | 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){ 4 | keyUp(40); 5 | keyDown(38); 6 | } 7 | 8 | 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) 9 | keyDown(40); 10 | } 11 | }, 5); 12 | 13 | 14 | function keyDown(codeKey){ 15 | // Simulate a key press 16 | 17 | Podium = {}; 18 | 19 | var oEvent = document.createEvent('KeyboardEvent'); 20 | 21 | Object.defineProperty(oEvent, 'keyCode', { 22 | get : function() { 23 | return this.keyCodeVal; 24 | } 25 | }); 26 | 27 | if (oEvent.initKeyboardEvent) { 28 | oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, codeKey, codeKey, "", "", false, ""); 29 | } else { 30 | oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, codeKey, 0); 31 | } 32 | 33 | oEvent.keyCodeVal = codeKey; 34 | 35 | document.body.dispatchEvent(oEvent); 36 | 37 | } 38 | 39 | 40 | function keyUp(codeKey) { 41 | // Similate a key up 42 | 43 | Podium = {}; 44 | 45 | var oEvent = document.createEvent('KeyboardEvent'); 46 | 47 | Object.defineProperty(oEvent, 'keyCode', { 48 | get : function() { 49 | return this.keyCodeVal; 50 | } 51 | }); 52 | 53 | if (oEvent.initKeyboardEvent) { 54 | oEvent.initKeyboardEvent("keyup", true, true, document.defaultView, codeKey, codeKey, "", "", false, ""); 55 | } else { 56 | oEvent.initKeyEvent("keyup", true, true, document.defaultView, false, false, false, false, codeKey, 0); 57 | } 58 | 59 | oEvent.keyCodeVal = codeKey; 60 | 61 | document.body.dispatchEvent(oEvent); 62 | } 63 | -------------------------------------------------------------------------------- /TRexBot.min.js: -------------------------------------------------------------------------------- 1 | function keyDown(e){Podium={};var n=document.createEvent("KeyboardEvent");Object.defineProperty(n,"keyCode",{get:function(){return this.keyCodeVal}}),n.initKeyboardEvent?n.initKeyboardEvent("keydown",!0,!0,document.defaultView,e,e,"","",!1,""):n.initKeyEvent("keydown",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),n.keyCodeVal=e,document.body.dispatchEvent(n)}function keyUp(e){Podium={};var n=document.createEvent("KeyboardEvent");Object.defineProperty(n,"keyCode",{get:function(){return this.keyCodeVal}}),n.initKeyboardEvent?n.initKeyboardEvent("keyup",!0,!0,document.defaultView,e,e,"","",!1,""):n.initKeyEvent("keyup",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),n.keyCodeVal=e,document.body.dispatchEvent(n)}setInterval(function(){Runner.instance_.horizon.obstacles.length>0&&(Runner.instance_.horizon.obstacles[0].xPos<20*Runner.instance_.currentSpeed-Runner.instance_.horizon.obstacles[0].width/2&&Runner.instance_.horizon.obstacles[0].yPos>75&&(keyUp(40),keyDown(38)),Runner.instance_.horizon.obstacles[0].xPos<20*Runner.instance_.currentSpeed-Runner.instance_.horizon.obstacles[0].width/2&&Runner.instance_.horizon.obstacles[0].yPos<=75&&keyDown(40))},5); 2 | --------------------------------------------------------------------------------