如何判断Python龟是否在squ中

2024-04-26 15:05:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用两只乌龟写寻宝代码。两者都被设置为随机坐标——其中一个供用户使用,而另一个则绘制一个正方形,这意味着地图的宝藏区。我的问题是想知道乌龟什么时候在正方形的范围内。 这就是我目前所拥有的:

import turtle
from random import randint
#create turtle and its shape
pepe= turtle.Turtle()
pepe.shape("turtle")
pepe.color("blue")
pepex=randint(0,250)
pepey=randint(0,250)
pepe.setposition(pepex, pepey)
#create a new screen and set screen size
scrn1 = turtle.Screen ()
scrn1.screensize(500,500)
scrn1.bgcolor("pink")
scrn1.title("Treasure hunt")
#create turtle that draws treasure square, set in random positon
pat = turtle.Turtle()
pat.shape("arrow")
pat.color("red")
patx=randint(0,250)
paty=randint(0,250)
pat.setposition(patx, paty)
pat.begin_fill()
for i in range (4):
    pat.forward(30)
    pat.left(90)
pat.end_fill()
#prompt user if they want to go on a treasure hunt
treasure_hunt=input("Do you want to go on a treasure hunt? Press y to continue")
while treasure_hunt=='y':
    #ask user for input to move their turtle, each time sets the angle back to 0 to move right and 90 to move up
    left_or_right=int(input("Enter number between -250 and 250 to move left or right"))
    pepe.seth(0)
    pepe.forward(left_or_right)
    up_or_down=int(input("Enter number between -250 and 250 to move up or down"))
    pepe.seth(90)
    pepe.forward(up_or_down)
    if pepe.distance(pat) ==0:
        break
pat.write("You did it!", font=("Arial", 16, "normal"))

Tags: orandtorightinputmoveleftrandint
1条回答
网友
1楼 · 发布于 2024-04-26 15:05:57

解决这一问题的一个简单方法是让pat成为宝藏,而不是抽取宝藏。我们不是画一个正方形,而是把光标变成一个正方形的形状,调整它的大小,然后把它放在我们想要的地方。那么两个海龟之间的distance()方法就有意义了:

from turtle import Screen, Turtle
from random import randint

CURSOR_SIZE = 20

# create a new screen and set screen size
screen = Screen()
screen.screensize(500, 500)
screen.bgcolor("pink")
screen.title("Treasure hunt")

# create turtle and its shape
pepe = Turtle("turtle", visible=False)
pepe.color("blue")
pepe.penup()
pepe.setposition(randint(-250, 250), randint(-250, 250))
pepe.pendown()
pepe.showturtle()

# create turtle that draws treasure square, set in random position
pat = Turtle('square', visible=False)
pat.shapesize(30 / CURSOR_SIZE)
pat.color("black", "red")
pat.penup()
pat.setposition(randint(-250, 250), randint(-250, 250))
pat.showturtle()

# prompt user if they want to go on a treasure hunt
treasure_hunt = input("Do you want to go on a treasure hunt? Press y to continue: ")

while treasure_hunt == 'y':
    # ask user for input to move their turtle, each time sets
    # the angle back to 0 to move right and 90 to move up

    left_or_right = int(input("Enter number between -250 and 250 to move left or right: "))
    pepe.setheading(0)
    pepe.forward(left_or_right)

    up_or_down = int(input("Enter number between -250 and 250 to move up or down: "))
    pepe.setheading(90)
    pepe.forward(up_or_down)

    if pepe.distance(pat) < 15:
        pat.write("You did it!", font=("Arial", 16, "normal"))
        break

screen.mainloop()

enter image description here

相关问题 更多 >