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0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1
1,1,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1
1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
源碼:
import time
from mcpi.minecraft import Minecraft
import mcpi.block as block
from mcpi.minecraft import CmdPositioner
mc = Minecraft.create()
mc.postToChat("連接成功,開(kāi)始編程")
l_id = mc.getPlayerEntityId("longsimiao")
positioner = CmdPositioner(mc.conn, b"entity")
pos = positioner.getTilePos(l_id)
x = pos.x
y = pos.y
z = pos.z
filename = "迷宮數(shù)據(jù).csv"
f = open(filename, "r")
for line in f.readlines():
print(line)
data = line.split(",")
x = pos.x
for cell in data:
if cell == '0':
mc.setBlock(x, y, z, block.AIR.id)
mc.setBlock(x, y + 1, z, block.AIR.id)
mc.setBlock(x, y - 1, z, block.GRASS.id)
else:
mc.setBlock(x, y, z, block.DIAMOND_BLOCK.id)
mc.setBlock(x, y + 1, z, block.DIAMOND_BLOCK.id)
mc.setBlock(x, y - 1, z, block.GRASS.id)
time.sleep(0.1)
x += 1
z += 1