简体   繁体   中英

How to check if two turtles have the same x, y coordinates on turtle graphics?

As my first turtle project I started to code the "snake" game, but instead of a snake it's just a turtle eating random-coordinated foods.

I faced a problem when eating the food. I mean, there has to be an if statement which checks if the snake (that is, a turtle) and the food (which is also a turtle) are in the same XY coordinates. If so, first, higher up the size of the turtle, then hide the food, get another random coordinate for it, and then show it on the screen.

Here's my code:

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')

The problem is right there, on the if statement where it checks if the turtle has eaten the food or not. The execution doesn't even get into it. it has to be because of those xcor() and ycor() methods but I don't know what should I use instead. Would you help me? :)

if turtle1.pos() == turtle2.pos(): print('Two turtles are together')

What you can do is define collision boundaries for both turtle and the food in the form of a radius and check whether the sum of the two radii is greater than the distance from one turtle to another. The following code should work.

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: you can set radius values as you prefer. I just used two random values.

Here are a few things I would like to address:

  1. When you only need to import one to two functions from a module, don't use the asterisk to import all the functions.

  2. Instead of using unbound functions on your turtle object, create a class. There you can safely store dynamic attributes, for example, the size of the turtle as it eats.

  3. It is a waste of efficiency to call screen.onkeypress inside the while loop; calling it once before the loop is enough.

Here is a rewrite of your code, with the food eating action implemented:

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()

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM