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-based 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 found 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 on 
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 project 
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 = Ursina()
grass_texture = load_texture('assets/grass_block.png')
stone_texture = load_texture('assets/stone_block.png')
brick_texture = load_texture('assets/brick_block.png')
dirt_texture  = load_texture('assets/dirt_block.png')
sky_texture   = load_texture('assets/skybox.png')
arm_texture   = load_texture('assets/arm_texture.png')
punch_sound   = Audio('assets/punch_sound',loop = False, autoplay = False)
block_pick = 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’s 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 we 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 = (0,0,0), texture = grass_texture):
                super().__init__(
                        parent = scene,
                        position = position,
                        model = 'assets/block',
                        origin_y = 0.5,
                        texture = texture,
                        color = color.color(0,0,random.uniform(0.9,1)),
                        scale = 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 the 
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’d 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 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 = Voxel(position = (x,0,z))
Now that we have everything, we need to create a button for it. The cube’s 
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 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 down. As a result, there 
would be a significant amount of optimization work to be done.

Step 5. Creating First person Controller(FPC)
from ursina.prefabs.first_person_controller import FirstPersonController
player = 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’t 
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_person_ 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.

Step 6. Making Interactive UI for Minecraft Python Edition
def input(self,key):
        if self.hovered:
                if key == 'left mouse down':
                        punch_sound.play()
                        if block_pick == 1: voxel = Voxel(position = 
self.position + mouse.normal, texture = grass_texture)
                        if block_pick == 2: voxel = Voxel(position = 
self.position + mouse.normal, texture = stone_texture)
                        if block_pick == 3: voxel = Voxel(position = 
self.position + mouse.normal, texture = brick_texture)
                        if block_pick == 4: voxel = Voxel(position = 
self.position + mouse.normal, texture = dirt_texture)
                        if key == '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 do this. Make some 
transmissions as well. And then make a to change voxel for 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 = 1
        if held_keys['2']: block_pick = 2
        if held_keys['3']: block_pick = 3
        if held_keys['4']: block_pick = 4
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 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 = scene,
                        model = 'sphere',
                        texture = sky_texture,
                        scale = 150,
                        double_sided = True)
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 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 named Sky.

Step 9. Make an FPC Hand
class Hand(Entity):
        def __init__(self):
                super().__init__(
                        parent = camera.ui, # This 3D space for Our UI and 2D 
space for our camera
                        model = 'assets/arm',
                        texture = arm_texture,
                        scale = 0.2,
                        rotation = Vec3(150,-10,0), #vec3 is represent 3D space 
and the parameters are for position(x,y,z)
                        position = Vec2(0.4,-0.6)) #vec2 is represent 2D space 
and the parameters are for position(x,y,z)

        def active(self):
                self.position = Vec2(0.3,-0.5)

        def passive(self):
                self.position = 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 on 
https://enterprise.affle.com/mobile-app-development . It does not do anything 
besides simulating having a hand it’s 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 = Ursina()
grass_texture = load_texture('assets/grass_block.png')
stone_texture = load_texture('assets/stone_block.png')
brick_texture = load_texture('assets/brick_block.png')
dirt_texture  = load_texture('assets/dirt_block.png')
sky_texture   = load_texture('assets/skybox.png')
arm_texture   = load_texture('assets/arm_texture.png')
punch_sound   = Audio('assets/punch_sound',loop = False, autoplay = False)
block_pick = 1

window.fps_counter.enabled = False #To Disable the FPS Counter 
window.exit_button.visible = 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 = 1
        if held_keys['2']: block_pick = 2
        if held_keys['3']: block_pick = 3
        if held_keys['4']: block_pick = 4

class Voxel(Button):
        def __init__(self, position = (0,0,0), texture = grass_texture):
                super().__init__(
                        parent = scene,
                        position = position,
                        model = 'assets/block',
                        origin_y = 0.5,
                        texture = texture,
                        color = color.color(0,0,random.uniform(0.9,1)),
                        scale = 0.5)

        def input(self,key):
                if self.hovered:
                        if key == 'left mouse down':
                                punch_sound.play()
                                if block_pick == 1: voxel = Voxel(position = 
self.position + mouse.normal, texture = grass_texture)
                                if block_pick == 2: voxel = Voxel(position = 
self.position + mouse.normal, texture = stone_texture)
                                if block_pick == 3: voxel = Voxel(position = 
self.position + mouse.normal, texture = brick_texture)
                                if block_pick == 4: voxel = Voxel(position = 
self.position + mouse.normal, texture = dirt_texture)

                        if key == 'right mouse down':
                                punch_sound.play()
                                destroy(self)

class Sky(Entity):
        def __init__(self):
                super().__init__(
                        parent = scene,
                        model = 'sphere',
                        texture = sky_texture,
                        scale = 150,
                        double_sided = True)

class Hand(Entity):
        def __init__(self):
                super().__init__(
                        parent = camera.ui,
                        model = 'assets/arm',
                        texture = arm_texture,
                        scale = 0.2,
                        rotation = Vec3(150,-10,0),
                        position = Vec2(0.4,-0.6))

        def active(self):
                self.position = Vec2(0.3,-0.5)

        def passive(self):
                self.position = Vec2(0.4,-0.6)

for z in range(20):
        for x in range(20): 
                voxel = Voxel(position = (x,0,z))

player = FirstPersonController()
sky = Sky()
hand = Hand()

app.run()

Hope it will help everyone.
_______________________________________________
Edu-sig mailing list -- edu-sig@python.org
To unsubscribe send an email to edu-sig-le...@python.org
https://mail.python.org/mailman3/lists/edu-sig.python.org/
Member address: arch...@mail-archive.com

Reply via email to