简体   繁体   English

如何在形状龟蟒里面画点?

[英]How to draw dot inside shape turtle python?

I am new with Turtle python library and I am trying to draw turtle dot inside the shape (square), which should look like on the picture below.我是 Turtle python 库的新手,我正在尝试在形状(正方形)内绘制海龟点,如下图所示。 The problem is that when I am trying to do this the shape covers the dot and I see only the shape (square).问题是当我尝试这样做时,形状覆盖了点,我只看到形状(正方形)。

enter image description here在此处输入图像描述

My code:我的代码:

def add_dot_square():
    obj = Turtle()
    obj.penup()
    obj.shape("square")
    obj.shapesize(1.5, 1.5)
    obj.color("orange")
    obj.goto(0, 0)
    obj.dot(20, "red")

Turtle's can't appear behind things they draw, only other turtles (and even that's tricky.) Instead of the turtle being the square, have the turtle draw or stamp the square, and then place the dot atop it:海龟不能出现在他们画的东西后面,只能出现在其他海龟后面(即使这很棘手。)不要让海龟成为正方形,而是让海龟标记正方形,然后将点放在它上面:

from turtle import Screen, Turtle

def add_dot_square(obj):
    obj.penup()
    obj.shape('square')
    obj.shapesize(1.5)
    obj.color('orange')
    obj.goto(0, 0)

    obj.stamp()
    obj.dot(20, 'red')

screen = Screen()

turtle = Turtle()
turtle.hideturtle()

add_dot_square(turtle)

screen.exitonclick()

在此处输入图像描述

I still ha[v]ea problem when I want to for example move this square and dot例如,当我想移动这个正方形和点时,我仍然有问题

Let's rearrange the code a bit and add some motion:让我们重新排列代码并添加一些动作:

from turtle import Screen, Turtle

def add_dot_square(obj):
    obj.clear()
    obj.stamp()
    obj.dot(20, 'red')

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(1.5)
turtle.color('orange')
turtle.penup()

for _ in range(360):
    turtle.circle(100, extent=1)
    add_dot_square(turtle)
    screen.update()

screen.exitonclick()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM