Learn Python With MineCraft
- have fun with programming and game
2.2 Use for
Loop to stack blocks
for learnning how to use for
loop, please visit Python For Loops
Below mission only need using for ... range
loop.
- [Mission-2.1]Stack 5 blocks without loop
You could repeat your code 5 times
x,y,z = pos = mc.player.getTilePos()
# place 5 block
id=103
mc.setBlock(x,y,z,id)
mc.setBlock(x,y,z,id)
mc.setBlock(x,y,z,id)
mc.setBlock(x,y,z,id)
mc.setBlock(x,y,z,id)
- [Mission-2.2] Stack 5 blocks by using for
loop
Check below for range
syntax, please build 5 pillars around you.
# for loop examplei
x,y,z = pos = mc.player.getTilePos()
id=103
for i in range(0,5):
print("i=",i)
mc.setBlock(x,y+i,z,id)
-[Mission 2.3] Try set blocks
to use the setBlocks, you need pass two set of blocks
mc.setBlocks(x,y,z,x1,y1,z1,blockId)
Double loop
You could put one loop in another loop, it call double loop.
- [Mission 2.4] use loop build a wall
let’s start build a tower with single loop
from mcpi_e.minecraft import Minecraft
from mcpi_e import block
serverAddress = "localhost" # change to your minecraft server
playerName = "stoneskin2020"
pythonApiPort = 4711
mc = Minecraft.create(serverAddress,pythonApiPort,playerName)
pos = mc.player.getTilePos()
x = pos.x
y = pos.y
z = pos.z
#build a tower use loop
for h in range(5):
print("h=",h)
mc.setBlock(x,y+h,z,block.STONE.id)
you could build couple of tower to make it wall
for h in range(5):
print("h=",h)
mc.setBlock(x,y+h,z,block.STONE.id)
mc.setBlock(x+1,y+h,z,block.STONE.id)
mc.setBlock(x+2,y+h,z,block.STONE.id)
mc.setBlock(x+3,y+h,z,block.STONE.id)
mc.setBlock(x+4,y+h,z,block.STONE.id)
- [Mission 2.5] Use double loop build a wall
Try To use double loop:
for h in range(5):
for w in range(5):
print("h={} w={}".format(h,w))
mc.setBlock(x+w,y+h,z,block.STONE.id)
- [Mission 2.6] 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)
- [Mission-2.7] Build a 5x5x5 Cube
you need 3 layers of loop, see below example of add 3 layer of loop on mission 2.3.
id=103
for k in range(0,5):
for j in range(0,5):
for i in range(0,5):
mc.setBlock(x+j,y+i,z+k,id)
- [Mission-2.8] use double loop to explor the map
create a map, or use a map mods, then try to use python code move your player to exploer the map x from -1000 to 1000 step 75 z from -1000 to 1000 step 75 you need stay each place for 5 sec waiting for the view be loaded
import time
for x in range(-1000,1000,75):
time.sleep(5)
- [Challenge] [Mission-2.9] Build a pyramid in Minecraft
Modified your code in mission-2.15, build a pyramid. please show you work by send your pyramid’s location and your script to the teacher.