2017-01-14 12 views
-1

私はminecraftでPythonコードを使用するためにAPIを使用しています。私は2つの柱を持っています。私はそれらを幾分ランダムな場所で正方形の区域に建てるようにしています。私のコードはすべて下まで細かく動作します。 "1/20完了"のループを最初に実行しますが、2回目をスキップして、 "2/20完了"行をゲームチャットに投稿します。 2番目のforループをスキップするのはなぜですか?Pythonが私の2番目の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ループ)を作成し、その行をチャットにポストします。次に、2番目のforループ(列の正の座標セクション)をスキップする2番目の行を直ちにポストします。私は間違って何をしていますか?

+3

デバッグコードを挿入してみます。例えばそれぞれの関数内でprint( "fat_pillar()activated!") '、' print( "skinny_pillar successfully created!") '、' print(biomex) 'を実行して関数が正しい値で活性化されているかどうかを確認します。 – ooknosi

+0

forループ自体が動いているため、より深く見る必要があります。あなたはプリントステートメントを追加するための素晴らしい提案を持っています...それは何かを示しましたか? – tdelaney

答えて

0

私は提案のようなすべてのものを表示するためにテキストを追加して行きました。すべてのコードがうまく動くことがわかりました。biomex = random.randint(a, b)まで追加されたテキスト"117を得て、107を得た。"それから、私は問題がaとbの順序であることに気付きました。第2のループでは、bを第1のループの反対側の低い方の番号と高くする。そのためには、私がしなければならなかったことは、aとbをスワップして、最初の番号と最後の番号を入れ替えることでした。biomex = random.randint(b, a) Pythonがコードの実行を止めてしまったと思います。 。もしそれがあったなら、私はおそらくそれを早く気付くことができたでしょう。

関連する問題