首页 > 解决方案 > 乌龟的动画不超过一个字符

问题描述

我希望多只海龟在屏幕上移动,但只有一个对象被动画化。另外,我希望这两个对象朝相反的方向移动,因为它是游戏的一部分(类似于青蛙)。伤害对象在列表 'game_objects' 和 type='harm 中,它们的方向是让我让第一个从右到左工作,而不是另一个从左到右工作。

    game_objects = [{'t':turtle.Turtle(),'x': 0, 'y': -140, 'radius': 10, 'image': 
    'pikachu.gif', 'speed': 1, 'type':'player', 'direction':''}, 
    {'t':turtle.Turtle(), 'y': 0, 'image': 'gym.gif', 
    'type':'harm', 'direction':'left'}, {'t':turtle.Turtle(), 'y': -80,'image': 'gym.gif', 'type':'harm', 'direction':'right'}]
   
    def main():
      global sc
      global player
      sc = turtle.Screen()
      sc.setup(width=300, height=300)
      sc.bgcolor("black")
      sc.bgpic('ezgif.com-gif-maker (3).gif')
      sc.tracer(0)
      w, h = sc.screensize()
      sc.addshape('pikachu.gif')
      pikachu = game_objects[0]

    def animate (x,speed,  y , path):
      global sc
      sc.addshape(x['image'])
      if x['type'] == 'harm':
        if path == 'left':
          x['t'].speed(0)
          x['t'].shape("gym.gif")         
          x['t'].penup()
          x['t'].goto(-100, y)
          while True :
            if x['t'].xcor() < 160:
              sc.update()
              x['t'].forward(speed)
            else:
              x['t'].goto(-150,y)
        else:
          x['t'].speed(0)
          x['t'].shape("gym.gif")         
          x['t'].penup()
          x['t'].goto(-100, y)
          while True:
            if player.distance(x['t']) < 25:
              update_values(1, 1)
              player.goto(0,-120)
            if x['t'].xcor() < -160:
              sc.update()
              x['t'].backward(speed)
            else:
              x['t'].goto(150,y)

      main()


      for b in game_objects:
         animate(b,0.04, b['y'], b['direction'])

标签: pythonanimationgraphicsturtle-graphicspython-turtle

解决方案


我们可以使用ontimer()screen 的方法来替换你的while True:循环,这些循环在像海龟这样的事件驱动的世界中没有位置。下面是代码的精简版本,它以不同的速度在屏幕上以相反的方向移动两个对象:

from turtle import Screen, Turtle

TRUE_WIDTH, TRUE_HEIGHT = 300, 300

CHROME = 14  # magic number possibly derivable from tkinter

WIDTH, HEIGHT = TRUE_WIDTH + CHROME, TRUE_HEIGHT + CHROME # need to be slightly larger

IMAGE_SIZE = 20  # cursor size for this example; for your code, the size of the gif images

game_objects = [
    {'t': Turtle(), 'y': 0, 'image': 'circle', 'type':'harm', 'direction':'left', 'speed': 0.4},
    {'t': Turtle(), 'y': -80, 'image': 'square', 'type':'harm', 'direction':'right', 'speed': 0.8}
]

def initialize(game_object):
    # screen.addshape(game_object['image'])

    turtle = game_object['t']
    turtle.shape(game_object['image'])
    turtle.penup()

    direction = game_object['direction']

    if direction == 'right':
        turtle.goto(-100, game_object['y'])
    elif direction == 'left':
        turtle.goto(100, game_object['y'])

    screen.update()

def animate(game_object):
    if game_object['type'] == 'harm':
        turtle = game_object['t']

        if game_object['direction'] == 'right':
            if turtle.xcor() < IMAGE_SIZE + TRUE_WIDTH/2:
                turtle.forward(game_object['speed'])
            else:
                turtle.goto(-IMAGE_SIZE - TRUE_WIDTH/2, game_object['y'])
        elif game_object['direction'] == 'left':
            if turtle.xcor() > -IMAGE_SIZE - TRUE_WIDTH/2:
                turtle.backward(game_object['speed'])
            else:
                turtle.goto(IMAGE_SIZE + TRUE_WIDTH/2, game_object['y'])

        screen.update()

    screen.ontimer(lambda: animate(game_object), 100)

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.screensize(WIDTH/2, HEIGHT/2)  # backing store needs to be smaller than window
screen.tracer(False)

for game_object in game_objects:
    initialize(game_object)
    animate(game_object)

screen.mainloop()

由于我没有您的 GIF 图像,因此我已将其切换为光标形状。我还添加了代码来帮助在海龟中使用小窗口。(小窗口更容易受到不必要的滚动条和窗口镶边区域损失的影响。)另外,请重新阅读global关键字的正确使用。


推荐阅读