import turtle def square(x,y,size,s_color): turtle.up() turtle.goto(x,y) turtle.down() turtle.color(s_color) turtle.begin_fill() for i in range(4): turtle.fd(size) turtle.left(90) turtle.end_fill() #turtle.update() turtle.hideturtle() turtle.tracer(False) turtle.setup(420,420,0,0) square(10,10,10,'red') turtle.update()
时间: 2023-10-13 21:06:18 浏览: 137
这段代码定义了一个名为square的函数,用于绘制指定大小和颜色的正方形。该函数使用turtle库中的up()和down()函数控制小海龟光标的移动,使用color()函数设置正方形的颜色,使用begin_fill()和end_fill()函数填充正方形内部。
在主程序中,使用setup()函数设置绘图窗口的大小和位置,使用hideturtle()函数隐藏小海龟光标,使用tracer(False)函数关闭动画效果。接着调用square函数绘制一个红色的正方形,最后使用update()函数更新绘图窗口。
相关问题
为什么会没有显示任何图形呢 import turtle #角度 def getPosition(x, y): turtle.setx(x) turtle.sety(y) print(x, y) class Pikachu: def __init__(self): self.t = turtle.Turtle() t = self.t t.pensize(3) t.speed(9) t.ondrag(getPosition) def noTrace_goto(self, x, y): self.t.penup() self.t.goto(x, y) self.t.pendown() def leftEye(self, x, y): self.noTrace_goto(x, y) t = self.t t.seth(0) t.fillcolor('#333333') t.begin_fill() t.circle(22) t.end_fill() self.noTrace_goto(x, y + 10) t.fillcolor('#000000') t.begin_fill() t.circle(10) t.end_fill() self.noTrace_goto(x + 6, y + 22) t.fillcolor('#ffffff') t.begin_fill() t.circle(10) t.end_fill() def main(): print('Painting the Pikachu... ') turtle.screensize(800, 600) turtle.title('Pikachu') pikachu = Pikachu() turtle.mainloop() if __name__ == '__main__': main()
这段代码没有显示任何图形的原因可能是因为你没有调用`leftEye`函数来绘制皮卡丘的左眼,或者你的窗口被关闭了。你可以尝试在`main`函数中调用`leftEye`函数来绘制皮卡丘的左眼,例如:
```python
def main():
print('Painting the Pikachu... ')
turtle.screensize(800, 600)
turtle.title('Pikachu')
pikachu = Pikachu()
pikachu.leftEye(-50, 0) # 绘制左眼
turtle.mainloop()
if __name__ == '__main__':
main()
```
另外,你也可以在`leftEye`函数中加入`self.t.hideturtle()`来隐藏乌龟光标,以使绘制的图形更美观。
import turtle def draw_ring(radius, color): turtle.pendown() turtle.color(color) turtle.circle(radius) turtle.penup() def draw_olympic_rings(): turtle.speed(10) turtle.penup() colors = ["blue", "black", "red", "yellow", "green"] for i in range(5): turtle.goto(i * 120, 0) draw_ring(50, colors[i]) turtle.hideturtle() draw_olympic_rings() turtle.done()
这段代码是一个 Python 程序,使用了 turtle 库来绘制奥林匹克五环图案。
它定义了两个函数:
1. draw_ring(radius, color):绘制半径为 radius,颜色为 color 的圆环。
2. draw_olympic_rings():绘制奥林匹克五环图案。
代码中,首先定义了五种颜色:"blue", "black", "red", "yellow", "green",然后使用 for 循环依次绘制五个圆环。最后调用 draw_olympic_rings() 函数绘制奥林匹克五环图案,最后调用 turtle.done() 等待绘制完成。
阅读全文
相关推荐

















