我找不到能创建正确的“乌龟”图案的方法,因为我的代码只画了一个正方形

时间:2019-04-11 00:34:05

标签: python zelle-graphics

任务是做一个乌龟库会做的事情,但是没有该库。

这是我获得的图纸 This is the drawing that I obtain 它需要使用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()

1 个答案:

答案 0 :(得分:0)

我的信念是您没有正确地控制角度。由于您基本上是一遍又一遍地绘制正方形,每次旋转一点,因此角度为90或-90度。通过传递给函数的小旋转角,它们在正方形之间稍微倾斜。这是绘制所需目标的代码的高度简化版本:

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()

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

enter image description here