python跳过我的第二个for循环

时间:2017-01-14 23:52:43

标签: python minecraft

我正在使用API​​在我的世界中使用python代码。我有两个柱子,一个瘦的,一个胖的,两个都有不同的大小。我试图将它们建在一个有点随机点的方形区域。我的所有代码都工作正常,直到底部。它运行第一个for循环发布“1 of 20 complete”但跳过第二个并将“2 of 20 complete”行发布到游戏聊天中。为什么它跳过第二个for循环?

from mcpi.minecraft import Minecraft
mc = Minecraft.create()
import random
import time

定义了如何构建瘦骨柱

def skinny_pillar(height, blockType):
    global biomex
    global biomez
    y = mc.getHeight(biomex, biomez)
    if (height == 3):
        height = 2
    if (height == 5):
        height = 4

    #Check it won't hit into anything
    count = 0
    block1 = mc.getBlock(biomex - 3, y, biomez)
    block2 = mc.getBlock(biomex + 3, y, biomez)
    block3 = mc.getBlock(biomex, y, biomez - 3)
    block4 = mc.getBlock(biomex, y, biomez + 3)
    while (count != 1):
        if (block1 != 0):
            biomex += 1
        elif (block2 != 0):
            biomex -= 1
        elif (block3 != 0):
            biomez += 1
        elif (block4 != 0):
            biomez -= 1
        else:
            count += 1

    #Base of the pillar
    baseHeight = height
    mc.setBlocks(biomex - 1, y, biomez - 1, biomex + 1, y + baseHeight, biomez + 1, blockType)
    mc.setBlocks(biomex - 2, y, biomez, biomex  + 2, y + baseHeight + 1, biomez, blockType)
    mc.setBlocks(biomex, y, biomez - 2, biomex, y + baseHeight + 1, biomez + 2, blockType)

    #2nd layer of the pillar
    secondHeight = height * 3
    mc.setBlocks(biomex - 1, y, biomez, biomex + 1, y + secondHeight + 1, biomez, blockType)
    mc.setBlocks(biomex, y, biomez - 1, biomex, y + secondHeight + 1, biomez + 1, blockType)

    #Point of the pillar
    pointHeight = height * 6
    mc.setBlocks(biomex, y, biomez, biomex, y + pointHeight, biomez, blockType)

    #Randomlly choose if the pillar will have a rock on top and only put it on the taller ones
    rock = random.randint(1, 100)
    if (height >= 4):
        if (rock >= 70):
            #Layer one
            mc.setBlocks(biomex - 1, y + pointHeight, biomez - 1, biomex + 1, y + pointHeight, biomez + 1, blockType)
            mc.setBlocks(biomex, y + pointHeight, biomez - 2, biomex, y + pointHeight, biomez + 2, blockType)
            #Layer two
            mc.setBlocks(biomex - 2, y + pointHeight + 1, biomez - 1, biomex + 2, y + pointHeight + 1, biomez + 1, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 1, biomez - 2, biomex + 1, y + pointHeight + 1, biomez + 2, blockType)
            mc.setBlocks(biomex, y + pointHeight + 1, biomez - 3, biomex, y + pointHeight + 1, biomez + 3, blockType)
            #Layer three
            mc.setBlocks(biomex - 2, y + pointHeight + 2, biomez - 2, biomex + 2, y + pointHeight + 2, biomez + 2, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 2, biomez - 3, biomex + 1, y + pointHeight + 2, biomez + 3, blockType)
            mc.setBlocks(biomex , y + pointHeight + 2, biomez - 4, biomex, y + pointHeight + 2, biomez + 4, blockType)
            #Layer four
            mc.setBlocks(biomex - 2, y + pointHeight + 3, biomez - 1, biomex + 2, y + pointHeight + 3, biomez + 1, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 3, biomez - 2, biomex + 1, y + pointHeight + 3, biomez + 2, blockType)
            mc.setBlocks(biomex, y + pointHeight + 3, biomez - 3, biomex, y + pointHeight + 3, biomez + 3, blockType)
            #Layer five
            mc.setBlocks(biomex - 1, y + pointHeight + 4, biomez - 1, biomex + 1, y + pointHeight + 4, biomez + 1, blockType)
            mc.setBlocks(biomex, y + pointHeight + 4, biomez - 2, biomex, y + pointHeight + 4, biomez + 2, blockType)

定义了如何构建胖柱

def fat_pillar(height, blockType):
    global biomex
    global biomez
    y = mc.getHeight(biomex, biomez)
    if (height == 3):
        height = 2
    if (height == 5):
        height = 4

    #Check it won't hit into anything
    count = 0
    while (count != 1):
        block1 = mc.getBlock(biomex - 5, y, biomez)
        block2 = mc.getBlock(biomex + 5, y, biomez)
        block3 = mc.getBlock(biomex, y, biomez - 5)
        block4 = mc.getBlock(biomex, y, biomez + 5)
        if (block1 != 0):
            biomex += 1
        elif (block2 != 0):
            biomex -= 1
        elif (block3 != 0):
            biomez += 1
        elif (block4 != 0):
            biomez -= 1
        else:
            count += 1

    #Base of the pillar
    baseHeight = height / 2
    mc.setBlocks(biomex - 3, y, biomez - 2, biomex + 3, y + baseHeight - 1, biomez + 2, blockType)
    mc.setBlocks(biomex - 2, y, biomez - 3, biomex + 2, y + baseHeight - 1, biomez + 3, blockType)
    mc.setBlocks(biomex, y, biomez - 4, biomex, y + baseHeight - 1, biomez + 4, blockType)
    mc.setBlocks(biomex - 4, y, biomez, biomex + 4, y + baseHeight - 1, biomez, blockType)

    #2nd layer of the pillar
    secondHeight = height
    mc.setBlocks(biomex - 2, y, biomez - 1, biomex + 2, y + secondHeight, biomez + 1, blockType)
    mc.setBlocks(biomex - 1, y, biomez - 2, biomex + 1, y + secondHeight, biomez + 2, blockType)
    mc.setBlocks(biomex - 3, y, biomez - 1, biomex + 3, y + secondHeight - 1, biomez + 1, blockType)
    mc.setBlocks(biomex - 1, y, biomez - 3, biomex + 1, y + secondHeight - 1, biomez + 3, blockType)

    #3rd layer of the pillar
    thirdHeight = height * 2
    mc.setBlocks(biomex - 1, y, biomez - 1, biomex + 1, y + thirdHeight, biomez + 1, blockType)
    mc.setBlocks(biomex, y, biomez - 1, biomex, y + thirdHeight + 1, biomez + 1, blockType)
    mc.setBlocks(biomex - 1, y, biomez, biomex + 1, y + thirdHeight + 1, biomez, blockType)

    #Point of the pillar
    pointHeight = height * 3
    mc.setBlocks(biomex, y, biomez, biomex, y + pointHeight, biomez, blockType)

    #Randomlly choose if the pillar will have a rock on top and only put it on the taller ones
    rock = random.randint(1, 100)
    if (height >= 4):
        if (rock >= 70):
            #Layer one
            mc.setBlocks(biomex - 1, y + pointHeight, biomez - 1, biomex + 1, y + pointHeight, biomez + 1, blockType)
            mc.setBlocks(biomex, y + pointHeight, biomez - 2, biomex, y + pointHeight, biomez + 2, blockType)
            #Layer two
            mc.setBlocks(biomex - 2, y + pointHeight + 1, biomez - 1, biomex + 2, y + pointHeight + 1, biomez + 1, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 1, biomez - 2, biomex + 1, y + pointHeight + 1, biomez + 2, blockType)
            mc.setBlocks(biomex, y + pointHeight + 1, biomez - 3, biomex, y + pointHeight + 1, biomez + 3, blockType)
            #Layer three
            mc.setBlocks(biomex - 2, y + pointHeight + 2, biomez - 2, biomex + 2, y + pointHeight + 2, biomez + 2, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 2, biomez - 3, biomex + 1, y + pointHeight + 2, biomez + 3, blockType)
            mc.setBlocks(biomex , y + pointHeight + 2, biomez - 4, biomex, y + pointHeight + 2, biomez + 4, blockType)
            #Layer four
            mc.setBlocks(biomex - 2, y + pointHeight + 3, biomez - 1, biomex + 2, y + pointHeight + 3, biomez + 1, blockType)
            mc.setBlocks(biomex - 1, y + pointHeight + 3, biomez - 2, biomex + 1, y + pointHeight + 3, biomez + 2, blockType)
            mc.setBlocks(biomex, y + pointHeight + 3, biomez - 3, biomex, y + pointHeight + 3, biomez + 3, blockType)
            #Layer five
            mc.setBlocks(biomex - 1, y + pointHeight + 4, biomez - 1, biomex + 1, y + pointHeight + 4, biomez + 1, blockType)
            mc.setBlocks(biomex, y + pointHeight + 4, biomez - 2, biomex, y + pointHeight + 4, biomez + 2, blockType)

获得所有内容都围绕着的球员位置。

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

我将方形区域(生物群系)分成多个列。这是第一次。其余的代码与此相同,但坐标位置略有不同。

#Make the first column of the biome
c = z -50
d = z -40
for i in range(-50, 0, 10):
    a = x + i
    b = x + (i + 10)
    biomex = random.randint(a, b)
    biomez = random.randint(c, d)
    pillar = random.randint(1, 2)
    if (pillar == 1):
        fat_pillar(random.randint(2, 6), 1)
    else:
        skinny_pillar(random.randint(2, 6), 1)
    time.sleep(0.5)
mc.postToChat("1 of 20 complete")
for i in range(50, 0, -10):
    a = x + i
    b = x + (i - 10)
    biomex = random.randint(a, b)
    biomez = random.randint(c, d)
    pillar = random.randint(1, 2)
    if (pillar == 1):
        fat_pillar(random.randint(2, 6), 1)
    else:
        skinny_pillar(random.randint(2, 6), 1)
    time.sleep(0.5)
mc.postToChat("2 of 20 complete")

它构建列的负坐标部分(第一个for循环)并将该行发布到聊天。然后它立即将第二行跳过第二个for循环(该列的正坐标部分)。我做错了什么?

1 个答案:

答案 0 :(得分:0)

所以我开始添加文本以显示所有类似建议的内容,并发现所有代码都能正常工作,甚至进入第二个循环的开始,直到biomex = random.randint(a, b)我之前添加的文本该行显示“得到了117,得到了b 107”。由此我意识到问题是a和b的排序。在第二个循环中,它使b为较低的数字,而较高的数字与第一个循环相反。因为它太工作所有我不得不做的是交换a和b,以便较低的数字是第一个而较高的数字是最后的biomex = random.randint(b, a)我认为奇怪的是python刚停止执行代码而没有显示我的错误信息。如果有的话,我可能早就注意到了。