Hello welcome to video four um where we’re making some kind of minecraft version um in python in the in the python language using the cena um game engine and no we’re in tutorial four now so i’ve just copied my um code from tutorial three and so we’ve got exactly we’re at Exactly the same point as in the last video and i’m recording this in a cafe in the same day because i didn’t get to these two items here we need to change the colors um of our terrain um with a bit more a bit more variety and sophistication we want to use some Random numbers in their random values and then of course get to our mining and building and things so let’s go down to the generate subset function and that’s where we were applying the colors so red green and blue components to our sub cube color and then i got i didn’t really talk About this but i got the the textures working correctly by turning off the texture or commenting out the texture of our subset here and that meant for some reason i don’t fully understand we weren’t overriding the texture or the colors of our um sub cubes so they look the way that We want them to look i.e we can apply a color now and then when we finish the terrain we can apply um the grass stroke texture in fact i’ve i forgot to comment that out in the last video i kind of moved it to here i bet if i comment this one out Let’s comment out the first one i bet now uh this breaks it so the colors won’t appear correctly but then i guess we’ve learned something about the way that the um uh the texture functions are working right so what we want to do here was Here we wanted to add a little bit of randomness so that we’re not getting like straight lines or bands of color um so what we can do is let’s say if y um let’s say oh yeah our map function what we’ll do what we’ll do is we’ll add a random number To our our y value that won’t upset the purlin terrain noise it won’t like change the height of the block itself because the block itself its value is being set up here and we’re just storing that value in this other variable called y purely so that we can Um change the colors of the sub cube so we’re going to say y plus equals a random number so i want to use the rand range function and i want to add between minus four and four let’s say so there’s a bit of variation um actually i might change it No okay i’ll stick to my first decisions stick to four and then i can use the round range i think because have i already imported yeah from random import rand range i’d either done that before for some other reason i can’t remember we used any randomness or my text editor here automatically Imports things sometimes when i try to use like inbuilt functions so you don’t have to install random that’s part of the standard library for for python um just like i think numpy and math and um like multiple pro what’s it called um multi-processing that’s already part of the standard library as well Uh purlin noise isn’t oh time is so you don’t have to install time you do have to install perlin noise like we did in the first video right um let’s run this so we’ve got a terrain of 20 so it should be a little faster to test i’ve got coffee because i’m in The coffee shop when you’re at home drink tea when you’re in a coffee shop you have to drink coffee so our terrain is yeah looking different now and i don’t know if you just saw that when it combined then it’s applied the uh the terrain color finally But there we go now we’ve got some variety which looks really good i think it looks more like minecraft for example um what would be better is where we’ve just added a random range is to use some more purlin noise so that every time we generate this terrain using particular seed it would Look exactly the same and because we’ve introduced some randomness now it’s not always going to look this way um we’ve still got our giant goose looking at us right the next thing let’s just let’s hurry through um actually i did want to try something else just very quickly For the um the terrain color i haven’t done yet myself but what we could do is make them blue i guess if we’re not looking at the height but if they’re like halfway across the terrain or something like that so firstly i want to make my terrain a little bit bigger Let’s double it like that and then i want to kind of override all of this um or do i let’s okay let’s let’s try and make like a red zone um yeah okay so we’ll say i’ll make myself a note down here so all of this kind of makes some sense red zone Are we in the red zone so we’re gonna say if um zed is greater than the sub width or terrain with terrain width um divided by half or times half um then we’ll say red it’s like green equals zero blue equals zero but red now equals and we use our map function again So basically we’re going to say if y uh from between its height being at zero up to half the amplitude we can now go from actually i’ll stretch this right up to the amplitude we’ll go from 110 to 255 so i’ll have a nice red zone this is underlining for some reason Expected independ i think that’s fine have we got terrain width in here no we need to maybe set this as a global variable as well just so that this function is using the global variable right oh i see what’s going on sorry this is bad wasn’t it So i’ve got i forgot about this else here and then you can’t have immediately after that a um a comment also this green setting belonged there there we go and i’ll kind of keep all of this together because it’s all to do with the the color of the sub cubes And the visibility of them right um oh yeah and the other thing was i commented this one out so let’s keep that one there let’s comment this one out and just see what happens in terms of when the the texture is applied have some more coffee interesting so that didn’t make a Difference in terms of the there being no texture to begin with while we’re generating our terrain when it’s finished hopefully it will be um it will actually be applied that texture now we’ve gone over to the red zone i’m saying it’s too brown i thought i’d made a mistake which i Couldn’t understand but it was just it’s just a fog i think it’s kind of a blue color yes and so we’ve got our we’ve got our texture applied so that’s okay let’s jump to the top of the red mountain right so now we’ve got like different areas of Our terrain different colors which is like the beginning the simplest step of getting different um biomes i guess there we go this is looking a lot more interesting in terms of variety okay happy with that and we’ve got wow we just had 70 70 frames per second so this is working Uh very nice very nicely right let’s just save our code there and i’m just going to pause the video not that you’ll notice just so i i think about the next thing which is doing building and mining okay welcome back right so all i did there actually just to save some of your Time was i’ve put inside of our assets so inside of our folder where we’ve got um in my case that you took the tutorial files the python files the modules and i’m currently in tutorial four i’ve also got an assets folder where just to organize things a little bit i’ve got a Minecraft chicken nothing’s i can’t remember what i put in there what did i put in there oh i remember what that was anyway i’ve got the the textures the grass texture the monochrome grass which doesn’t have a border and then stroke mono which is a monochrome General texture like this one but with a white borderline so you can see like each individual cube sometimes looks good and then what i just added which will be useful for building is this wireframe block and all it is is a png so we’ve got transparencies you won’t have transparencies if you Save it as on export it as a jpeg file or a bitmap all it is is just that border so it’s just a white border i’ve made it white so that when this is textured onto a cube it’ll look like a wireframe cube and because the center is see-through transparent It’s just like a wireframe so we can like look through that cube and that might be like a nice tool to project in front of our player and then we can instantiate or create a block in that position so it’s like a 3d cursor for building and things so that’s maybe What we’ll use later on right so that’s all there in the assets and again you can download all of these from mine or you can make them yourself the link will be in the description okay so first thing we want to do yeah is let’s go and project that wireframe cube In front of our player so in the update loop in the update function here what we want to do and do after everything else we want to kind of like we’ll call it project um a builder block uh let’s call that build tool there we go so we want to just go and Run this function project build tool now this function doesn’t exist yet so let’s go make it up here i’m just thinking now that our code is getting quite long really when we’re like generate we’ve got a load of functions that are generating the terrain got some functions that are moving the player We’ve got some other functions that are doing other things these should all be in their separate python files as modules and like we should have like a main game file with the update loop and we should import those other files those modules in like i did with the the map function in The last tutorial um but i won’t do that yet maybe i’ll tidy it up later on but it just means also we won’t have to scroll through everything my code is fairly organized at the moment anyway let’s make this projectile function just up here so this is at the End of like the setup stuff at the beginning and before the input function so you want to define um project or let’s just call it build tool that’s a better name isn’t it there we go build tool build tool i always feel bad when i change the names because i’m imagining You’re kind of like coding along with me then i keep changing things but i don’t know you can call them what you like all right and so what we want to do we want to set what should we call out we’ll call our build tool entity a bte build tool entity And we want to set its position to just in front of our player so it kind of wants to be the subject’s position plus plus maybe two blocks ahead um so let’s try do we have a subject forward yeah i think we do so what that property is Is just which direction is our player following uh facing sorry but we want to multiply that by two blocks so if we didn’t multiply by two it just be immediately in front of our player our subject now our bte doesn’t exist yet so we need to create that Let’s just create it next to our uh definition for our build build tool function so our bte wants to be an oceana entity its model wants to be a cube and its texture wants to be we’ll call this uh wire text so we’re in the right place because this is Just above there is where we’re loading in our textures so we’ll create that we’ll create that texture now y text equals load underscore texture and then what i was talking about at the beginning of the video i think it’s called wireframe.png obviously if you’ve called your texture a different name You’ll have to name it differently there and so the transparency will work automatically there and it’s going to be a cube a unit cube so that just means one by one by one on the x y and z axes and it won’t have a collider on we Don’t want it to be hitting the terrain or anything like that um is that all we need to say for it yeah i think so so it exists we can see it um let’s just see what’s happening now so it should be in front of us all the time Already because we’re we’re calling this function in the the update um in the update function what’s wrong with it build tool is not defined it is defined it’s fine it’s there look maybe i’ve done something wrong anyway we’ll run this and see what happens oh build tool isn’t defined What have i done wrong what’s gone wrong here it’s either something i’ve written incorrectly within the function itself or have i got some incorrect indentation oh must be capitalization that was my fault that was karma because i uh changed the name didn’t i at the last minute Right out we can see it it’s already working now is it working correctly well it is moving in front of us it’s not getting higher or lower um also it’s way too precise it’s like it’s not fitting on on each of the like the terrain cube spaces so what we haven’t done yet But we can easily do is wrap that position in a mathematical floor function like we’ve done for the whole terrain and actually we don’t want to project it in front of the player the subject we want to project in front of our camera so we want to replace the subject’s position with the The camera position and then perhaps we maybe want to move upwards i don’t know that’s up to you you could play around with that but let’s go and make those changes so at least it’s beginning to work right so we’ve got subject position here so we don’t maybe we can keep the Subject’s position but then we want to say subjects or can we just go straight to the camera can we say camera forward times two oh and then we want to wrap this whole thing in a floor function so we want to floor that entire thing and now that we’ve got parentheses there Actually i can just go on to another line just so that you can see it a bit more easily it doesn’t go across here so i can only do that remember because i’ve got in python because i’ve got the uh in parentheses right and then oh yeah i’m saying we could Just lift the position up a little bit so we could say uh pte your y value now could you go uh minus equals uh two blocks so it goes up by two blocks and we don’t have to floor this i don’t think because it’s already flawed here and we’re only subtracting An integer value from it anyway so it is flawed in that sense it is rounded to a whole number an integer was there anything else i wanted to do i can’t remember let’s just see if that works this one gets i’ll get some more coffee okay So this is not an improvement because i can now i now can’t see it where has it gone so i’ve done something wrong my guess i don’t know if we get any um or problems end up could not be resolved that’s fine um maybe i’m guessing the camera wasn’t working correctly Just i’m scrolling upwards here no it’s not flagged up any problems let’s oh i’ll cheat just to save your time let me look at my prepared code so i called it position subject position well i’ve done the same thing i’ve created a new vector here the camera forward Let’s just copy this and what have i done oh i think the main problem is this i’ve lowered it minus two i’m thinking what am i think i’m thinking in 2d where uh subtracting a number moves things up in 3d um subtracting a number moves them down so hopefully that was the problem Because i know i wrote the uh projection of the camera position slightly differently with that vector three things but um as far as i could understand it was the same thing yeah so now this is working it’s projecting in front of where we’re looking so if you want to build Now a cube slightly higher you can look up and it moves up what is wrong however is that it let me just pause the video a bit of background one second hello again so i just um start up the uh the simulation um oh yeah what i was just saying was Um yeah our cube is kind of in front of us but it’s not exactly in front of us it’s kind of like to the left sometimes to the right which is very off-putting so i think what that is is that we are using floor here to To get our whole number so it is positioned on the grid exactly but maybe we want to use a round function so if it’s like 0.5 or 0.6.7 it will be more accurate to where we’re facing now i don’t know why i’m able to use rounds straight away I think i’ve got to import oh is it because i’ve got well i’ve imported from numpy floor and absolute functions do i have to well has python got in built a round function will it flag up an error oh okay it’s got one in bill ah I love it when it works correctly so it’s it’s projecting exactly in front of us on the x and the one oh but now actually it’s not i don’t think it’s lining up precisely where we needed to is that because i now need to apply the round function to um to the y As well let’s just say b t e dot y um equals b t e dot y uh but rounded sorry bte not bty that feels better but now it feels like um now it feels like it’s not exactly on because i can i can kind of shift it around that Shouldn’t be happening should it so what’s going on what’s going on here in my let me see my prepared code so i’m rounding each component of the of the of the vector so let’s do that let’s uh round so i’ve round the y component let’s round the x component and around The z component i’ve got to uh change these as well i don’t remember doing this i must have had the same problem before my prepare code now that feels like it snaps to the correct grid position exactly yes oh that’s really satisfying isn’t it it’s sitting exactly where it needs to be Brilliant so i probably discovered the same solution as i did in the repair code so if you want to be able to build maybe a bit further away all you need to do is change this value here because that’s multiplying as forward according to that that vector that 3d vector Um or closer would be one so i’ll keep that at two actually i’ll i’ll go to three i know and see how that looks so final thing that we’ll do is um be able to build and delete things um so so what we want to do in input I want to say if key is um now what do i write do i write mouse button right no i want left mouse button it’s the other way around minecraft isn’t it to build you do the right mouse button okay so if it’s the left mouse button Then what we want to do is to say yeah but we just want to go build build just go and build so i need to write another function to do that i’ll write that underneath the build tool so define build and what we want to do there is We could kind of duplicate i think this is where i did my prepare code we could duplicate the bte our what’s that build tool entity um and then just change its texture give it a collider so we can walk into it um and something else that i’ve forgotten let’s have a look So we can say e equals um mt squared now equals um um bte duplicate so that’s an acena built-in function and that should return an entity and we’re going to store that entity in e that’s just a temporary variable to hold our object So then what we can do is e you can say collider equals a cube collider um what else did i just say oh yeah and it needs at the moment it’s going to look like the wireframe texture so we don’t want to do that we want to say e um texture equals um Right we want the we want the mono we want this other texture actually we want it’s going to my assets folder we want this grass mono texture because it’s monochrome i.e gray we can again shine any color we want through it to get different types of blocks so red for Some kind of redstone or ruby or something and then green for grass etc and we just leave it gray for stone let’s say that’ll be the default one so let’s go and make that texture first so um we’ll call it stone text stone text equals load texture um What is it called grass mono grass monitor i’m gonna brag and say my memory is really good today and that’s probably wrong now so stone tex equals uh stone text thank you very much auto complete um is that it is that all i have to do oh yeah this is probably wrong Mouse button left let’s just go and check my prepared code left mouse up that’s not bad i didn’t i wasn’t too too wrong left mouse up there we go so yeah what’s really important is he’s going to wait until the the key is released and so this is maybe something you could play Around with if you did want this maybe if the key is held down if the mouse button is held down you could like spray a load of blocks everywhere but i don’t want to do that i just want to place one when i’ve released the mouse button so let’s see if this Uh works have we already got building so i can jump i’m going to go over to the you know what no i’ll go over to my my god goose man goosey boos and we’ll lay down in his honor a brick let’s see if this works oh yeah and i’m projecting three ahead You know what three feels about right press the mouse button and it crashes so what happened entity object has no attribute duplicate oh so let me guess do i do it in this different way do i do it this way do i just say duplicate um Bte and then that returns bte there oops ctrl z let’s try that again bte and let’s just see in my prepared code oh goodness do i just do this in the um update so messy my uh prepare code sorry not an update in the key so oh that’s yeah how i do it So i duplicate it and then a lovely okay oh i’ve also got this you know what let’s just nick this let’s just nick that copy and paste that and i’ll put that in there as well so a cena’s got a few as you’ve already seen some nice inbuilt functions and another one Is shape so it’s like an animation that you can apply to any entity and you give it a duration i.e how long it’s going to shake and then the speed of the shaking so i just set that to 0.5 so half a second and then speed to what’s that i don’t know A tenth of a second oh maths right so that should now shake um so now hopefully i’ve duplicated bte our build tool entity in the correct way let’s see i embarrassed myself in front of the giant goose sorry hence his angry look on his face So i’m sorry about that let me try this again and left key and it’s made one and i’m quite proud of my shake animation idea i didn’t code the shake animation code that was peta amaland i think okay so now now it’s actually uh not only can we explore and make friends With giant geese but we can build things and it suddenly becomes like lego suddenly becomes like a minecraft whoops what did i do then first person controller has no attribute height oh so that’s something that i’ve done wrong in that previous it does have an added an attribute called height Hmm right so we need to go and correct that code so that’s scary that means my previous um tutorial code has an error in it so i probably changed that code as well because i don’t want it to crash on people’s machines i’ll not know what’s happened um So that’s in our safety net so our safety net can now crash and crash you um i think it was the first time our safety net was working though so it kind of detected that i fell through um glitch through the terrain and the problem is i don’t know where oh That’s in the update loop isn’t it and now because i’m kind of scrolling around can’t find things i’m thinking maybe next video i need to organize this into different files so we could do that together maybe the next video um oh so it doesn’t have height subject I can’t think what that is right um let’s just hard code in two um i just hope that’s okay also this maybe should be minus one i’m thinking now no we do want to go higher no we’re talking about being under the terrain so it does want to be minus Save as whoops cancel i just wanted to save right let’s play it again wow lots of bugs recently but i think bugs are helpful in terms of tutorials showing debugging is uh good well i find it helpful anyway when i’m trying to learn so left key we can build I’ll try and do lots of jumping building hopefully try and glitch myself so i’m just trying to wait for that there we go it’s trying to combine we didn’t glitch too badly so we’re okay so we’ve left these floating blocks and things oh in minecraft you can’t do that so we’ve Made super minecraft so you could and maybe i’ll do it if if you really want me to you could write a constraint to say if there’s no block beneath us then you can’t or sorry no block attached to this position then you can’t build so that would take a little bit of a Coding to do that so um we do want to do before we get to can i do this all right let’s do another race i’ve got four minutes can we now press the other mouse button to to delete blocks um so i’m going to let’s just look at my prepared code And see what i’ve done i’ve said if key is right mouse up um and then oh this is quite a nice function actually if um our mouse hovered entities that means this is an inbuilt function in a cena so if our mouth is actually over an entity Such as our terrain or it might be a built block um and it’s actually visible then we can destroy it and that shouldn’t destroy our terrain because our terrain doesn’t have a collider so this mouse hovered function will ignore things or it won’t detect things that don’t have a collider on What might go wrong though is if oh no i was gonna say if um our build tool entity is getting in the way but it doesn’t have a collider either but let’s just i’ll let’s okay let’s try and write the code out without copying it so we’ll say in input we’ll say alif Meaning else if else if you don’t have to put that but i’m just doing it to expose you to some new things you might not know um and it’s kind of better a practice um to like logically determine things and i’m running out of time let me Stop explaining stuff so i want to say if i route uh right mouse is up then um entity equals um mouse so the hovered entity function is a method on the mouse object in the orcina engine e equals mouse um hovered entity um let’s look at my code again oh no I don’t need to uh call a function there so all i’ve done is is store whatever entity our mouse is over as long as it’s got a collider and then let’s just try deleting it let’s just say um destroy destroy e so that’s how you delete an entity destroy e let’s just See what happens we might not have to add any uh visibility to this so i can make my objects let’s try and delete them well it’s working already what if i do it here well it’s not crashing at least what if i go into the terrain good it’s at least not deleting the Terrain um one bad thing of course is that at the moment with this system we can’t mine we can’t um delete this terrain um because it’s just like one object and what we’d need to do well i haven’t worked this out but i have to like change the system somehow But what we need to do is re um calculate this part of the purlin terrain and then at this position minus the height by one and then recombine this subset into the whole terrain the difficult thing is we then have to take away this combined part of the terrain Which doesn’t really work with our system at the moment in other words we have to maybe make not combine all of the um all of the subsets into the train at the end so we’re gonna have to figure something else out so that’s five minutes um where that’s taken me to Do the kind of pseudo mining so we can we can at least build things and we can at least remove them so we can do kind of our own mining and oh yeah we can we can interact with these new things so can i jump up and Yeah i can jump and i build myself a tower which is kind of cool okay so um thank you very much for watching next time um what we’ll do is we’ll be able to build different types of um different types of block and i don’t know what else maybe put in the comments What what feature you want to see next maybe it’s biomes maybe it’s trees maybe it’s maybe making an infinite terrain um yeah but just let me know thank you very much again for watching and i’ll see you next time goodbye Video Information
This video, titled ‘Minecraft in python: building and mining, and more colour (with Ursina) – part 4’, was uploaded by Red Hen dev on 2021-06-26 17:14:12. It has garnered 2914 views and 91 likes. The duration of the video is 00:41:24 or 2484 seconds.
Here we complete what I had intended to finish last video — we add a little nuance to the coloration of the terrain, and then create a simple block building and ‘mining’ function.
Code: https://github.com/RedHenDev/ursina_tutorials/tree/main/python_minecraft_tut_2021
Thanks for watching!