View on GitHub

python-minecraft

Introductions and Python Code examples for kids to learn python programming with minecraft. The Python code will run with a modified MCPI (Pi edition API Python Library) call `mcpi-e`, and a mincraft server call spigot with the RaspberryJuice plugin installed.

2.3 Data Types String and Number

1 Use PostToChat to print a “String” to Minecraft

In Python any amount of text call a string, you could use string like this

print("Hello Minecraft")

name ="Steve the Miner"

print(name)

- [Mission-3.1] Say Hello to other in the Minecraft

To say “Hello” to everybody in the Minecraft server, you could do below

from mcpi_e.minecraft import Minecraft
serverAddress="server-address" # change to your minecraft server
playerName ="yourname"
pythonApiPort=4711

mc=Minecraft.create(serverAddress,pythonApiPort,playerName)
mc.postToChat("Hello!")

[Mission-3.2] Use Python Input Function input other’s nae

You could take the data from input, try blow

name=input("What's your name?")
mc.postToChat(playerName+" say: Hello, "+name+"!")

[Mission-3.3] Try the format function to build a string

Other than user “+” to connect string into a sentense, you could use format function:

yourName ="stoneskin2020"
aname=input("Who you want say hello to?")
print("{} say: Hello {}!".format(yourName,aname))

2 Convert a String to Number with int()

String and Intiger is different DataType, for detail please read Python Data Types. Below is the Data Types we possible will used in our class

datatypes

example of get type of a variable:

x = 5
print(type(x))

The data you got form input is a string, we need convert to number before using as number. int(str) could do this job.

blockType=input("Enter a block type:")
blockTypeId=int(blockType)

other way if you want change a int to string, you could use str(number)

value=103
print("Watermelon block id is "+str(value))

[Mission-3.4] Create a block with input

...
blockType= #add input() function here
blockTypeId= # add conver code here

x,y,z=pos=mc.player.getTilePos()

mc.setBlock(x,y,z,blokTypeId)

[Mission-3.5] Use try: except: to get the error

Python will throw the error to the terminal, you can get that error message and handle it.

try:
    number=int(input("what's the block type you want get?"))
except:
    print("Invalid input: please enter a number!")

add upper code to your previous code, and try it.

[Mission-3.6] Use Sleep to wait some time

Some time you want wait some time and continue run your code, you could use sleep

import time # this need use time module
time.sleep(30) #Make the program wait 30 seconds

Please finish below code to check how long you move

import time
from mcpi_e.minecraft import Minecraft
serverAddress = "the server" # change to your minecraft server
playerName = "your user name" # change to your username
pythonApiPort = 4711
mc = Minecraft.create(serverAddress,pythonApiPort,playerName)

pos1 = mc.player.getTilePos()

time.sleep(15) # change to the second you like to wait

pos2=mc.player.getTilePos()

#compaire the distance
xDistance = pos1.x - pos2.x
yDistance = pos1.y - pos2.y
zDistance = pos1.z - pos2.z

print("x-distance="+xDistance+"  y-distance="+yDistance+" z-distance="+zDistance)
# Post the results to the chat
mc.postToChat("x-distance={}  y-distance={} z-distance={}".format(xDistance,yDistance,zDistance))

[Mission-3.7][Charlenage] Put them together

Write a python script do below:

  1. use for loop to run this 5 times
  2. In each loop, ask user input a block type between 0 to 250. (blocktype id list, some id is not works)
  3. if user give is not a valid number print “please give a valid blocktype” (use try..except)
  4. get the user’s current tile position, and place block
  5. [charllenge] ask the user input howmany blocks you want to stack, modify the code to build a stacks of block