0

概述:我用PythonPanda3D创建了一个类似于 Minecraft 的小型生成脚本,我把它放在一个线程上,这样我就可以使用 Time.Sleep() 和其他函数,但是知道线程为什么会崩溃吗?

我所做的:我用PythonPanda3D创建了一个类似于 Minecraft 的小型生成脚本。

问题:我不能在 Panda3D 中使用线程。

是我创建的所有代码:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
import perlin, colorsys, time
import thread

scaleX = 0.05
scaleZ = 0.05
blockSize = 1
size = 16
bottom = -70



waiter = 0.001

sn = perlin.SimplexNoise()

def threaded_generator():
    for x in range(size):
            for z in range(size):
                    time.sleep(waiter)
                    y = sn.noise2(x*scaleX, z*scaleZ)
                    # Load the environment model.
                    cube = loader.loadModel("Test")
                    # Reparent the model to render.
                    cube.reparentTo(render)
                    # Apply scale and position transforms on the model.
                    cube.setScale(blockSize, blockSize, blockSize)
                    cube.setPos(x*blockSize, z*blockSize, round(y))
                    cy = round(y)
                    while cy > bottom:
                        cy -= 1
                        cube = loader.loadModel("Test")
                        # Reparent the model to render.
                        cube.reparentTo(render)
                        # Apply scale and position transforms on the model.
                        cube.setScale(blockSize, blockSize, blockSize)
                        cube.setPos(x*blockSize, z*blockSize, round(cy))


class MyApp(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        #Ambient
        alight = AmbientLight('alight')
        alight.setColor(VBase4(0.2, 0.2, 0.2, 1))
        alnp = render.attachNewNode(alight)
        render.setLight(alnp)
        #Directional
        dlight = DirectionalLight('dlight')
        dlight.setColor(VBase4(0.8, 0.8, 0.5, 1))
        dlnp = render.attachNewNode(dlight)
        dlnp.setHpr(0, -60, 0)
        render.setLight(dlnp)
        #Cubes 
        print("Ding!")
        thread.start_new_thread(threaded_generator, ())

app = MyApp()
app.run()

对我来说它开始产生一点,

然后它停止,它似乎没有完成,有时它会很快停止,有时它会在几秒钟后停止。

〜酷:)

4

1 回答 1

0

Panda3D 手册中,您必须使用 Panda3D 提供的线程,因为底层的 c++ 结构。

检查线程:

from panda3d.core import Thread
print Thread.isThreadingSupported()

线程的使用

# WRONG:
import thread
# RIGHT:
from direct.stdpy import thread

线程的使用

# WRONG:
import threading
# RIGHT:
from direct.stdpy import threading
# ALSO RIGHT:
from direct.stdpy import threading2 as threading
于 2016-05-04T12:56:17.897 回答