【问题标题】:How to check if two turtles have the same x, y coordinates on turtle graphics?如何检查两只海龟在海龟图形上是否具有相同的 x、y 坐标?
【发布时间】:2020-11-15 11:02:54
【问题描述】:

作为我的第一个海龟项目,我开始编写“蛇”游戏,但不是蛇,而是海龟吃随机协调的食物。

我在吃食物时遇到了问题。我的意思是,必须有一个if 语句来检查蛇(即乌龟)和食物(也是乌龟)是否在相同的 XY 坐标中。如果是这样,首先,将乌龟的尺寸提高,然后隐藏食物,为其获取另一个随机坐标,然后将其显示在屏幕上。

这是我的代码:

from turtle import *
from random import *


def go():
    # the main walking function for the turtle
    turtle.forward(2)


def rotate():
    # to rotate the turtle 90 degrees to the left
    turtle.left(90)


def getfood():
    # get random coordinates for the food
    x = randint(-280, 280)
    y = randint(-280, 280)
    # set the food to the random position
    food.hideturtle()
    food.up()
    food.goto(x, y)
    food.showturtle()


turtle = Turtle()
screen = Screen()
screensize(600, 600)
food = Turtle()
food.shape('circle')
turtle.shape('turtle')
turtle.shapesize(3)
turtleSize = 3
getfood()
while True:
    turtle.up()
    go()
    #  check if the turtle has eaten the food.
    if food.xcor == turtle.xcor and food.ycor() == turtle.ycor():
        turtleSize += 1
        turtle.shapesize(turtleSize)
        getfood()
    # let the player rotate pressing the "a" key
    screen.listen()
    screen.onkeypress(rotate, 'a')

问题就在那里,在if 语句中,它检查乌龟是否吃过食物。执行甚至没有进入它。一定是因为那些xcor()ycor() 方法,但我不知道应该改用什么。你能帮我吗? :)

【问题讨论】:

    标签: python turtle-graphics snakemake


    【解决方案1】:

    您可以做的是以半径的形式定义海龟和食物的碰撞边界,并检查两个半径之和是否大于从一只海龟到另一只海龟的distance。以下代码应该可以工作。

    turtle.radius = 10
    food.radius = 5
    while True:
        turtle.up()
        go()
        #  check if the turtle has eaten the food.
        if (turtle.radius+food.radius)>=food.distance(turtle):
            turtleSize += 1
            turtle.shapesize(turtleSize)
            getfood()
        # let the player rotate pressing the "a" key
        screen.listen()
        screen.onkeypress(rotate, 'a')
    

    ps:您可以根据需要设置半径值。我只是使用了两个随机值。

    【讨论】:

    • @HosnaH 随着海龟变大,您还需要扩展海龟边界。如果它解决了问题,请将答案标记为已接受,以便将来对其他人有所帮助。
    【解决方案2】:

    如果 turtle1.pos() == turtle2.pos(): print('两只乌龟在一起')

    【讨论】:

    • 嗯,你 print(turtle1.pos()) 和 print(turtle2.pos()) 实时看看是 (if) 函数的问题还是其他问题。跨度>
    【解决方案3】:

    以下是我想解决的一些问题:

    1. 当你只需要从一个模块中导入一到两个函数时, 不要使用星号来导入所有函数。

    2. 不要在你的海龟对象上使用未绑定的函数, 创建一个类。在那里您可以安全地存储动态属性,例如海龟吃东西时的大小。

    3. while循环内部调用screen.onkeypress是浪费效率;在循环足够之前调用一次。

    这是对您的代码的重写,并实现了进食动作:

    from turtle import Turtle, Screen
    from random import randint
    from time import sleep
    
    class Eater(Turtle):
        def __init__(self, size):
            Turtle.__init__(self)
            self.shapesize(size)
            self.shape('turtle')
            self.penup()
            self.turtleSize = size
    
        def touched(self, food):
            return self.turtleSize * 10 >= food.distance(self)
    
        def eat(self, food):
            self.turtleSize += 1
            self.shapesize(self.turtleSize)
            x = randint(-280, 280)
            y = randint(-280, 280)
            food.goto(x, y)
    
        def go(self):
            self.forward(2)
    
        def rotate(self):
            self.left(90)
    
    screen = Screen()
    screen.setup(600, 600)
    screen.tracer(0)
    
    food = Turtle('circle')
    food.penup()
    
    turtle = Eater(3)
    
    screen.listen()
    screen.onkeypress(turtle.rotate, 'a')
    
    while True:
        sleep(0.01)
        turtle.go()
        if turtle.touched(food):
            turtle.eat(food)
        screen.update()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2018-05-30
      相关资源
      最近更新 更多