Re: MineCraft with Python
Neil Shah <[email protected]> Fri, 2 Dec 2022 10:27:58 -0500
| Newsgroups | gmane.comp.python.education |
|---|---|
| Message-ID | <CA+mnL9EQ1wQQamsPiieMkN56gJHLr7GLKkgU38_=rRvqF5ZeAQ@mail.gmail.com> |
--===============1689573374014039896== Content-Type: multipart/alternative; boundary="0000000000004b4d8e05eed9fca1" --0000000000004b4d8e05eed9fca1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks Owen! I combined it into a Gist to make it easier for people to use. https://gist.github.com/NeilShah2026/32d48b438407ba0eb5c6a56cbbc4ff65 On Fri, Dec 2, 2022 at 10:20 AM Cabal Owen <[email protected]> wrote: > Python scripts can generate neat in-world things, and there are many > examples on the web. With a few lines you can draw a giant glass sphere, > and with a bit more work make a giant Sierpinski triangle in the sky and > even import obj files like a space shuttle. I myself made fun scripts to > draw a water-filled glass donut and a gigantic Klein bottle, to turn > everything around into TNT and to control Minecraft with your brain using= a > MindFlex EEG toy. There is a whole book introducing programming using > python scripts for Minecraft, and you can even make simple Minecraft-base= d > games. I will also show how to do simple (and sometimes more elaborate) > turtle-based drawings in Minecraft, while you can ride along with the > drawing as the turtle. > > For a while now you could write python scripts for Minecraft on the > Raspberry Pi. I wanted my kids to be able to do that, but we don't have a > Pi, plus it would be nice to do this with the full desktop Minecraft. You > could run your own server with the Raspberry Juice plugin which enables > most of the python scripts to work. But not everyone wants to install and > configure a server. > > So I wrote the Raspberry Jam Mod for Minecraft 1.8 (now ported to 1.8.8, > 1.8.9 and 1.9 as well) that emulates most of the Raspberry Pi Minecraft > protocol (about the same as the Raspberry Juice plugin provides) and lets > Raspberry Pi python scripts run with full desktop Minecraft. (I later fou= nd > out that someone wrote the mcpiapi mod for Minecraft 1.7.10 a couple of > weeks earlier.) I wrote this Instructable initially for Python 2.7, but I > think most of my samples will work for 3.x. > > I assume that you have basic facility with creating folders and > downloading, unzipping, and copying files on Windows (or your operating > system of choice). > > You can create Python scripts for Minecraft with a text editor, the IDLE > environment which comes with Python, or with Visual Studio Python Tools o= n > Windows. The last is actually the nicest in some ways, so I'll have some > optional steps on how to do that. > > This summer I plan to teach coding and basic 3D geometry to gifted middle= - > and high-schoolers using Minecraft, Raspberry Jam Mod, Python and Visual > Studio. > > If you want to do this with Minecraft Pocket Edition on Android instead, = I > have an Instructable for that, too. > > Step 1. Modules and libraries for Make Minecraft in Python > To make this Minecraft game in Python, we will utilize the ursina Python > module. It is a Python cross-platform game creation package similar to > PyGame. > > To install this library, start a terminal or command prompt in the projec= t > folder and paste the following command. > > pip install ursina > Step 2. importing library > Import the Ursina Engine and all the Textures that we will require in the > future. > > from ursina import * > from ursina.prefabs.first_person_controller import FirstPersonController > app =3D Ursina() > grass_texture =3D load_texture('assets/grass_block.png') > stone_texture =3D load_texture('assets/stone_block.png') > brick_texture =3D load_texture('assets/brick_block.png') > dirt_texture =3D load_texture('assets/dirt_block.png') > sky_texture =3D load_texture('assets/skybox.png') > arm_texture =3D load_texture('assets/arm_texture.png') > punch_sound =3D Audio('assets/punch_sound',loop =3D False, autoplay =3D= False) > block_pick =3D 1 > To load the textures and block you need to download the Assets folder and > paste it where your Minecraft code Python file is stored. > > Now there are a few things we need to do to make this all work, and let= =E2=80=99s > start by making actual cubes that we can utilize. This will be the most > significant modification that we can do. It would appear appropriate if w= e > applied a more appropriate texture to it. To do that we created a couple = of > textures. > > Step 3. Making Voxel(Cubes) for Minecraft in Python > class Voxel(Button): > def __init__(self, position =3D (0,0,0), texture =3D grass_textur= e): > super().__init__( > parent =3D scene, > position =3D position, > model =3D 'assets/block', > origin_y =3D 0.5, > texture =3D texture, > color =3D color.color(0,0,random.uniform(0.9,1)), > scale =3D 0.5) > The third step is to build a new class for each voxel that we want to > construct, therefore the class name will be voxel. And it derives from th= e > button class. The basic explanation is that anytime I click on a voxel, I > want to make another voxel just adjacent to it. So I need to know where > each voxel is in relation to which button. And in here, we need our init > method again and nothing else for the time being, and we also need the > super method with an underscore to init. And now we must supply a few > parameters, the first of which is parent, as shown above. > > The next stage would be to find a position, and the current status is > (0,0). Next, I=E2=80=99d want to make a model of what the object would lo= ok like. > And for the time being, this one will be a cube (parameters), but we may > use different forms. The next step is to pass origin_y so that the height > and 3D space of this cube are effectively (0.5), and the final and most > crucial item is texture and color. And both of these things multiplied. > > Step 4. Making Minecraft Arena > for z in range(20): > for x in range(20): > voxel =3D Voxel(position =3D (x,0,z)) > Now that we have everything, we need to create a button for it. The cube= =E2=80=99s > current positions are x, y, and z. (0,0,0). To create individual voxels, = we > must first create a for loop in which the voxel is set. And if you need t= o > make it bigger, increase 35 by 35. One of the current issues with this ga= me > is that if you add too many of these fields, the game may slow down. As a > result, there would be a significant amount of optimization work to be do= ne. > > Step 5. Creating First person Controller(FPC) > from ursina.prefabs.first_person_controller import FirstPersonController > player =3D FirstPersonController() > The question is how we can develop a first-person character in all of > this. and ursina makes this super simple. Because it comes with a number = of > preset classes that we may utilize to construct a first-person character. > We don=E2=80=99t really have to do anything about it. And really, all we = have to do > is import something else first, which is from ursina. prefabs.first_perso= n_ > controller. This is not by default imported into Oceana, so we have to do > it ourselves, but once we do it, all we have to do to create FPC (First > Person Character) is to create a new variable where we store and then cal= l > FPC. > > Step 6. Making Interactive UI for Minecraft Python Edition > def input(self,key): > if self.hovered: > if key =3D=3D 'left mouse down': > punch_sound.play() > if block_pick =3D=3D 1: voxel =3D Voxel(position = =3D > self.position + mouse.normal, texture =3D grass_texture) > if block_pick =3D=3D 2: voxel =3D Voxel(position = =3D > self.position + mouse.normal, texture =3D stone_texture) > if block_pick =3D=3D 3: voxel =3D Voxel(position = =3D > self.position + mouse.normal, texture =3D brick_texture) > if block_pick =3D=3D 4: voxel =3D Voxel(position = =3D > self.position + mouse.normal, texture =3D dirt_texture) > if key =3D=3D 'right mouse down': > punch_sound.play() > destroy(self) > What we need to find out now is how to make this all more interactive. We > must also create and destroy blocks, which will be really simple. Because > what we want to accomplish is, when you push the voxel button, we want to > construct a new block at that location. We develop an input function to d= o > this. Make some transmissions as well. And then make a to change voxel fo= r > voxel texture > > Step 7. Changing Different Textures for Coding Minecraft in Python > def update(): > global block_pick > > if held_keys['left mouse'] or held_keys['right mouse']: > hand.active() > else: > hand.passive() > > if held_keys['1']: block_pick =3D 1 > if held_keys['2']: block_pick =3D 2 > if held_keys['3']: block_pick =3D 3 > if held_keys['4']: block_pick =3D 4 > To make Minecraft with python, we need to change the cube texture, and fo= r > changing the cube texture here we created a loop by clicking one to four > keys to alter the voxel texture by Left click and destroying it with the > right key. In simple words. If we press any of these buttons this block > pick gets a different number. > > Step 8. Creating the Sky > class Sky(Entity): > def __init__(self): > super().__init__( > parent =3D scene, > model =3D 'sphere', > texture =3D sky_texture, > scale =3D 150, > double_sided =3D True) > There are three other components that can significantly help to convey th= e > game. The first is a Sky Box, the second is a Hand, and the third is > noises. And let us take them one step at a time in Minecraft coding Pytho= n. > The first is a skybox, which we emulated in our game by creating a huge > sphere with a sky texture. To do this, we will construct a new class name= d > Sky. > > Step 9. Make an FPC Hand > class Hand(Entity): > def __init__(self): > super().__init__( > parent =3D camera.ui, # This 3D space for Our UI = and > 2D space for our camera > model =3D 'assets/arm', > texture =3D arm_texture, > scale =3D 0.2, > rotation =3D Vec3(150,-10,0), #vec3 is represent = 3D > space and the parameters are for position(x,y,z) > position =3D Vec2(0.4,-0.6)) #vec2 is represent 2= D > space and the parameters are for position(x,y,z) > > def active(self): > self.position =3D Vec2(0.3,-0.5) > > def passive(self): > self.position =3D Vec2(0.4,-0.6) > Now the next thing we will going to need is a hand and this one is purely > decorative. Basically I have used some types of variables while working o= n > https://enterprise.affle.com/mobile-app-development . It does not do > anything besides simulating having a hand it=E2=80=99s a straightforward = thing by > creating a class called hand, this one is also going to be an entity. > > Complete code of Minecraft in Python - > from ursina import * > from ursina.prefabs.first_person_controller import FirstPersonController > > app =3D Ursina() > grass_texture =3D load_texture('assets/grass_block.png') > stone_texture =3D load_texture('assets/stone_block.png') > brick_texture =3D load_texture('assets/brick_block.png') > dirt_texture =3D load_texture('assets/dirt_block.png') > sky_texture =3D load_texture('assets/skybox.png') > arm_texture =3D load_texture('assets/arm_texture.png') > punch_sound =3D Audio('assets/punch_sound',loop =3D False, autoplay =3D= False) > block_pick =3D 1 > > window.fps_counter.enabled =3D False #To Disable the FPS Counter > window.exit_button.visible =3D False #To Remove the exit(close) Button > > def update(): > global block_pick > > if held_keys['left mouse'] or held_keys['right mouse']: > hand.active() > else: > hand.passive() > > if held_keys['1']: block_pick =3D 1 > if held_keys['2']: block_pick =3D 2 > if held_keys['3']: block_pick =3D 3 > if held_keys['4']: block_pick =3D 4 > > class Voxel(Button): > def __init__(self, position =3D (0,0,0), texture =3D grass_textur= e): > super().__init__( > parent =3D scene, > position =3D position, > model =3D 'assets/block', > origin_y =3D 0.5, > texture =3D texture, > color =3D color.color(0,0,random.uniform(0.9,1)), > scale =3D 0.5) > > def input(self,key): > if self.hovered: > if key =3D=3D 'left mouse down': > punch_sound.play() > if block_pick =3D=3D 1: voxel =3D Voxel(p= osition > =3D self.position + mouse.normal, texture =3D grass_texture) > if block_pick =3D=3D 2: voxel =3D Voxel(p= osition > =3D self.position + mouse.normal, texture =3D stone_texture) > if block_pick =3D=3D 3: voxel =3D Voxel(p= osition > =3D self.position + mouse.normal, texture =3D brick_texture) > if block_pick =3D=3D 4: voxel =3D Voxel(p= osition > =3D self.position + mouse.normal, texture =3D dirt_texture) > > if key =3D=3D 'right mouse down': > punch_sound.play() > destroy(self) > > class Sky(Entity): > def __init__(self): > super().__init__( > parent =3D scene, > model =3D 'sphere', > texture =3D sky_texture, > scale =3D 150, > double_sided =3D True) > > class Hand(Entity): > def __init__(self): > super().__init__( > parent =3D camera.ui, > model =3D 'assets/arm', > texture =3D arm_texture, > scale =3D 0.2, > rotation =3D Vec3(150,-10,0), > position =3D Vec2(0.4,-0.6)) > > def active(self): > self.position =3D Vec2(0.3,-0.5) > > def passive(self): > self.position =3D Vec2(0.4,-0.6) > > for z in range(20): > for x in range(20): > voxel =3D Voxel(position =3D (x,0,z)) > > player =3D FirstPersonController() > sky =3D Sky() > hand =3D Hand() > > app.run() > > Hope it will help everyone. > _______________________________________________ > Edu-sig mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/edu-sig.python.org/ > Member address: [email protected] > --0000000000004b4d8e05eed9fca1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr">Thanks Owen!</div><div dir=3D"ltr"><br></= div><div>I combined it into a Gist to make it easier for people to use.</di= v><div><a href=3D"https://gist.github.com/NeilShah2026/32d48b438407ba0eb5c6= a56cbbc4ff65">https://gist.github.com/NeilShah2026/32d48b438407ba0eb5c6a56c= bbc4ff65<br></a></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class= =3D"gmail_attr">On Fri, Dec 2, 2022 at 10:20 AM Cabal Owen <<a href=3D"m= ailto:[email protected]">[email protected]</a>> wrote:<br></div><blo= ckquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left= :1px solid rgb(204,204,204);padding-left:1ex">Python scripts can generate n= eat in-world things, and there are many examples on the web. With a few lin= es you can draw a giant glass sphere, and with a bit more work make a giant= Sierpinski triangle in the sky and even import obj files like a space shut= tle. I myself made fun scripts to draw a water-filled glass donut and a gig= antic Klein bottle, to turn everything around into TNT and to control Minec= raft with your brain using a MindFlex EEG toy. There is a whole book introd= ucing programming using python scripts for Minecraft, and you can even make= simple Minecraft-based games. I will also show how to do simple (and somet= imes more elaborate) turtle-based drawings in Minecraft, while you can ride= along with the drawing as the turtle.<br> <br> For a while now you could write python scripts for Minecraft on the Raspber= ry Pi. I wanted my kids to be able to do that, but we don't have a Pi, = plus it would be nice to do this with the full desktop Minecraft. You could= run your own server with the Raspberry Juice plugin which enables most of = the python scripts to work. But not everyone wants to install and configure= a server.<br> <br> So I wrote the Raspberry Jam Mod for Minecraft 1.8 (now ported to 1.8.8, 1.= 8.9 and 1.9 as well) that emulates most of the Raspberry Pi Minecraft proto= col (about the same as the Raspberry Juice plugin provides) and lets Raspbe= rry Pi python scripts run with full desktop Minecraft. (I later found out t= hat someone wrote the mcpiapi mod for Minecraft 1.7.10 a couple of weeks ea= rlier.) I wrote this Instructable initially for Python 2.7, but I think mos= t of my samples will work for 3.x.<br> <br> I assume that you have basic facility with creating folders and downloading= , unzipping, and copying files on Windows (or your operating system of choi= ce).<br> <br> You can create Python scripts for Minecraft with a text editor, the IDLE en= vironment which comes with Python, or with Visual Studio Python Tools on Wi= ndows. The last is actually the nicest in some ways, so I'll have some = optional steps on how to do that.<br> <br> This summer I plan to teach coding and basic 3D geometry to gifted middle- = and high-schoolers using Minecraft, Raspberry Jam Mod, Python and Visual St= udio.<br> <br> If you want to do this with Minecraft Pocket Edition on Android instead, I = have an Instructable for that, too.<br> <br> Step 1. Modules and libraries for Make Minecraft in Python<br> To make this Minecraft game in Python, we will utilize the ursina Python mo= dule. It is a Python cross-platform game creation package similar to PyGame= .<br> <br> To install this library, start a terminal or command prompt in the project = folder and paste the following command.<br> <br> pip install ursina<br> Step 2. importing library<br> Import the Ursina Engine and all the Textures that we will require in the f= uture.<br> <br> from ursina import *<br> from ursina.prefabs.first_person_controller import FirstPersonController<br= > app =3D Ursina()<br> grass_texture =3D load_texture('assets/grass_block.png')<br> stone_texture =3D load_texture('assets/stone_block.png')<br> brick_texture =3D load_texture('assets/brick_block.png')<br> dirt_texture=C2=A0 =3D load_texture('assets/dirt_block.png')<br> sky_texture=C2=A0 =C2=A0=3D load_texture('assets/skybox.png')<br> arm_texture=C2=A0 =C2=A0=3D load_texture('assets/arm_texture.png')<= br> punch_sound=C2=A0 =C2=A0=3D Audio('assets/punch_sound',loop =3D Fal= se, autoplay =3D False)<br> block_pick =3D 1<br> To load the textures and block you need to download the Assets folder and p= aste it where your Minecraft code Python file is stored.<br> <br> Now there are a few things we need to do to make this all work, and let=E2= =80=99s start by making actual cubes that we can utilize. This will be the = most significant modification that we can do. It would appear appropriate i= f we applied a more appropriate texture to it. To do that we created a coup= le of textures.<br> <br> Step 3. Making Voxel(Cubes) for Minecraft in Python<br> class Voxel(Button):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self, position =3D (0,0,0), textur= e =3D grass_texture):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D scene,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 position =3D position,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'assets/block',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 origin_y =3D 0.5,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 color =3D color.color(0,0,random.uniform(0.9,1)),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 0.5)<br> The third step is to build a new class for each voxel that we want to const= ruct, therefore the class name will be voxel. And it derives from the butto= n class. The basic explanation is that anytime I click on a voxel, I want t= o make another voxel just adjacent to it. So I need to know where each voxe= l is in relation to which button. And in here, we need our init method agai= n and nothing else for the time being, and we also need the super method wi= th an underscore to init. And now we must supply a few parameters, the firs= t of which is parent, as shown above.<br> <br> The next stage would be to find a position, and the current status is (0,0)= . Next, I=E2=80=99d want to make a model of what the object would look like= . And for the time being, this one will be a cube (parameters), but we may = use different forms. The next step is to pass origin_y so that the height a= nd 3D space of this cube are effectively (0.5), and the final and most cruc= ial item is texture and color. And both of these things multiplied.<br> <br> Step 4. Making Minecraft Arena<br> for z in range(20):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 for x in range(20):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 voxel =3D Voxel(pos= ition =3D (x,0,z))<br> Now that we have everything, we need to create a button for it. The cube=E2= =80=99s current positions are x, y, and z. (0,0,0). To create individual vo= xels, we must first create a for loop in which the voxel is set. And if you= need to make it bigger, increase 35 by 35. One of the current issues with = this game is that if you add too many of these fields, the game may slow do= wn. As a result, there would be a significant amount of optimization work t= o be done.<br> <br> Step 5. Creating First person Controller(FPC)<br> from ursina.prefabs.first_person_controller import FirstPersonController<br= > player =3D FirstPersonController()<br> The question is how we can develop a first-person character in all of this.= and ursina makes this super simple. Because it comes with a number of pres= et classes that we may utilize to construct a first-person character. We do= n=E2=80=99t really have to do anything about it. And really, all we have to= do is import something else first, which is from ursina. prefabs.first_per= son_ controller. This is not by default imported into Oceana, so we have to= do it ourselves, but once we do it, all we have to do to create FPC (First= Person Character) is to create a new variable where we store and then call= FPC.<br> <br> Step 6. Making Interactive UI for Minecraft Python Edition<br> def input(self,key):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if self.hovered:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if key =3D=3D '= left mouse down':<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 punch_sound.play()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if block_pick =3D=3D 1: voxel =3D Voxel(position =3D self.positi= on + mouse.normal, texture =3D grass_texture)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if block_pick =3D=3D 2: voxel =3D Voxel(position =3D self.positi= on + mouse.normal, texture =3D stone_texture)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if block_pick =3D=3D 3: voxel =3D Voxel(position =3D self.positi= on + mouse.normal, texture =3D brick_texture)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if block_pick =3D=3D 4: voxel =3D Voxel(position =3D self.positi= on + mouse.normal, texture =3D dirt_texture)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if key =3D=3D 'right mouse down':<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 punch_sound.play()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 destroy(self)<br> What we need to find out now is how to make this all more interactive. We m= ust also create and destroy blocks, which will be really simple. Because wh= at we want to accomplish is, when you push the voxel button, we want to con= struct a new block at that location. We develop an input function to do thi= s. Make some transmissions as well. And then make a to change voxel for vox= el texture<br> <br> Step 7. Changing Different Textures for Coding Minecraft in Python<br> def update():<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 global block_pick<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['left mouse'] or held_keys= ['right mouse']:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 hand.active()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 else:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 hand.passive()<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['1']: block_pick =3D 1<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['2']: block_pick =3D 2<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['3']: block_pick =3D 3<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['4']: block_pick =3D 4<br> To make Minecraft with python, we need to change the cube texture, and for = changing the cube texture here we created a loop by clicking one to four ke= ys to alter the voxel texture by Left click and destroying it with the righ= t key. In simple words. If we press any of these buttons this block pick ge= ts a different number.<br> <br> Step 8. Creating the Sky<br> class Sky(Entity):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D scene,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'sphere',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D sky_texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 150,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 double_sided =3D True)<br> There are three other components that can significantly help to convey the = game. The first is a Sky Box, the second is a Hand, and the third is noises= . And let us take them one step at a time in Minecraft coding Python. The f= irst is a skybox, which we emulated in our game by creating a huge sphere w= ith a sky texture. To do this, we will construct a new class named Sky.<br> <br> Step 9. Make an FPC Hand<br> class Hand(Entity):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D camera.ui, # This 3D space for Our UI and 2D space fo= r our camera<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'assets/arm',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D arm_texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 0.2,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 rotation =3D Vec3(150,-10,0), #vec3 is represent 3D space and th= e parameters are for position(x,y,z)<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 position =3D Vec2(0.4,-0.6)) #vec2 is represent 2D space and the= parameters are for position(x,y,z)<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def active(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.position =3D V= ec2(0.3,-0.5)<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def passive(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.position =3D V= ec2(0.4,-0.6)<br> Now the next thing we will going to need is a hand and this one is purely d= ecorative. Basically I have used some types of variables while working on <= a href=3D"https://enterprise.affle.com/mobile-app-development" rel=3D"noref= errer" target=3D"_blank">https://enterprise.affle.com/mobile-app-developmen= t</a> . It does not do anything besides simulating having a hand it=E2=80= =99s a straightforward thing by creating a class called hand, this one is a= lso going to be an entity.<br> <br> Complete code of Minecraft in Python -<br> from ursina import *<br> from ursina.prefabs.first_person_controller import FirstPersonController<br= > <br> app =3D Ursina()<br> grass_texture =3D load_texture('assets/grass_block.png')<br> stone_texture =3D load_texture('assets/stone_block.png')<br> brick_texture =3D load_texture('assets/brick_block.png')<br> dirt_texture=C2=A0 =3D load_texture('assets/dirt_block.png')<br> sky_texture=C2=A0 =C2=A0=3D load_texture('assets/skybox.png')<br> arm_texture=C2=A0 =C2=A0=3D load_texture('assets/arm_texture.png')<= br> punch_sound=C2=A0 =C2=A0=3D Audio('assets/punch_sound',loop =3D Fal= se, autoplay =3D False)<br> block_pick =3D 1<br> <br> window.fps_counter.enabled =3D False #To Disable the FPS Counter <br> window.exit_button.visible =3D False #To Remove the exit(close) Button<br> <br> def update():<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 global block_pick<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['left mouse'] or held_keys= ['right mouse']:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 hand.active()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 else:<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 hand.passive()<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['1']: block_pick =3D 1<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['2']: block_pick =3D 2<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['3']: block_pick =3D 3<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 if held_keys['4']: block_pick =3D 4<br> <br> class Voxel(Button):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self, position =3D (0,0,0), textur= e =3D grass_texture):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D scene,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 position =3D position,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'assets/block',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 origin_y =3D 0.5,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 color =3D color.color(0,0,random.uniform(0.9,1)),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 0.5)<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def input(self,key):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if self.hovered:<br= > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if key =3D=3D 'left mouse down':<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 punch_sound.play()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if block_pick =3D=3D 1: voxel =3D Vo= xel(position =3D self.position + mouse.normal, texture =3D grass_texture)<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if block_pick =3D=3D 2: voxel =3D Vo= xel(position =3D self.position + mouse.normal, texture =3D stone_texture)<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if block_pick =3D=3D 3: voxel =3D Vo= xel(position =3D self.position + mouse.normal, texture =3D brick_texture)<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if block_pick =3D=3D 4: voxel =3D Vo= xel(position =3D self.position + mouse.normal, texture =3D dirt_texture)<br= > <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 if key =3D=3D 'right mouse down':<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 punch_sound.play()<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 destroy(self)<br> <br> class Sky(Entity):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D scene,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'sphere',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D sky_texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 150,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 double_sided =3D True)<br> <br> class Hand(Entity):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def __init__(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 super().__init__(<b= r> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 parent =3D camera.ui,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 model =3D 'assets/arm',<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 texture =3D arm_texture,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 scale =3D 0.2,<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 rotation =3D Vec3(150,-10,0),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 position =3D Vec2(0.4,-0.6))<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def active(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.position =3D V= ec2(0.3,-0.5)<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 def passive(self):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 self.position =3D V= ec2(0.4,-0.6)<br> <br> for z in range(20):<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 for x in range(20): <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 voxel =3D Voxel(pos= ition =3D (x,0,z))<br> <br> player =3D FirstPersonController()<br> sky =3D Sky()<br> hand =3D Hand()<br> <br> app.run()<br> <br> Hope it will help everyone.<br> _______________________________________________<br> Edu-sig mailing list -- <a href=3D"mailto:[email protected]" target=3D"_bl= ank">[email protected]</a><br> To unsubscribe send an email to <a href=3D"mailto:[email protected]"= target=3D"_blank">[email protected]</a><br> <a href=3D"https://mail.python.org/mailman3/lists/edu-sig.python.org/" rel= =3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/ed= u-sig.python.org/</a><br> Member address: <a href=3D"mailto:[email protected]" target=3D"_blank= ">[email protected]</a><br> </blockquote></div></div> --0000000000004b4d8e05eed9fca1-- --===============1689573374014039896== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Edu-sig mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/edu-sig.python.org/ Member address: [email protected] --===============1689573374014039896==--