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? :)
