Python 如何检查两个海龟在海龟图形上的x,y坐标是否相同?

Python 如何检查两个海龟在海龟图形上的x,y坐标是否相同?,python,turtle-graphics,snakemake,Python,Turtle Graphics,Snakemake,作为我的第一个海龟项目,我开始编写“蛇”游戏的代码,但它不是蛇,而是一只随意吃食物的海龟 我吃这些食物时遇到了一个问题。我的意思是,必须有一个if语句来检查蛇(也就是海龟)和食物(也就是海龟)是否在相同的XY坐标中。如果是这样的话,首先,将海龟的大小放大,然后隐藏食物,获取另一个随机坐标,然后在屏幕上显示 这是我的密码: from turtle import * from random import * def go(): # the main walking function fo

作为我的第一个海龟项目,我开始编写“蛇”游戏的代码,但它不是蛇,而是一只随意吃食物的海龟

我吃这些食物时遇到了一个问题。我的意思是,必须有一个
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()
方法,但我不知道应该用什么来代替。你能帮我吗?:)

如果turtle1.pos()==turtle2.pos():
打印(“两只乌龟在一起”)

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

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:您可以根据自己的喜好设置半径值。我只使用了两个随机值。

以下是我想解决的几个问题:

  • 当您只需要从模块导入一到两个函数时, 不要使用星号来导入所有函数

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

  • 调用
    屏幕是一种浪费效率的行为。在
    循环时,在
    内部按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()
    
    输出:


    Hmm,您可以实时打印(turtle1.pos())和打印(turtle2.pos()),看看这是(if)函数中的问题还是其他问题。@HosnaH您还需要随着海龟大小的增加扩展海龟边界。如果答案解决了问题,请将其标记为已接受,以便将来帮助他人。