需要帮助以海龟蟒蛇为背景制作两个动画

问题描述 投票:0回答:1

guy0
guy1
下的图我都有,背景是
back0
。我可以使用我尝试使用的方法将其中一张图画制作成动画,但不能同时制作两者。

当我为其设置动画时,我似乎也无法绘制背景。另一个人与

human0
完全相同,只是衬衫颜色不同。我需要缩短这篇文章。如有任何帮助,我们将不胜感激。

这是代码:

from turtle import *
def back0():
  delay(0)
  tracer(0)
  bgcolor("#87ceeb")
  up()
  goto(0,0)
  pd()
  up()
  goto(160,350)
  pd()
  fillcolor("#FDB813")
  begin_fill()
  circle(60)
  end_fill()
  rt(90)
  up()
  fd(5)
  rt(90)
  pd()
  begin_fill()
  fd(5)
  lt(130)
  fd(20)
  lt(130)
  fd(20)
  lt(30)
  lt(90)
  fd(15)
  end_fill()
  up()

def guy0(x,y):
  delay(0)
  tracer(0)
  up()
  goto(x,y)
  pd()
  fillcolor("#E8BEAC")
  begin_fill()
  circle(25)
  end_fill()
  up()
  goto(x,y-80)
  pd()
  fd(20)
  lt(90)
  fd(60)
  fillcolor("#FF5F1F")
  begin_fill()
  circle(20,180)
  end_fill()
  begin_fill()
  fd(60)
  lt(90)
  fd(40)
  lt(90)
  fd(60)
  lt(90)
  up()
  fd(40)
  end_fill()
  setheading(0)

xCoor = -400
yCoor = -50
screen = Screen()
def move():
  global xCoor, yCoor
  xCoor += 3
  

  if xCoor  > 420:
    xCoor=-420
  
  clear()
  guy0(xCoor,yCoor)

  screen.ontimer(move,1)

move()
python turtle-graphics python-turtle
1个回答
0
投票

我在任何地方都看不到

guy1
human0
,因此我将按原样使用您的代码,并让您从那里获取它。

一般来说,在调试和编码时,缓慢而有条理地工作很重要。如果您跳过并添加一堆代码,而不验证其是否有意义并且在整个过程中的每一步都有效,那么您很容易会遇到 4-5 个难以解决的问题。

  • back0
    永远不会被调用。
  • tracer(0)
    bgcolor("#87ceeb")
    应该是在程序开始时一次性调用,而不是在循环中运行,一遍又一遍地做同样的事情。
  • 我不确定你的屏幕尺寸是多少,但对我来说,
    goto(160,350)
    将看似太阳的东西移出屏幕,并且有一些
    pd()
    /
    up()
    调用似乎没有实现任何目标。我会添加注释或将太阳绘图代码移至其自己的函数中,以便更容易理解。
  • 画完太阳后,调用
    setheading(0)
    恢复航向,避免
    guy0
    的身体被画歪。
  • 我建议使用默认值
    screen.ontimer(10)
    ,而不是
    screen.ontimer(1)
    ,它比我的屏幕刷新速度更快,可能会导致抖动。

以下是带有注释的修改:

from turtle import *

def back0():
    up()
    goto(0, 0)
    goto(160, 150) # 350 is off-screen for me
    pd()
    fillcolor("#FDB813")
    begin_fill()
    circle(60)
    end_fill()
    rt(90)
    up()
    fd(5)
    rt(90)
    pd()
    begin_fill()
    fd(5)
    lt(130)
    fd(20)
    lt(130)
    fd(20)
    lt(30)
    lt(90)
    fd(15)
    end_fill()
    up()
    setheading(0) # restore heading

def guy0(x, y):
    up()
    goto(x, y)
    pd()
    fillcolor("#E8BEAC")
    begin_fill()
    circle(25)
    end_fill()
    up()
    goto(x, y - 80)
    pd()
    fd(20)
    lt(90)
    fd(60)
    fillcolor("#FF5F1F")
    begin_fill()
    circle(20, 180)
    end_fill()
    begin_fill()
    fd(60)
    lt(90)
    fd(40)
    lt(90)
    fd(60)
    lt(90)
    up()
    fd(40)
    end_fill()
    setheading(0)

x_coor = -400
y_coor = -50
screen = Screen()
ht()               #
tracer(0)          # call once up front rather than in the loop
bgcolor("#87ceeb") #

def move():
    global x_coor, y_coor
    x_coor += 3

    if x_coor > 420:
        x_coor =- 420

    clear()
    back0()
    guy0(x_coor, y_coor)
    screen.ontimer(move, 10)

move()
exitonclick()

现在,如果您想为另一个人设置动画,请添加坐标和绘图函数,然后在循环中调用它,就像

guy0
一样。或者研究并创建一个类(可能还有列表和循环),它可用于动态存储坐标,让您轻松地创建任意数量的人,而无需使用
guy0
guy1
...
 重复代码guy100
这很快就会变得乏味。

还可以考虑使用

from turtle import Screen, Turtle
,或者至少逐项列出所有导入的变量,而不是使用通配符。这可以使您的变量与海龟明确分开,避免别名错误,并有助于防止同时使用函数模式和实例模式引起的错误。

© www.soinside.com 2019 - 2024. All rights reserved.