cikai2 2022-06-16 13:03 采纳率: 100%
浏览 20
已结题

终于体会到程序员的痛苦——查找错误&细节优化

问题遇到的现象和发生背景

我有一串动态樱花的代码和小黄人的代码,当我把他们两组合在一起时,画布中的小黄人出现了局部绘制错误。
局部错误如下图:衣服吊带处出现绘图错位

img

单独的小黄人绘图如下图:

img

单独的动态樱花如下图:动态樱花使用了随机函数,每次绘图都不一样。

img

本人在编写提问的时候已经自行解决了该问题,最终的绘图结果如下:

img

为不浪费大家时间,下方的问题相关代码可不看,直接跳转至我的解答思路和尝试方法!

问题相关代码,请勿粘贴截图

小黄人代码:
import turtle as t

ModelSize = 1
S = ModelSize
X = 0
Y = 0
t.pensize(4 * S)
t.setup(1200,800)
t.speed(5)

def head():
t.penup()
t.fillcolor("#FFEE26")
t.goto(-130 * S + X , 10 * S + Y)
t.pendown()
t.begin_fill()
t.seth(81)
t.fd(90 * S)
t.seth(100)
t.circle(-500 * S, 3)
t.circle(-100 * S, 10)
t.circle(-200 * S, 25)
t.circle(-110 * S, 20)
t.circle(-140 * S, 30)
t.circle(-180 * S, 30)
t.circle(-200 * S, 20)
t.circle(-140 * S, 10)
t.circle(-160 * S, 50)
t.seth(85)
t.fd(-148 * S)
t.seth(-112)
t.circle(-250 * S, 14)
t.fd(200 * S)
t.right(80)
t.fd(190 * S)
t.seth(110)
t.circle(-200 * S, 7)
t.circle(-130 * S, 30)
t.end_fill()

肚兜

后脚

def houjiao():
t.begin_fill()

t.penup()
t.goto(-120 * S + X , -250 * S + Y)
t.pendown()
t.fillcolor("#030003")
t.setheading(-135)
t.circle(60 * S, 20)
t.fd(35 * S)
t.circle(20 * S, 160)
t.circle(100 * S, 10)
t.fd(20 * S)
t.goto(-120 * S + X , -250 * S + Y)

t.end_fill()

def houtui():
t.begin_fill()
t.color("black", "#0045D9")
t.penup()
t.goto(-50 * S + X , -300 * S + Y)
t.pendown()
t.setheading(-150)
t.circle(-80 * S, 60)
t.setheading(90)
t.circle(-40 * S, 67)
t.seth(-30)
t.goto(-50 * S + X , -300 * S + Y)
t.end_fill()

#衣服
def yifu():
t.begin_fill()

t.penup()
t.goto(-45 * S + X , -70 * S + Y)
t.pendown()
t.fillcolor("#0045D9")
t.setheading(-15)

t.circle(500 * S, 5)
t.circle(400 * S, 26)

t.seth(-112)
t.circle(-250 * S, 7)
t.seth(-69)
t.circle(-250 * S, 7)
t.right(15)
t.circle(-320 * S, 18)
t.circle(-330 * S, 10)
t.fd(80 * S)
t.right(81)
t.fd(190 * S)
t.seth(141)
t.circle(-180 * S, 15)
t.circle(-150 * S, 30)
t.right(6)
t.circle(-90 * S, 15)
t.seth(-45)
t.circle(50 * S, 10)
t.seth(-30)
t.circle(200 * S, 20)
t.circle(150 * S, 10)
t.seth(92)
t.circle(500 * S, 10)

t.setheading(75)
t.goto(-45 * S + X , -70 * S + Y)

t.end_fill()

口袋

def koudai():
t.begin_fill()
t.penup()
t.goto(52 * S + X , -120 * S + Y)
t.pendown()
t.fillcolor("#BFC5AD")
t.seth(-15)
t.circle(200 * S, 25)
t.seth(-88)
t.circle(-200 * S, 18)
t.seth(-150)
t.circle(-90 * S, 5)
t.right(10)
t.circle(-90 * S, 45)
t.right(20)
t.circle(-50 * S, 50)
t.goto(52 * S + X , -120 * S + Y)
t.end_fill()

t.begin_fill()
t.penup()
t.goto(70 * S + X , -155 * S + Y)
t.pendown()
t.fillcolor("#0045D9")
t.circle(-25 * S)
t.end_fill()

t.penup()
t.goto(120 * S + X , -160 * S + Y)
t.pencolor("#5C7F58")
t.pendown()
t.seth(180)
t.fd(20 * S)
t.right(60)
t.circle(6 * S, 340)
t.pencolor("black")

右手

def youshou():
t.begin_fill()
t.fillcolor("#FFEE26")
t.pencolor("black")
t.penup()
t.goto(-130 * S + X , 10 * S + Y)
t.pendown()
t.goto(-130 * S + X , -25 * S + Y)
t.seth(130)
t.circle(130 * S, 20)
t.circle(20 * S, 50)
t.right(10)
t.pencolor("black")
t.circle(120 * S, 20)
t.circle(90 * S, 30)
t.seth(-25)
t.fd(33 * S)
t.seth(40)
t.fd(35 * S)
t.circle(-30 * S, 30)
t.circle(-15 * S, 60)
t.right(5)
t.fd(80 * S)
t.end_fill()

def youzhua():
t.begin_fill()
t.fillcolor("#BEC5B3")
t.penup()
t.goto(-255 * S + X , -40 * S + Y)
t.pendown()
t.seth(-120)
t.circle(-100 * S, 10)
t.right(60)
t.circle(20 * S, 60)
t.circle(10 * S, 90)
t.right(60)
t.fd(10 * S)
t.circle(20 * S, 100)
t.left(10)
t.fd(15 * S)
t.seth(50)
t.circle(-60 * S, 30)
t.left(60)
t.circle(10 * S, 60)
t.fd(5 * S)
t.right(90)
t.fd(21 * S)
t.goto(-255 * S + X , -40 * S + Y)
t.end_fill()
# 黑色

#前腿
def qiantui():
t.begin_fill()

t.penup()
t.goto(-50 * S + X , -295 * S + Y)
t.pendown()
t.fillcolor("#0045D9")
# t.fillcolor("red")
t.setheading(-30)
t.fd(127 * S)
t.seth(62)
t.fd(60 * S)
t.seth(155)
t.circle(300 * S, 22)
t.pencolor("#0045D9")
t.goto(-49 * S + X , -294 * S + Y)
t.pencolor("black")
t.goto(-50 * S + X , -295 * S + Y)

t.end_fill()

#前脚
def qianjiao():
t.begin_fill()

t.penup()
t.goto(140 * S + X , -260 * S + Y)
t.pendown()
t.fillcolor("#030003")
t.seth(110)
t.circle(20 * S, 120)
t.seth(-120)
t.circle(500 * S, 12)
t.circle(20 * S, 82)
t.goto(140 * S + X , -260 * S + Y)

t.end_fill()

def jiaodi():
t.begin_fill()

t.penup()
t.goto(140 * S + X , -260 * S + Y)
t.pendown()
t.fillcolor("#BFC5AD")
t.seth(150)
t.circle(20 * S, 95)
t.seth(-120)
t.circle(500 * S, 10)
t.seth(-30)
t.circle(-15 * S, 60)
t.seth(-112)
t.fd(17 * S)
t.left(90)
t.circle(30 * S, 90)

t.seth(70)
t.circle(-50 * S, 25)
t.fd(7 * S)
t.circle(66 * S, 60)
t.circle(40 * S, 10)
t.goto(140 * S + X , -260 * S + Y)

t.end_fill()

def xie():
t.pencolor("black")
t.penup()
t.goto(83 * S + X , -353 * S + Y)
t.pendown()
t.seth(-30)
t.fd(31 * S)

def yanjing():
t.begin_fill()

t.penup()
t.goto(-125 * S + X , 140 * S + Y)
t.pendown()
t.fillcolor("#000000")
t.pencolor("black")
t.seth(100)
t.circle(-25 * S, 80)
t.seth(40)
t.circle(-200 * S, 23)
t.seth(-90)
t.fd(45 * S)
t.seth(195)
t.circle(200 * S, 27)
t.seth(150)
t.circle(-12 * S, 90)
t.goto(-125 * S + X , 140 * S + Y)
t.end_fill()
# 黑色
t.begin_fill()

t.penup()
t.goto(-39 * S + X , 205 * S + Y)
t.pendown()
t.fillcolor("#E6E8FA")
t.setheading(90)
t.circle(-8 * S, 180)
t.seth(-90)
t.fd(45 * S)
t.circle(-8 * S, 180)
t.goto(-39 * S + X , 205 * S + Y)
t.end_fill()
# 银色
t.begin_fill()

t.penup()
t.goto(-23 * S + X , 160 * S + Y)
t.pendown()
t.fillcolor("#E6E8FA")
t.seth(-78)
t.circle(85 * S, 130)
t.goto(-23 * S + X , 160 * S + Y)
t.end_fill()
# 银色
t.begin_fill()
t.penup()
t.goto(-23 * S + X , 190 * S + Y)
t.pendown()
t.fillcolor("#E6E8FA")
t.seth(-90)
t.circle(90 * S)
t.end_fill()
# 银色
t.begin_fill()
t.penup()
t.goto(155 * S + X , 205 * S + Y)
t.pendown()
t.fillcolor("#000000")
t.seth(-15)
t.circle(-100 * S, 20)
t.seth(-60)
t.circle(-105 * S, 25)
t.seth(160)
t.circle(200 * S, 13.5)
t.seth(75)
t.circle(90 * S, 20)
t.goto(155 * S + X , 205 * S + Y)
t.end_fill()
# 黑色
t.begin_fill()
t.penup()
t.goto(128 * S + X , 195 * S + Y)
t.pendown()
t.fillcolor("#ffffff")
t.circle(60 * S)
t.end_fill()
# 白色
t.begin_fill()
t.penup()
t.goto(110 * S + X , 150 * S + Y)
t.pendown()
t.fillcolor("#000000")
t.seth(70)
t.circle(70 * S, 110)
t.seth(20)
t.circle(-60 * S, 150)
t.end_fill()
# 黑色
t.begin_fill()
t.penup()
t.goto(65 * S + X , 205 * S + Y)
t.pendown()
t.fillcolor("#B85300")
t.circle(20 * S)
t.end_fill()
# 白色
t.begin_fill()
t.penup()
t.goto(75 * S + X , 199 * S + Y)
t.pendown()
t.fillcolor("#000000")
t.circle(8 * S)
t.end_fill()
# 黑色

#嘴巴
def zui():
t.begin_fill()
t.penup()

t.fillcolor("#FF1305")
t.goto(-25 * S + X , 60 * S + Y)
t.pendown()
t.right(30)
t.circle(-30 * S, 70)
t.left(5)
t.circle(300 * S, 20)
t.circle(120 * S, 20)
t.seth(-70)
t.circle(-50 * S, 60)
t.left(10)
t.circle(-100 * S, 30)
t.circle(-60 * S, 35)
t.right(8)
t.circle(-200 * S, 15)
t.circle(-100 * S, 15)
t.circle(-50 * S, 25)
t.circle(-200 * S, 10)
t.goto(-25 * S + X , 60 * S + Y)
t.end_fill()

#衣领
def yiling():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(-130 * S + X , 10 + Y)
t.pendown()
t.setheading(-59)
t.circle(225 * S, 38)
t.setheading(-100)
t.forward(28 * S)
t.setheading(160)
t.circle(-255 * S, 35)
t.setheading(90)
t.circle(-30 * S, 45)
t.goto(-130 * S + X , 10 * S + Y)
t.end_fill()

扣子

def zuokou():
t.begin_fill()
t.penup()

t.fillcolor("#FFFFFF")
t.goto(-40 * S + X , -80 * S + Y)
t.pendown()
t.seth(0)
t.circle(-9 * S, 360)
t.end_fill()

#左衣领
def zuoyl():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(191 * S + X , -40 * S + Y)
t.pendown()
t.seth(-112)
t.circle(-250 * S, 17)
t.seth(-68)
t.fd(25 * S)
t.seth(49)
t.circle(130 * S, 36)
t.goto(191 * S + X , -40 * S + Y)
t.end_fill()

t.begin_fill()
t.penup()
t.fillcolor("#FFFFFF")
t.goto(169 * S + X , -93 * S + Y)
t.pendown()
t.seth(0)
t.circle(-9 * S, 360)
t.end_fill()

左手

def zuoshou():
t.begin_fill()
t.penup()

t.fillcolor("#FFEE26")
t.goto(195 * S + X , -56 * S + Y)
t.pendown()
t.seth(-8)
t.circle(150 * S, 15)
t.circle(25 * S, 40)
t.left(2)
t.fd(60 * S)
t.right(85)
t.fd(28 * S)
t.right(92)
t.fd(45 * S)
t.circle(-100 * S, 20)
t.circle(-80 * S, 40)
t.circle(80 * S, 13)
t.goto(195 * S + X , -56 * S + Y)
t.end_fill()

#左掌
def zuozhua():
t.begin_fill()
t.penup()

t.fillcolor("#BEC5B3")

t.goto(295 * S + X , 25 * S + Y)
t.pendown()
t.seth(55)
t.fd(-30 * S)
t.right(110)
t.circle(80 * S, 38)
t.left(90)
t.fd(10 * S)
t.seth(20)
t.circle(100 * S, 30)
t.circle(35 * S, 180)
t.goto(295 * S + X , 25 * S + Y)
t.end_fill()

t.begin_fill()
t.fillcolor("#BEC5B3")
t.seth(140)
t.circle(-15 * S)
t.end_fill()

头发

def toufa():
t.penup()
t.goto(10 * S + X ,285 * S + Y)
t.seth(90)
t.pendown()
t.circle(80 * S,45)
t.penup()
t.goto(30 * S + X ,285 * S + Y)
t.seth(90)
t.pendown()
t.circle(200 * S,20)
t.penup()
t.goto(50 * S + X ,285 * S + Y)
t.seth(90)
t.pendown()
t.circle(-80 * S,45)

head()
zui()
youshou()
yanjing()
youzhua()
houjiao()
houtui()
yifu()
koudai()
qiantui()
qianjiao()
jiaodi()
xie()
yiling()
zuokou()
zuoshou()
zuozhua()
zuoyl()
t.penup()
t.goto(80 * S + X , -40 * S + Y)
t.pendown()
t.seth(100)
t.circle(90 * S, 85)
toufa()
t.hideturtle()

t.done()

动态樱花的代码:
import turtle
import random
from turtle import*
from time import sleep

画樱花的躯干(60,t)

def tree(branchLen, t):
sleep(0.0005)
if branchLen > 3:
if 8 <= branchLen <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 3)
elif branchLen < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branchLen / 10) # 6
t.forward(branchLen)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
tree(branchLen - 10 * b, t)
t.left(40 * a)
tree(branchLen - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branchLen)
t.down()

掉落的花瓣

def petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)

def main():

# 绘图区域
t = turtle.Turtle()
# 画布大小
setup(1200,800)

w = turtle.Screen()
t.hideturtle()  # 隐藏画笔
getscreen().tracer(5, 0)
w.screensize(bg='wheat')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

# 画樱花的躯干
tree(60, t)
# 掉落的花瓣
petal(200, t)

main()

运行结果及报错内容

运行中比较稳定,只有绘制吊带的时候出现错位
吊带处的代码为:

衣领

def yiling():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(-130 * S + X , 10 + Y)
t.pendown()
t.setheading(-59)
t.circle(225 * S, 38)
t.setheading(-100)
t.forward(28 * S)
t.setheading(160)
t.circle(-255 * S, 35)
t.setheading(90)
t.circle(-30 * S, 45)
t.goto(-130 * S + X , 10 * S + Y)
t.end_fill()
我的解答思路和尝试过的方法

通过观察,错位的那段代码也无其他特殊之处。
猜测可能的原因:
1、组合过程中出现定义重叠(只是出现绘制错位故而排除)
2、数值参数给错了(但是单独的的小黄人绘制准确)
尝试办法:
鉴于右吊带并没有出现错位现象,于是我尝试对照右吊带看看是什么问题!
右衣领代码如下:

左衣领(表述错误,此代码是右吊带!)

def zuoyl():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(191 * S + X , -40 * S + Y)
t.pendown()
t.seth(-112)
t.circle(-250 * S, 17)
t.seth(-68)
t.fd(25 * S)
t.seth(49)
t.circle(130 * S, 36)
t.goto(191 * S + X , -40 * S + Y)
t.end_fill()

t.begin_fill()
t.penup()
t.fillcolor("#FFFFFF")
t.goto(169 * S + X , -93 * S + Y)
t.pendown()
t.seth(0)
t.circle(-9 * S, 360)
t.end_fill()

和左吊带的区别:
1、右吊带的吊带和扣子是放在一个函数内;
2、使用的是seth() 而不是setheading(),但其实等价;
3、左吊带第一个 t.goto(-130 * S + X , 10 + Y),纵坐标似乎需要*S
分析之后发现第三个是问题关键!
那为何单独绘制的小黄人没有问题呢?
原来是源代码中设置的比例系数S=1 ,故而不影响。
而组合代码中设置的S=0.3 ,于是问题显露!

除了自己的细节错误之外,可见源代码有许多可以优化的地方!

我想要达到的结果

修改后的代码:

左吊带

def zuodd():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(-130 * S + X , 10 * S + Y)
t.pendown()
t.seth(-59)
t.circle(225 * S, 38)
t.seth(-100)
t.forward(28 * S)
t.seth(160)
t.circle(-255 * S, 35)
t.seth(90)
t.circle(-30 * S, 45)
t.goto(-130 * S + X , 10 * S + Y)
t.end_fill()

t.begin_fill()
t.penup()

t.fillcolor("#FFFFFF")
t.goto(-40 * S + X , -80 * S + Y)
t.pendown()
t.seth(0)
t.circle(-9 * S, 360)
t.end_fill()

右吊带

def youdd():
t.begin_fill()
t.penup()

t.fillcolor("#0045D9")
t.goto(191 * S + X , -40 * S + Y)
t.pendown()
t.seth(-112)
t.circle(-250 * S, 17)
t.seth(-68)
t.fd(25 * S)
t.seth(49)
t.circle(130 * S, 36)
t.goto(191 * S + X , -40 * S + Y)
t.end_fill()

t.begin_fill()
t.penup()
t.fillcolor("#FFFFFF")
t.goto(169 * S + X , -93 * S + Y)
t.pendown()
t.seth(0)
t.circle(-9 * S, 360)
t.end_fill()

实现的效果:

img

最终实现效果:

img

本人在编写提问帖子的时候发现并解决了这个问题,希望各位不要犯这种低级错误,写出简洁的代码!

  • 写回答

2条回答 默认 最新

  • chuifengde 2022-06-16 13:15
    关注

    恭喜楼主,贺喜楼主!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月9日
  • 已采纳回答 7月1日
  • 创建了问题 6月16日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效