我正在嘗試使用Python 2.7的新Minecraft Pi Edition API編寫遊戲。當我意識到有兩段代碼互相阻塞時,我已經完成了遊戲。如果我先放置A節,則會阻止B節運行,直到A節完成。如果我先放置B部分,它會運行得非常慢。我決定我需要將兩部分分成兩個獨立的線程。 '當從線程類調用Python函數時行爲不如預期
這是仍然存在問題的代碼的減少版本。我注意到如果我爲兩個類中的一個註釋掉了ClassName.start()
行,那麼其他類將會正常運行。
import mcpi.minecraft as minecraft
import threading
mc = minecraft.Minecraft.create()
class BlockCheckThread(threading.Thread):
def run(self):
while True:
event = mc.events.pollBlockHits()
class WinningCheckThread(threading.Thread):
def run(self):
while True:
blockTest = mc.getBlock(1, 1, 1,) == 50
def main():
WinningCheckThread().start() # If I comment out either of these .start() lines
BlockCheckThread().start() # the other class executes perfectly.
運行此的錯誤如下:
Exception in thread Thread-2:
...
TypeError: Hit() takes exactly 5 arguments (1 given)
Exception in thread Thread-1:
...
ValueError: invalid literal for int() with base 10: '
要運行需要一個樹莓派的代碼,需要從here下載的Minecraft然後,您必須從運行api/python/
目錄。 (即它必須與它正在導入的mcpi
模塊在同一個文件夾中。)
這是我第一次嘗試線程化,所以不要在我的代碼中笑得太厲害。我很想知道爲什麼這段代碼不起作用,我該怎麼辦才能解決它。
謝謝。這工作!現在一切都很完美!好極了!太多驚歎號! – daviewales 2013-02-13 00:43:20