首页 > 解决方案 > Turtle.goto 函数仅适用于两个正坐标?

问题描述

运行我的代码时没有错误,但我认为问题在于turtle.goto 函数。它仅适用于两个正坐标。我似乎找不到靠近我的问题,我已经尝试了所有不同的海龟函数来建立坐标。我不确定我错过了什么或还能做什么。

我知道这段代码可能看起来很草率,但我是编码新手,这只是一门大学课程。

import turtle
import time

#Asking for the shape
shape = int(input("Would you like a Square or a Circle? Type 1 for Square or 2 for Circle: "))

#Asking for the (x,y) coordinates
coordinate = int(input("Which option would you like your shape to be drawn in? please enter a number 1-4. 1. top left, 2. bottom left, 3. top right, 4. bottom right: "))
color = int(input("What color would you like your pen to be? Input 1 for red, 2 for blue, or 3 for yellow."))

#Executing if statment
if shape == 2:
    if color == 1:
        turtle.pencolor("red")
        turtle.fillcolor("blue")
    elif color == 2:
        turtle.pencolor("blue")
        turtle.fillcolor("yellow")
    else:
        turtle.pencolor("yellow")
        turtle.fillcolor("red")
        
        
#Determining pensize due to coordinate and establishing coordinate plane
        if coordinate == 1:
            turtle.penup()
            turtle.postion(-150,150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        elif coordinate == 2:
            turtle.penup()
            turtle.setpos(-150,-150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        elif coordinate == 3:
            turtle.penup()
            turtle.goto(150,150)
            turtle.pendown()
            turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()
        else:
            turtle.penup()
            turtle.goto(150,-150)
            turtle.pendown()
            Turtle.pensize(3)
            turtle.begin_fill()
            turtle.circle(50)
            turtle.end_fill()

#Squares if statement
elif shape == 1:

    if color == 1:
        turtle.fillcolor("red")
        turtle.pencolor("blue")
    elif color == 2:
        turtle.fillcolor("blue")
        turtle.pencolor("yellow")
    else:
        turtle.fillcolor("yellow")
        turtle.pencolor("red")
        
#Determining pensize due to coordinate and establishing coordinate plane
        if coordinate == 1:
            turtle.penup()
            turtle.goto(-300,300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        elif coordinate == 2:
            turtle.penup()
            turtle.goto(-300,-300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        elif coordinate == 3:
            turtle.penup()
            turtle.goto(300,300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()
        else:
            turtle.penup()
            turtle.goto(300,-300)
            turtle.pendown()
            turtle.begin_fill()
            turtle.right(90)
            turtle.forward(50)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(100)
            turtle.left(90)
            turtle.forward(50)
            turtle.end_fill()

else:
    print("Your shape is not an option.")
    
    
    

标签: pythonturtle-graphicspython-turtle

解决方案


负坐标没有问题。缩进不正确。仅当您选择 color=3 时才会绘制形状。

这是更新的代码:

import turtle
import time

#Asking for the shape
shape = int(input("Would you like a Square or a Circle? Type 1 for Square or 2 for Circle: "))

#Asking for the (x,y) coordinates
coordinate = int(input("Which option would you like your shape to be drawn in? please enter a number 1-4. 1. top left, 2. bottom left, 3. top right, 4. bottom right: "))
color = int(input("What color would you like your pen to be? Input 1 for red, 2 for blue, or 3 for yellow."))

#Executing if statment
if shape == 2:
    if color == 1:
        turtle.pencolor("red")
        turtle.fillcolor("blue")
    elif color == 2:
        turtle.pencolor("blue")
        turtle.fillcolor("yellow")
    else:
        turtle.pencolor("yellow")
        turtle.fillcolor("red")
        
        
#Determining pensize due to coordinate and establishing coordinate plane
    if coordinate == 1:   # shifted left to align with 'if color`
        turtle.penup()
        turtle.postion(-150,150)
        turtle.pendown()
        Turtle.pensize(3)
        turtle.begin_fill()
        turtle.circle(50)
        turtle.end_fill()
 ...........

推荐阅读