我需要想出一种方法,让我的程序告诉用户他们是赢是输取决于乌龟是否在圈内



这段代码让海龟做出一个形状,然后让用户向前、向后、向右和向左移动十次,将海龟移回圆圈。我需要找出一种方法,让我的程序告诉用户他们是赢是输取决于海龟是否在圈内,我试图使用xpos和ypos命令来告诉海龟在哪里,但我不确定如何使用它们,以便我可以让它告诉海龟何时在圈内。

#Importing the package
import turtle
shape = str()
direction = str()
#Change turtle color and background color
turtle.color("green")
turtle.Screen().bgcolor("blue")
screen = turtle.Screen()
#Ask the user what shape they want the turtle to be
shape = turtle.textinput("Enter a shape", "Enter a shape: classic, arrow, turtle, circle, square, or triangle.")
turtle.shape(shape)
#Making a circle
turtle.penup()
turtle.goto(-200, -100)
turtle.pendown()
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
turtle.penup()
turtle.color("green")
#Move turtle back to start
turtle.goto(0, 0)
#Moving the turtle into the circle
for counter in range(1, 11, 1):
#getting the direction from the user
direction = screen.textinput("Direction", "Enter F for Forward, B for Backward, R for Right and L for Left")
if direction == "F":
turtle.forward(100)
elif direction == "B":
turtle.backward(100)
elif direction == "R":
turtle.right(90)
turtle.forward(100)
else:
turtle.left(90)
turtle.forward(100)
#end if
#Find where the turtles ending position is
xpos = turtle.xcor()
ypos = turtle.ycor()
turtle.write("Sorry You Lost", align="center", font=('Arial', '16', 'bold'))
turtle.write("Good Job You Won!", align="center", font=('Arial', '16', 'bold'))
turtle.exitonclick()
from turtle import Screen, Turtle
#Change turtle color and background color
tim = Turtle()
screen = Screen()
tim.color("green")
screen.bgcolor("blue")
#Ask the user what shape they want the turtle to be
shape = screen.textinput("Enter a shape", "Enter a shape: classic, arrow, turtle, circle, square, or triangle: ")
tim.shape(shape)
#Making a circle
tim.penup()
tim.goto(-200, -100)
tim.pendown()
tim.fillcolor("red")
tim.begin_fill()
tim.circle(100)
tim.end_fill()
tim.penup()
tim.color("green")
#Move turtle back to start
tim.goto(0, 0)
#Moving the turtle into the circle
for counter in range(1, 11, 1):
#getting the direction from the user
direction = screen.textinput("Direction", "Enter F for Forward, B for Backward, R for Right and L for Left")
if direction == "F":
tim.forward(100)
elif direction == "B":
tim.backward(100)
elif direction == "R":
tim.right(90)
tim.forward(100)
else:
tim.left(90)
tim.forward(100)
#end if
#Find where the turtles ending position is
xpos = tim.xcor()
ypos = tim.ycor()
print(xpos)
print(ypos)
tim.write("Sorry You Lost", align="center")
tim.write("Good Job You Won!", align="center")
screen.exitonclick()

在这里,我修复了你的代码,我认为它现在是你真正想要的工作。

相关内容