python - 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.")
解决方案
负坐标没有问题。缩进不正确。仅当您选择 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()
...........
推荐阅读
- visual-studio-code - 如何在 settings.json 中为给定语言指定折叠开始和折叠结束模式
- c# - 匹配音频文件长度的秒和毫秒
- javascript - 为旨在作为集合的对象创建布尔值
- jquery - 如何制作一个过滤类名(多个)下拉列表的 jQuery 过滤器
- javascript - javascript播放声音,好像它来自左侧
- javascript - Javascript - 创建 div 后,Ajax 无法正常工作
- regex - 正则表达式在整个 ini 文件中提取类似的配置块
- javascript - 在js中将逗号分隔符拆分为新行
- jhipster - JHIPSTER JDL:连接字段和显示字段之间的混淆
- scala - 如何测试自定义播放框架序列化程序