How I make bots using python (educational)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll show you how i make a gamebot using python in this game you can control fleets of ships to attack enemy fleets let's say i needed to kill thousands of these fleets i obviously don't want to have to do it manually right so when making a gamebot i usually look at what i need to manually do and put it in a list first i have to select the fleet i want to use to attack to do this i can either click on it or press a number from 1 to 7 to select the respective fleet once a fleet is selected i have to find the fleet i want to attack select it and press attack the game has a nice feature which lets me tag a fleet and then press the find button to find it and select it let's go over the list first i select the fleet i want to use then i find and select the target i want to attack and finally i press attack in a new python file i'll start by importing the libraries i generally always use when making scripts these libraries have everything i need to be able to make the bot when i start the script i don't want the mouse to immediately start doing things so i'll add a time dot sleep 2 to give me 2 seconds to alt tab to the game i then write the click function which uses win32 api to move the mouse to position x y pushes down the left click holds it down for 0.1 seconds and then releases the left click so if i were to right click 200 500 this will run the click function and send the values 200 and 500 into x y respectively it will then use win32 api to set the cursor position to x and y which in this case would be 200 500 it then clicks down the left mouse button holds the click for 0.1 seconds and releases the left button i'll be using the click function to click elements in the game if i now look at the list of steps i had to do manually the first one is selecting the fleet i want to use as mentioned there are two ways to select the simpler one in this scenario is to simulate keyboard input since the fleet in the game is in slot number one i have to press the number 1 on the keyboard to select it to do this with python i simply write this this will press the key 1 hold it for 0.1 seconds and then release it the reason there's a sleep in between is because if i press it down and up instantly the game might flag me as a bot or simply not register the key press just to be safe i'll add a one second sleep after the key press to make sure the game can load properly now that the fleet is selected the next step is to find the target i want to attack i already have the target i want to attack tagged so the only thing i have to do here is make the script click on this find button i already created the click function at the start of the script so i need to find the coordinates of the find button so i can tell the function where to click to find the coordinates i open a new idle window by searching for idle inside of idle i write import pi auto gui and then pyro gy dot display mouse position after entering that the mouse's coordinates will be printed out every few seconds this part indicates the red green and blue values of the pixel i'm pointing to i move over the mouse to the find button and i can see that this is its location so now that i have the coordinates for the find button the only thing i need to do is click it again i'm going to add a time dot sleep 1 after the click to give the game some time to breathe now that the find button is pressed and the target is selected all i have to do is press the attack button if it's there but how can i detect if it's there you see the target in the center of the button is very red so i will check if the button is that shade of red and if it is the attack button is there to find the rgb values of the center of this button i'll do the same process as finding the position for the find button if i now hover over the reddest part of the button i can see the coordinates and the rgb values of that location since the button is red the only thing that really interests me is this red value now that i have the red value i want i can use payout gy's get pixel function like this this will check if the pixel's red value in this position is the same as the one the target has the little zero in the bracket here simply means the red value one would be green and two would be blue if this becomes true then the target is visible which means i can just click the attack button with a click function like this the location of the click can be the exact same position where i check the color for the red value since it's all part of the button if i now test from the script you will see that the script correctly presses 1 to select the fleet clicks on find and attacks the target but the script still has a ton of flaws the first one is that the script doesn't check if the fleet is currently in combat and will constantly be searching for new targets which it doesn't necessarily need to do and will put a lot of strain on your cpu to be able to tell if a fleet is in combat the only thing i really need to do is check if the join button is visible when selecting the fleet all i have to do then is check if any part of this join button is red in this case i'll check if this middle circle is red once again i'll open a new idle window write import powder gui and pyro gy the display mouse position i'll hover the mouse over the center red part of the join button these are the coordinates of the join button and these are the red green and blue values of that location i know that the join button only appears after the fleet is selected so i want to put the is in combat part of the code right below the part that selects the fleet so i'll simply enter this this means that if the pixel in this position has this red color then it's going to run the if statement but if you remember what i'm trying to do i don't really care if the fleet is in combat i only care about when the fleet is not in combat in other words not doing anything so instead of having this equal equal sign here i will change it to a not equal sandwich looks like this this will only return true if the fleet is not in combat which is exactly what i want now i simply have to intend this part of the code which finds and attacks the target inside the if statement so it will only run if the statement is true the second flaw of the script is it only runs once and i want to kill thousands of these ships so the solution to that is putting this entire part of the script inside a while loop to make it run infinitely now in theory i could put the code inside a while one loop but if i'm having the script moves the mouse around then closing it may become a bit difficult that's why i always like using this what this does is it checks if the q key is currently being pressed and if it's not it will infinitely play the script below i use it so i can stop the script simply by pressing q one of the issues this script has is it doesn't give the fleet enough time to move to the target it's trying to attack to be able to give the fleet enough time to travel i will add a 15 second sleep after the attack button is pressed and now if i run the code you can see through the sped up footage that everything works the script can be improved even more if you're enjoying the video so far a lag would be amazing the fleets i'm currently farming can't actually damage or hurt my fleet at all but what if i wanted to attack something that can actually damage my fleet the game has a feature where if it takes less than 5 minutes of repair damage i can instantly repair the entire fleet with a single button if i have the option to repair my fleet for free i obviously want to do it there are two ways of doing this the first would be to check if a single pixel in the area where the free repair button is supposed to be has a yellowish color but that would mean that the script would always click on the repair button even if it's not free as long as it's yellow and i don't really want it to some kind of broke if you watched the body tutorial i made quite a while ago you will remember the part where i check if an image is present on the screen or not and that's exactly what i'm going to do here if the repair is free the button will always look like this and if it's not free it will have the amount of coins displayed here i am first going to take a screenshot of the screen where the instant repair button is visible then i open a microsoft paint window paste the image select what i need which is the button crop out unnecessary details and save it in the same location as this python file as repair.png instead of taking a screenshot of the entire screen i could also use the shortcut shift windows s and then drag a box around the button then paste it in paint crop it and save it i'm going to use pyro jy's locate on screen function like this to check if the image is present on the screen this line will look for the image repair.png on the entire screen and it will return the location if it's found i use grayscale equals true to turn the repair.png into a black and white image and then try to find it in a black and white version of the screen making it black and white makes the script run a little bit faster so i generally always use it the confidence part here just tells the function how sure it has to be to have found the image for it to activate so 0.8 would return a location if the function is 80 sure or more that the image is on the screen in this loop i check if the output is not none since this function returns none if the image is not on the screen which means that returning not none means the image has not been found if the image has been found then it means that free repair is available so all i have to do is click on the location of the free repair button by finding its xy coordinate i will again use locate on screen to find the button's coordinates i'll add the click function inside of this if statement and enter the coordinates of the repair button and once again i'll give the same a couple of seconds to load just in case the script is still not perfect and can still be upgraded a lot i'll get to that in a second but first i'll run the script and check that everything works properly line by line when i run the script it waits two seconds then it creates the click function the script checks if the q key is pressed which i am not currently pressing the one key is pressed down and released there's a one second pause the script will try to find the repair.png image the image has not been found so the script will not click on the place where the repair button would be there's another 1 second pause this pixel is checked to see if it's red which would indicate the fleet is in combat the pixel is not red which means the fleet is not in combat which means that this statement is true and this part will run the find button is clicked there's a one second sleep the script checks if the red attack button is visible by checking if this pixel is red the pixel is red so it gets clicked the script sleeps for 15 seconds giving the ship time to travel and attack the target before looping again if i run the script against a target that can actually damage my fleet you will see that it detects the instant repair button and clicks it i damaged this fleet so it's no longer free repair and as you can see the script will ignore the button and move on without repairing now i did mention that the script could be improved a lot the first improvement is fixing a small issue with the repair system let's say i press the number one that will select the fleet number one and show you if the fleet is repairable but if i were to press the number one again the repair panel will close to make sure that the repair panel is always visible no matter what i have to first select a random fleet then select the fleet number one to do this i'll copy the code that selects the fleet number one paste it right under the while loop so it runs before the code that selects the fleet number one and then change the number from one to let's say two an important part when making scripts like this is making it as undetectable as possible so that anti-cheats and locks can't make it look like a program and if i were to bot like this in a game that logs user inputs it would be obvious that it's a bot by seeing that all of the clicks are exactly the same length the delays between selecting fleets is always the same and so on so what i'm going to do now is make the delays random so they can't detect it looking at the click function there's a hard-coded delay of 0.1 seconds i want to make this delay a random number between 0.1 and 0.3 to be able to generate a random number between an x and a y value you can use this so i'm going to change the delay here to time.sleep np.random.uniform 0.1 0.3 to pause anywhere between 0.1 and 0.3 seconds i added random sleep values to the entire script and now it looks like this this code is now perfect for single fleet farming but what if i wanted to farm with multiple fleets at the same time i could obviously copy the entire code and paste it then change the part that selects which fleet will be used to attack doing this for all seven fleets would look extremely ugly and there's a better way of doing it so the thing i want to do now is clean up the code using functions looking at the code that loops infinitely it can be divided into these different parts selecting the fleet i want to use repairing the fleet if necessary checking if the fleet is in combat selecting a target attacking the target i can actually put selecting a target and attacking a target into the same group that leaves us with a total of four groups i'll start creating the functions right below the click function but in theory they can be anywhere above the while loop starting from the first of the groups which is selecting the fleet i want to use i'll create a new function which i'll call select fleet like this this creates a function called select fleet with x as a passable variable to explain what this x does it allows me to send a value into the function sort of how we send coordinates to the click function so if i were to write print x inside of that function and then call the function with select fleet jeff the value jeff will be sent into the function select fleet stored inside of x and then print it out now that you know the basics of how a function works i'll copy the portion of the code that selects a fleet including the delay here and paste it inside of the select fleet function i'll ignore the part that selects the random fleet and look at this part which selects the fleet we're interested in what i want to do for example is call the function select fleet 2 to select the fleet number 2. since we already know how to pass a value to a function and store it in a variable like this x all i have to do is change the 1 in here to the x where the number of the fleet i want to select is stored if i now test the function with select fleet 1 the script is then going to send the value of the number 1 into the x it will press the key to release it wait for a bit and then give out this error looking at how the key down and key up usually work you can see that we have tiny quotation mark brackets surrounding the number of the key that has to be pressed this is because the input of the key up and key down function has to be text or a string what this select flick 1 does is it sends an int type variable 1 which is a number and not text there are two ways of solving this the first one would be sending in the 1 as text instead of number by stranding it with brackets the second one which is a bit more complicated is to convert the x into text if it's not text to do this instead of the select fleet function i will write this the is instance x int will return true if x is of the type integer so a number if it is a number i want to turn it into a string text which can be achieved with this line x will then be equal to the string value of x which is the output of str of x that means that if x was for example 5 this will return true and x will be turned into a string now x is always going to be a string even if we send in a number and you can see that it no longer gives an error if you try to give it an integer when selecting a fleet if the same number is pressed twice regardless of timing the information window where the free repair button would be will close so if i were to call select fleet 2 the function would first press 2 then it would press 2 again which is not what i want since it would close the repair menu so what i always want to do is select a random fleet other than the one i want to select since i'm using the fleet 2 as a random fleet the only time when the same number is going to be pressed twice is when i want to select the fleet number two to fix this issue before selecting the random fleet number two i will check if x is equal to two so if the number value of x just in case a string is sent in is equal to two i'll select a random fleet three if the fleet i want to select is not the fleet number two then i'll add an else statement that selects fleet two as the random fleet if i test it with the fleet number two the script will run the if statement which will press three since x is equal to two moving on to the repair function i'll create a function the same way as i made the select leak function this time i choose to name it repair if free and i don't need to pass a variable to this function since the fleet i'm interested in repairing has already been selected so all i have to do in this function is copy paste the part of the code that i already wrote that repairs the fleet when the free repair button is visible including the delay at the end now that the repair function is complete i have to think about when i want to call it i know that a fleet has to be selected before checking if the repairs are available which is why i'm going to call the repair a free function at the end of the fleet selection function the third group i have to turn into a function is that check if fleet is in combat once again i'll create a combat check function which does not require an input since a fleet will already be selected i'll paste in the code that checks if the fleet is in combat which is just a single if statement this statement will be true if the fleet is not in combat which means i want to find a new target and attack it if it's true so i'll create a new function which i'll call find and attack and call it in the combat checks if statement inside the find an attack function i'll just copy paste the final part of the code which finds the target and clicks on the attack button a tiny problem i forgot to mention is that sometimes fleets don't spawn very fast and if i try to find a fleet that hasn't spawned i will get this error message after a few seconds that tell me the target has not been found but how can i detect if a target has not been found if the target cannot be found then this if statement will not find the attack button which means i can just add an else statement to wait until the ok button is visible so then click it so around 8 seconds to be extra safe and then after finally waiting for the ok button getting its location and calling a click function to click on it the combat check function still doesn't have anything that calls it but i want to call it after checking if the free repair is available which means i'll call it at the end of the free repair function now that all the functions are complete i can test the script by running the select fleet 1 function which will in order select the fleet by pressing 2 wait for a bit press x which is 1 calling the repair function checking if the repair button is visible on the screen which it is not calling the combat check function checking if it's in combat which it is not so it calls the find and attack function it presses find waits for a bit checks for the attack button if it's there which it is clicks on the attack button and it waits so that the fleet can reach the target now that the script works perfectly for one fleet let's say i want to use fleets 1 2 and 4. all i have to do is call the select flick one two and four inside of the while loop and there you go that's how i'd make a bot for this game thank you for watching
Info
Channel: Kian Brose
Views: 68,207
Rating: 4.9690752 out of 5
Keywords: bot, bots, botting, automation, automating, tutorial, how, to, demonstration, how to, kian, brose, kian brose, fast, pace, how i make bots, how i make, how i, make bots, how to make bots, python tutorial, python bot, python bot tutorial, python chat bot, python chatbot
Id: wCho_BdpdyY
Channel Id: undefined
Length: 17min 11sec (1031 seconds)
Published: Thu Nov 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.