首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >海龟不为多个字符设置动画

海龟不为多个字符设置动画
EN

Stack Overflow用户
提问于 2021-11-07 01:12:22
回答 1查看 35关注 0票数 0

我想让多个乌龟在屏幕上移动,但只有一个对象有动画效果。另外,我希望这两个物体走在相反的方向,因为它是游戏的一部分(类似于青蛙)。危害对象在“game_objects”和“type=”危害列表中,它们的方向是给定的。我得到了第一个从右到左工作的对象,但另一个没有从左到右工作。

代码语言:javascript
复制
    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'])
EN

回答 1

Stack Overflow用户

发布于 2021-11-07 19:13:27

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

代码语言:javascript
复制
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图片,所以我把它换成了光标形状。在turtle中使用小窗口时,我还添加了一些代码来提供帮助。(小窗口更容易受到不必要的滚动条和窗口铬造成的面积损失的影响。)另外,请重新阅读有关global关键字的正确用法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69869086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档