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.

back to main

Learn Python With MineCraft

- have fun with programming and game

4 Use Condition if...else

To learn comdition please check Python If…Else

4.1 Boolean and Condition Basic

Booleans represent one of two values: True or False

For learn more and practic Boolean, please check Python Boolean

- [Mission-4.1] Try Comparatoers in python

Python has below comparatoers comparatoers

Try below python code and remember their result is True or False

a=True
b=False

print("a=",a)
print("b=",b)
print("is a equals b",a==b)
print("is a  not equals b", a!=b)

x=2
y=3
print ("x={} y={}".format(x,y))
print("is x equals y",x==y)
print("is x greater than y", x>y)
print("ix x smaller than y", x<y)

- [Mission 4.2] use if ... elif..else with boolean to Check if you are in water or not

To learn Python Conditions please check Python If…Else Try blow code in Minecraft

from mcpi_e.minecraft import Minecraft
from mcpi_e import block

serverAddress = "localhost" # change to your minecraft server
playerName = "yourname"
pythonApiPort = 4711

mc = Minecraft.create(serverAddress,pythonApiPort,playerName)

pos = mc.player.getPos()
x = pos.x
y = pos.y
z = pos.z

blockTypeId=mc.getBlock(x,y,z)
if(blockTypeId==block.WATER.id):
    mc.postToChat("["+playerName+"]: I am swimming!")

update upper code using more conditions with elif and else

...

blockTypeId=mc.getBlock(x,y,z)
if(blockTypeId==block.WATER.id):
    mc.postToChat("["+playerName+"]: I am swimming!")
elif(blockTypeId==block.AIR.id):
    mc.postToChat("["+playerName+"]: I am flying!")
else:
    mc.postToChat("[{}]: I am on block {}".format(playerName,blockTypeId))


- [Mission 4.3] Use Logical Operators and or not

logicaloperators

Learn more could check python logical operators

Try below code with and to validate if blockTypeId is valid or not.

# example of and
if(blockTypeId <0 and blockTypeId >252):
    print("It's not a valid blockId")

below code use or to check if you are on the ground

# example of or
if(blockTypeId ==block.DIRT.id or blockTypeId == block.GRASS.id):
    mc.postToChat("I am on the ground.")

below code user not to reverse True or False not(True) == False not(False) == True

# example of not
if(not(blockTypeId ==block.WATER.id)):
    mc.postToChat("I am not in water.")

- [Mission 4.4] Check if you are in a build

Complete the code to check if you are in your home

#step 1 get your home location and size
homeX=?
homeY=?
homeZ=?
homeWidth=10
homeHeight=5
homeLength=15

pos=mc.player.getTilePos()

insideX = homeX <pos.x < homeX+homeWidth
insideY = #todo
insideZ = # toto
if(inside):
    print("I am in my home")
else:
    print("I am out of my home")


- [Mission 4.5] Add condition to your code of build wall

you could put some pattern on your wall, add below code to your missiion 2.13 code

... loop
    ...
       if(w==y):
            mc.setBlock(x+w,y+h,z,block.BEDROCK.id)

back to main