我找不到创建正确 "turtle" 模式的方法,因为我的代码只绘制了一个正方形

I cannot find a way to create a correct "turtle" pattern, as my code only draws a square

任务是做海龟库在没有库的情况下会做的事情。

这是我得到的图 它需要用 for 循环追踪路径,但它不应该到达它开始的同一个地方,因为旋转角度应该使海龟每次都落在不同的点上。 BTW derecha(右), izquierda(左), arriba(上) abajo(下)

from graphics import*
import math
ancho = 500
alto = 500
a=alto/2
b=ancho/2
win = GraphWin ("Estrella",ancho,alto)
origen=Point(ancho/2, alto/2)
dirTort="derecha"

def T(dir,dist,ang):
    global dirTort,a,b
    ang=ang*math.pi/180
    for i in range (40):
        if dir == "derecha":
            if dirTort == "derecha":
                dirTort = "abajo"
                valx = math.cos(ang)
                valy = math.sin(ang)
            elif dirTort == "izquierda":
                dirTort = "arriba"
                valx = -math.cos(ang)
                valy = -math.sin(ang)
            elif dirTort == "arriba":
                dirTort = "derecha"
                valx = math.sin(ang)
                valy = -math.cos(ang)
            elif dirTort == "abajo":
                dirTort = "izquierda"
                valx = -math.sin(ang)
                valy = math.cos(ang)
        elif dir == "izquierda":
            if dirTort == "derecha":
                dirTort = "abajo"
                valx = -math.cos(ang)
                valy = -math.sin(ang)
            elif dirTort == "izquierda":
                dirTort = "arriba"
                valx = -math.cos(ang)
                valy = math.sin(ang)
            elif dirTort == "arriba":
                dirTort = "derecha"
                valx = -math.sin(ang)
                valy = -math.cos(ang)
            elif dirTort == "abajo":
                dirTort = "derecha"
                valx = math.sin(ang)
                valy = math.cos(ang)
        p1 = Point(a, b)
        coordx=a+dist*valx
        coordy=b+dist*valy
        p2=Point(coordx, coordy)
        linea=Line(p1,p2)
        linea.setFill("red")
        linea.draw(win)
        a=coordx
        b=coordy

    print(coordx,coordy)

T("derecha",200,10)
print(dirTort)

message = Text(Point(win.getWidth()/2,win.getHeight()/15),"Click para salir")
message.draw(win)
win.getMouse()

我认为您没有正确管理角度。由于您基本上是一遍又一遍地绘制一个正方形,每次都旋转一点,因此您的角度为 90 度或 -90 度。这是通过传入函数的小旋转角度稍微倾斜 between 正方形。这是绘制所需目标的代码的高度简化版本:

from graphics import *
from math import pi, sin as sine, cos as cosine, radians

WIDTH, HEIGHT = 500, 500
win = GraphWin("Star", WIDTH, HEIGHT)
origin = Point(WIDTH/2, HEIGHT/2)

def star(distance, angle):

    theta = 0
    angle_in_radians = radians(angle)
    p1 = origin.clone()

    for _ in range(360 // angle):

        for _ in range(4):  # draw a square
            theta += pi / 2

            p2 = p1.clone()
            p2.move(distance * cosine(theta), distance * sine(theta))

            line = Line(p1, p2)
            line.setFill("red")
            line.draw(win)

            p1 = p2

        theta += angle_in_radians  # tilt our starting point slightly

star(175, 9)

message = Text(Point(WIDTH/2, HEIGHT/15), "Click to exit")
message.draw(win)
win.getMouse()

希望它能帮助您使代码重回正轨。