猿问

Python Turtle 点无缘无故闪烁

在我的程序中,为了模拟运动,添加了点的x位置,清除旧点,绘制新点。


def drawing_zombies():

  clear()

  for zombie in zombies:

    goto(zombie.x, zombie.y)

    dot(20, 'red')

  update()


def movement():

  for zombie in zombies:

    zombie.x -= 0.5

  drawing_zombies()

我见过一个极其相似的程序运行,它的点不闪,看起来是真的在动。但是,当我的程序运行时,它会闪烁(消失和重新出现的速度非常快)


其余代码在下面(除了一堆定义矢量类的东西,它与工作程序中的相同,所以它里面没有任何问题)


class vector(collections.abc.Sequence):

    precision = 6

    __slots__ = ('_x', '_y', '_hash')


    def __init__(self, x, y):

        self._hash = None

        self._x = round(x, self.precision)

        self._y = round(y, self.precision)

    #The rest of the vector class would have been here


zombies = []

placement_options = [0, 1, 2, -1]


def new_zombie():

    placement_level = random.choice(placement_options)

    z = vector(200, placement_level*100)

    print(placement_level)

    zombies.append(z)


def drawing_zombies():

  clear()

  for zombie in zombies:

    goto(zombie.x, zombie.y)

    dot(20, 'red')

  update()


def movement():

  for zombie in zombies:

    zombie.x -= 0.5

  drawing_zombies()


  for z in zombies:

    if z.x < -200:

      done()

  ontimer(movement(), 50)


def gameplay():

  setup(420, 420, 370, 0)

  hideturtle()

  up()

  new_zombie()

  movement()

  done()



gameplay()


慕丝7291255
浏览 212回答 1
1回答

德玛西亚99

您可以使用该tracer(False)功能来禁用屏幕更新。所以基本上,所有的绘图都是在内存中完成的,并且会在你调用时一次性复制到屏幕上tracer(True)。def drawing_zombies():&nbsp; tracer(False)&nbsp; clear()&nbsp; for zombie in zombies:&nbsp; &nbsp; goto(zombie.x, zombie.y)&nbsp; &nbsp; dot(20, 'red')&nbsp; update()&nbsp; tracer(True)
随时随地看视频慕课网APP

相关分类

Python
我要回答