Turtle Graphics环形矩形绘制:代码输出与预期不符的修正需求
修正Python Turtle绘图代码以匹配预期效果
基础说明
使用Python的turtle模块绘图(属标准库,无需额外安装),核心流程分三步:
- 导入
turtle模块 - 创建控制用海龟对象
- 调用海龟方法完成绘图
绘图要求
- 矩形使用指定的填充色与边框色
- 可自定义矩形长宽,需保证整体图案效果一致
- 正确设置绘图起始坐标,确保图案在屏幕居中
问题描述
现有代码运行后的输出与预期效果不符,需调整代码逻辑使输出匹配预期。
现有代码
from turtle import * # Set up the screen and turtle screen = Screen() t = Turtle() t.speed(1) # You can adjust the speed as needed # Define colors color_fill = "yellow" color_border = "blue" border_size = 5 gap_size = 10 rectangle_width = 50 # Adjust the width as needed rectangle_height = 100 # Adjust the height as needed circle_radius = 50 # Adjust the radius for the circular space # Function to draw a colored rectangle with a border def draw_rectangle_with_border(fill_color, border_color, border_size, width, height): # Draw the border t.pencolor(border_color) t.pensize(border_size) t.penup() t.goto(-width / 2, -height / 2) t.pendown() for _ in range(2): t.forward(width) t.left(90) t.forward(height) t.left(90) # Draw the fill t.fillcolor(fill_color) t.begin_fill() for _ in range(2): t.forward(width) t.left(90) t.forward(height) t.left(90) t.end_fill() # Set the starting position t.penup() t.goto(0, -rectangle_height / 2) # Draw the central circular space t.pencolor("white") # Set the color to match the background t.fillcolor("white") t.penup() t.goto(0, -circle_radius) t.pendown() t.begin_fill() t.circle(circle_radius) t.end_fill() # Calculate the total number of rectangles to form a complete circle num_rectangles = 8 angle = 360 / num_rectangles # Draw the circular pattern of rectangles around the central circular space for _ in range(num_rectangles): draw_rectangle_with_border(color_fill, color_border, border_size, rectangle_width, rectangle_height) t.penup() t.goto(0, -rectangle_height / 2) t.left(angle) t.forward(gap_size) # Close the window on click screen.exitonclick()
修正后的代码
from turtle import * # 初始化屏幕和海龟对象 screen = Screen() t = Turtle() t.speed(1) # 定义绘图参数 color_fill = "yellow" color_border = "blue" border_size = 5 gap_size = 10 rectangle_width = 50 rectangle_height = 100 circle_radius = 50 # 绘制带边框的填充矩形 def draw_rectangle_with_border(fill_color, border_color, border_size, width, height): t.fillcolor(fill_color) t.begin_fill() t.pencolor(border_color) t.pensize(border_size) t.penup() t.goto(-width / 2, -height / 2) t.pendown() # 一次循环完成边框和填充绘制 for _ in range(2): t.forward(width) t.left(90) t.forward(height) t.left(90) t.end_fill() # 先绘制中心白色圆形 t.pencolor("white") t.fillcolor("white") t.penup() t.goto(0, -circle_radius) t.pendown() t.begin_fill() t.circle(circle_radius) t.end_fill() # 计算矩形排列参数 num_rectangles = 8 angle = 360 / num_rectangles # 矩形起始位置到中心的距离:中心圆半径 + 矩形高度一半 + 间隙 start_radius = circle_radius + rectangle_height / 2 + gap_size # 环形绘制外围矩形 for idx in range(num_rectangles): t.penup() t.goto(0, 0) # 调整海龟朝向,让矩形垂直向外 t.setheading(90 + idx * angle) t.forward(start_radius) t.setheading(0) # 重置朝向,保证矩形绘制方向正确 draw_rectangle_with_border(color_fill, color_border, border_size, rectangle_width, rectangle_height) # 点击窗口关闭程序 screen.exitonclick()
修正要点
- 调整绘图顺序:先绘制中心白色圆形,避免后续矩形被白色圆覆盖
- 修复矩形定位逻辑:以屏幕中心为原点,计算每个矩形的起始位置,通过朝向调整保证矩形垂直向外排列
- 优化矩形绘制函数:将边框与填充合并为一次路径绘制,避免重复操作,同时保证边框显示正常
- 修正循环位置重置:每次绘制完矩形后回到中心,重新计算下一个矩形的位置,避免累积偏移
内容的提问来源于stack exchange,提问作者xlu_uk dev
相关产品推荐
相关产品推荐

