Creating a Minecraft Mining Bot in Python 3.9 Tutorial (Fast & Easy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys in this lesson we're going to be looking at how we can create a minecraft bots in python it's going to be able to mine diamonds and it's going to be able to avoid lava once it runs into that alright so the first thing we should do is go ahead and open up minecraft because here we need to collect a few resources for our program and the first thing we need to do is take a screenshot of our options menu so i'm just going to do command shift plus three and this is going to be different on windows just find a way to snapshot the screen then we also need to take a snapshot of the lava because we're going to be using something called opencv to actually recognize that there is lava so it's going to scan the screen and it's going to locate the lava and it's going to make sure that our character runs backwards as soon as it notices the lava because of course we do not want to jump into it so once again i'm going to take a snapshot of the whole screen so command shift plus three and i'm going to pause minecraft for now because we will not be using it until we finish the program but what's important to note is we took some screenshots and here i have the options menu and here i have the one of the game so first we're going to open up the one of the options menu on full screen and we're just going to take another screenshot of this part right here to translate it because right now it's in italian we want to get the resume the game button so take a screenshot of that and then you can safely delete this options menu and we're going to call this one start underscore game so it should look something like this as you can see it's the button that starts the game then let's go ahead and open the second screenshot which contains the lava because we want to screenshot the lava and i found this to be the easiest way to take these screenshots because in minecraft of course you have a cursor and it's very hard to move the cursor when you're in the game so taking a screenshot of the screenshot will be perfect so just go ahead and try to get the lava as close as possible like this now we can close this delete the screenshots we're going to move the lava next to the start game and we're just going to name it lava so the next thing we have to do is go ahead and open up pycharm or whatever editor you have for code and i'm using python 3.9 for this but 3.8 and earlier should be fine but we need to open up a new project and of course we have our main.pi but we're going to go ahead and right click on our project folder create a new directory and it's going to be called images then we're going to go ahead and drag the two images we took from earlier because we will be using these screenshots for recognition so as you can see we have that one from earlier and we have the lava image up next we have to go ahead and open up terminal because we need to install a few packages the first package is going to be open cv so pip install open cv dash python and then tap on enter then we should go ahead and install a package called pillow and this is used for handling images so it's important we install pillow and finally we should go ahead and type in pip install and we should insert pi auto gui which will help us with simulating key presses and right after that we can go ahead and close the terminal and type in import pi auto gui as pt and then from time we are going to import sleep and the first thing inside here we're going to do is create a helper function so we're going to add a comment that says helper function and we're going to create a function called nav to image and this is going to help us with navigating to any image we provide to the program so first we need an image path followed by the amount of clicks we want to perform on that image and we're also going to provide an offset of x which will be set to zero and an offset of y which will also be set to zero initially and those are just used in case we want to change the position of the mouse just by a few pixels or by a lot it's good to have that for later next we need to go ahead and create a position which is going to equal dot locate on center screen and we want to locate the image and we are going to give it a confidence level of 0.7 so as long as it is 70 resembling the pixels we have provided it's going to find it on the screen if you leave this at 100 it's going to be very hard to find that pixel perfect image and you'll probably never find it if the tolerance is too low then you're probably going to recognize everything as the image that you're searching for so it's not very accurate so it's important you give it some leeway between 0.6 and 0.9 up next we need to define what happens if it cannot find the image so we have to go ahead and type in if the position is none which means it couldn't find the image we defined now we're going to print a simple log statement that says that the image we inserted was not found and we're going to return zero as an exit code else we're going to go ahead and type in pt move 2 which will move to a position and we have the position up there as soon as we locate the image and we will add a duration which will be set to 0.1 seconds so that's how fast the mouse cursor moves to the location we need it to go to then in case we also want to adjust it we can go ahead and type in move relatively and we're going to insert the offset x and the offset y so these are optional in case you wanted to move a few more pixels to the right or a few more pixels down or up you can provide that by adding the offset up here now we're going to set the duration to 0.1 as well next we also want to define a click mechanism so we're going to set the clicks to the amount of clicks that we define and we want to put an interval between each click which is going to be set to 0.3 seconds so that's going to cover the helper function right below that i'm going to add these three comments and that is because we're going to create a function that moves the character and defines how we can place blocks and delete blocks so as you may have noticed i provided x as the key for attacking in minecraft and c as the key for placing a block and by default your left click is the attack and the right click is the placement so inside your settings in minecraft you're going to have to go ahead and change the key bindings to map the attack to a different key such as x and to map the placement key to something such as c and those are the ones i picked of course you can pick any letters you want just find ones that are comfortable so that we can use them later but right below that we're going to type in def move underscore character and that's going to take first a key press which key we want to use a duration for how long we want to hold that key down and the action which by default will be set to walking and the first thing we should do is go ahead and type in pt key down and inside here we just have to insert the key press next we need to define what happens if an action is called so if the action is equal to walking which is the default of course then we're going to go ahead and print walking l if the action is equal to attack then we just have to go ahead and type in pt key down and we want to specify x because that is the attack key next we need to define a delay so that we have some time in between the key down and this one's going to be set to the duration and after the sleep function we can go ahead and type in bt key up and say that we want to lift x and we also need to go ahead and type in pt key up for whatever key press we have defined so this is going to take care of moving our character right below this we're going to go ahead and create a function called locate lava so we can make sure that we avoid that later and it's going to first take a position which is going to equal pt locate center on screen and now we can go to our project open up the images folder right click on the lava and we're going to copy the path and we're only going to take the path from the content root and insert it as a string over there so this is the lava image we had and since lava can have many different angles and it can have many different shapes we're going to have to set the confidence level to something extremely low such as 0.3 or 0.4 but we will just insert 0.3 for now which means maybe it might even confuse it with a torch but it's better to be safe than sorry but we also need to check if the position is none it means there is no lava so we can just go ahead and return false else we want to make sure that we can move the character backwards to avoid being burnt to death so here we can go ahead and type in move the character and the key press we want to specify is s and we will move back for two seconds and we are also going to print to the console that we found lava and we can return true because we did locate some lava now all we have to do is glue all of this together so the first thing we have to do is start the game so go ahead and type in start the game and to do this we'll go ahead and type in nav to image and we need to insert the image for start game so go ahead and right click there copy path get from the content roots and insert it inside there we're going to ask it to click three times just to make sure it starts the game and we can actually go ahead and test this immediately just remember to insert a delay such as three seconds before you start the program because pi auto gui only works on the main screen and in general you have a dual monitor so often what i do is work on my second monitor on the code and run all of the tests on my main monitor which is the main screen of my macbook so in case you don't have a dual monitor you're going to have to provide a sleep so it gives the program some time to wait before we go to the main screen all right so now i reopened minecraft so we can go ahead and test it the goal for this right now is for the mouse pointer to go to this button here and click on it so let's go ahead and right click on our main program click on run and quickly switch the screen to the game and as you can see the mouse pointer moved incredibly fast and started the game for us so that's the first step in making this work now let's go ahead and finish the program so here we're going to create a variable called duration and i'm just going to set it to 10. this is for how long you want your bot to continue running 10 does not necessarily translate 2 seconds but it does translate to a time frame that's good for yourself so while the duration is not equal to zero and the first thing we have to do is check if there is no lava then we're going to continue mining so we're going to check if not locate lava because it returns a boolean so it's checking if there is not lava in other words then we can go ahead and move the character forwards by holding down w of course because w is the move is the move forward key and we're only going to set this for two seconds but of course you should adjust this based on your pickaxe and we're going to go ahead and specify the action as attack because that's what we specified the options to be over here else if it does happen to find some lava we want to break out of this loop immediately because we happen to find lava and we do not want to continue mining the lava of course and it's also very important each time we go through this loop we decrement the duration by one so that the program can end after 10 times now we can also go ahead and print the time remaining because it's always fun to log stuff and we can add the duration but with that being done let's go ahead and test the program and to do that we're going to have to rebuild what i had earlier so we're going to place a block there now we can put some test blocks as you can see i inserted some diamond there then we need to switch back to game mode slash survival and you're going to have to angle your pickaxe slightly down so that we can mine two blocks at the same time so kind of like this we're going to go back and we're going to start the program by right clicking on main and going back to our screen and of course it has the auto start feature so it's going to find the button and then it's going to start moving so as you can see we moved backwards this time for the simple reason that the program thoughts we found lava because we put this confidence level to 0.3 which is incredibly low it even thought that the torch was lava so let's go ahead and set that up to 0.4 and we're going to try again so now we can go ahead and right click and restart the program and as you can see as soon as it saw the lava at 40 percent recognition it decided to back off and end the program if we go ahead and check the logs you're going to notice it happened to take two steps it was walking and then it found the lava and the program ended so of course we can also do this in a different direction if we wanted to we can go back we can rerun this program and we can just go back to our main screen this point onward it's going to mine as long as it can or as long as you set the duration to right there it's it thought it saw some lava which is absolutely wrong so we might have to go ahead and set the confidence level to 0.5 and as you can see there's a lot to be done with this program it is in its very basic form but let's go test it one more time we'll go here and the first thing to do of course is test whether it can recognize this lava so to do it we'll go ahead and run the program change over here it will start the program and it's going to back off immediately because it found the lava let's run it one more time to make sure it actually works and let's set the pickaxe of course otherwise it's going to be difficult and that is perfect but anyways guys that's actually all i wanted to show you in this tutorial and maybe in a future tutorial i'll show you how we can move the mouse pointer and do some more complicated tasks but just to get started i thought this was a great project and of course if you have any comments or questions feel free to leave them in the comment section down below and i'll do my best to answer them otherwise with that being said have a wonderful week and i will see you in the next lesson
Info
Channel: Code Palace
Views: 24,642
Rating: undefined out of 5
Keywords: code palace, cde palace, code palce, palace code
Id: beTP2HzR1xg
Channel Id: undefined
Length: 15min 53sec (953 seconds)
Published: Wed Aug 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.