You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python Turtle中按比例缩放蜜蜂图像并调整其朝向?

蜜蜂图像的缩放与朝向调整方案

一、按比例缩放的实现

你的代码已用step作为缩放基础,但存在固定数值(如50)导致比例失衡,需将所有尺寸相关数值改为step的倍数,确保整体比例统一:

  • 替换所有固定像素值为step关联的比例值
  • 保持绘制的长度、半径等参数均以step为基准

二、调整朝向的实现

利用Turtle的角度控制功能,在绘制蜜蜂前调整画笔朝向,即可改变整体图像方向:

  • 添加rotation参数控制旋转角度
  • 绘制前调用setheading(rotation)设置初始朝向

修复并优化后的完整代码

import turtle

def draw_body(x, y, step):
    # Body outline
    turtle.penup()
    turtle.pencolor('black')
    turtle.fillcolor('gold')
    turtle.goto(x, y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(90)
    turtle.circle((step * 5), 180)
    turtle.forward(step * 10)
    turtle.circle((step * 5), 180)
    turtle.forward(step * 10)
    turtle.end_fill()
    
    # Draw Stripe 1
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.setheading(180)
    turtle.forward(step * 10)
    turtle.setheading(270)
    turtle.forward(step * 4)
    turtle.setheading(0)
    turtle.forward(step * 10)
    turtle.setheading(90)
    turtle.forward(step * 4)
    turtle.end_fill()
    
    # Goto Stripe 2
    turtle.penup()
    turtle.setheading(180)
    turtle.forward(step * 10)
    turtle.setheading(270)
    turtle.forward(step * 6)
    
    # Draw Stripe 2
    turtle.setheading(0)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(step * 10)
    turtle.setheading(270)
    turtle.forward(step * 4)
    turtle.setheading(180)
    turtle.forward(step * 10)
    turtle.setheading(90)
    turtle.forward(step * 4)
    turtle.end_fill()
    turtle.penup()
    
    # Draw stinger(替换固定值为step比例)
    turtle.goto((x - step*5), (y - (step * 15)))
    turtle.setheading(270)
    turtle.pendown()
    turtle.width(5)
    turtle.forward(step)
    turtle.penup()
    turtle.width(3)
    
    # Draw right wing
    turtle.goto(x, (y * 0.66))
    turtle.setheading(25)
    turtle.fillcolor('gainsboro')
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(step * 5)
    turtle.circle((-step * 3.5), 180)
    turtle.goto(x, (-y * 0.66))
    turtle.goto(x, (y * 0.66))
    turtle.end_fill()
    turtle.penup()
    
    # Draw left wing
    turtle.goto(-x, (y * 0.66))
    turtle.setheading(155)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(step * 5)
    turtle.circle((step * 3.5), 180)
    turtle.goto(-x, (-y * 0.66))
    turtle.end_fill()
    turtle.penup()

def draw_head(x, y, step):
    # Head outline(替换固定值为step比例)
    turtle.width(3)
    turtle.pencolor('black')
    turtle.fillcolor('gold')
    turtle.penup()
    turtle.goto((x - step*5), (y * 1.5))
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(0)
    turtle.circle((step * 6), 360)
    turtle.end_fill()
    turtle.penup()
    
    # Eyes(调整坐标为step相关比例)
    eye_y = step * 2.75
    turtle.goto((x * 0.5), eye_y)
    turtle.pendown()
    turtle.fillcolor('white')
    turtle.begin_fill()
    turtle.circle(step, 360)
    turtle.penup()
    turtle.goto((-x * 0.5), eye_y)
    turtle.pendown()
    turtle.circle(step, 360)
    turtle.end_fill()
    turtle.penup()
    
    # Pupils
    pupil_y = step * 2.9
    turtle.goto((-x * 0.5), pupil_y)
    turtle.pendown()
    turtle.fillcolor('black')
    turtle.begin_fill()
    turtle.circle((step * 0.3), 360)
    turtle.penup()
    turtle.forward(step * 5)
    turtle.pendown()
    turtle.circle((step * 0.3), 360)
    turtle.end_fill()
    turtle.penup()
    
    # Mouth
    turtle.setheading(270)
    turtle.forward(step * 3)
    turtle.pendown()
    turtle.setheading(270)
    turtle.circle((-step * 2.5), 180)
    turtle.penup()
    
    # Antennae
    antenna_y = step * 3.6
    turtle.goto((-x * 0.3), antenna_y)
    turtle.setheading(100)
    turtle.pendown()
    turtle.forward(step * 4)
    turtle.setheading(220)
    turtle.forward(step * 2)
    turtle.penup()
    turtle.goto((x * 0.3), antenna_y)
    turtle.setheading(80)
    turtle.pendown()
    turtle.forward(step * 4)
    turtle.setheading(320)
    turtle.forward(step * 2)
    turtle.penup()
    turtle.speed(0)
    turtle.width(3)

def draw_main_body(x, y, step, rotation=0):
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(rotation)  # 设置整体朝向
    turtle.pendown()
    
    draw_body(x, y, step)
    draw_head(x, y, step)

# 主程序
turtle.title('The Australian Honey Bees-nest/Business')
turtle.write("Australia's Honey production is abundant!", move=True, align='left',
             font=("Arial", 8, "bold"))

# 示例:缩放step=8,朝向旋转30度
draw_main_body(0, 0, 8, rotation=30)

turtle.hideturtle()
turtle.done()

使用说明

  1. 缩放控制:修改draw_main_body的step参数,数值越大蜜蜂尺寸越大,所有比例自动保持一致
  2. 朝向调整:修改rotation参数,输入角度值(0为默认向上,90为向右,180为向下等),整个蜜蜂会按该角度旋转
  3. 修复了原代码中坐标计算的逻辑问题,避免缩放时位置错乱

内容的提问来源于stack exchange,提问作者Scudethius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:10:01